Git Cheat Sheet
Boost your Git proficiency effortlessly with this essential Git cheat sheet, designed to rescue you from command-related headaches and make you more confident with version control
Git is a distributed version control system widely used in software development to track changes, collaborate with teams, and manage codebases. Whether you’re a beginner or an experienced developer, this guide covers everything from basic Git operations to advanced features like GPG signing and rebasing.
Understanding KEY_ID in Git and GPG
When you work with GPG signing in Git, you’ll encounter the term KEY_ID.
- A KEY_ID is a unique identifier for a GPG key.
- It is used by Git to identify which key to use when signing commits or tags
- You can find your key IDs by running:
gpg --list-secret-keys --keyid-format=longExample output:
/home/user/.gnupg/secring.gpg
------------------------------------
sec rsa4096/4A3B5F8C2D12ABCD 2024-06-15 [SC]
9FBC1234567890ABCDEF1234567890ABCDEF1234
uid [ultimate] John Doe <john@example.com>
ssb rsa4096/8D12ABCD4A3B5F8C 2024-06-15 [E]- In this example, the KEY_ID is:
4A3B5F8C2D12ABCD- You’ll use this ID when configuring Git:
git config --global user.signingkey 4A3B5F8C2D12ABCDGit Command Reference
1. Initial Setup
Global Configuration
# Set your name for all repositories
git config --global user.name "[firstname lastname]"
# Set your email
git config --global user.email "[valid email]"
#Enable colorized output
git config --global color.ui autoRepository Setup
# Initialize a new repository
git init
# Clone an existing repository
git clone [url]2. Basic Operations
Status and Information
# Chek repository status
git status
# View commit history
git log
# Show changes in working directory
git diff
# Show staged chages
git diff --stagedBasic Workflow
# Add files to staging area
git add [file]
# Remove files fom staging area
git reset [file]
# Commit staged changes
git commit -m "[descriptive message]"3. Branch Management
# List all branches
git branch
# Create a new branch
git branch [branch name]
# Create and swith to new branch
git checkout -b [branch-name]
# Switch to a branch
git checkout [branch-name]Merging
# Merge a branch into current branch
git merge [branch-name]
# Rebase current branch onto another
git rebase [branch]4. Remote Operation
Remote Setup
# Add a remote repository
git remote add [alias] [url]
# List remote repositories
git remote -vSyncing Changes
# Download changes without merging
git fetch [alias]
# Download and merge changes
git pull
# Upload local changes
git push [remote] [branch]5. Security and Verification
GPG Key Setup
# Check exisiting GPG keys
gpg --list-secret-keys --keyid-format=long
# Generate new GPG Key
gpg --full-generate-key # Choose RSA and RSA, 4096 bits
# Export your public key
gpg --armor --export [KEY_ID]Git Signing Configuration
# Set signing key
git config --global user.signingkey [KEY_ID]
# Enable automatic commit signing
git config --global commit.gpgsign true
# Enable automatic tag signing
git config --global tag.gpgsign trueSigning Operations
# Sign a commit manually
git commit -S -m "Your commit message"
# Sign a tag
git tag -s v1.0.0 -m "Version 1.0.0"
# Verify Signatures
git verify-commit HEAD
git verify-tag v1.0.06. Temporary Storage
Stashing
# Save changes temporarily
git stash
# List stashed changes
git stash list
# Apply most recent stash
git stash pop
# Remove most recent stash
git stash drop7. History Modification
Changing History
# Modify last commit
git ommit --amend
# Reset to specific commit
git reset --hard [commit]
# Interactive rebase
git rebase -i [commit]8. Advanced Operations
Debugging
# Show commit that introduced a bug
git bisect start
git bisect bad
git bisect good [commit]
# Show file history
git blame [file]Maintenance
# Clean untracked files
git clean -fd
# Compress repository
git gc
# Check repository integrity
git fsck9. GPG Troubleshooting
Common Issues
#Test GPG signing
echo "test" | gpg --clearsign
# Restart GPG agent
gpgconf --kill gpg-agent
gpg-agent --daemonKey Management
# Export private key (backup)
gpg --export-secret-keys --armor [KEY_ID] > private.key
# Import private key
gpg --import private.key
# Update trust level
gpg --edit-key [KEY_ID]
# Type: trust
# Select: 5 (ultimate)
# Type: savePlatform-Specific Setup
# macOS GPG setup
brew install gpg-suite
# Add to ~/.zshrc or ~/.bashrc:
export GPG_TTY=$(tty)Quick Recape:
- KEY_ID is your GPG key identifier for signing commits and tags
- Use gpg —list-secret-keys —keyid-format=long to find your key ID
- Configure Git to use it with:
Shell
git config --global user.signingkey [KEY_ID] - This improves security, accountability, and integrity in collaborative Git workflows.