Contents
Getting Started and Configuration
Initial Setup
# Set your identity for all repositories
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Set your preferred editor
git config --global core.editor "vim"
# Create color output in your terminal
git config --global color.ui auto
# Store credentials for HTTPS repositories
git config --global credential.helper cache
Repository Creation and Cloning
# Initialize a new repository in current directory
git init
# Clone an existing repository
git clone https://github.com/username/repository.git
# Clone a specific branch
git clone -b branch-name https://github.com/username/repository.git
# Clone with a different name for the local directory
git clone https://github.com/username/repository.git custom-name
Daily Workflow Commands
Basic Operations
# Check repository status
git status
# Check status in short format
git status -s
# Show changes in working directory
git diff
# Show staged changes
git diff --staged
# Add files to staging area
git add filename # Add specific file
git add . # Add all files
git add *.js # Add all JavaScript files
git add dir/ # Add all files in directory
# Remove files
git rm filename # Remove file from working directory and staging
git rm --cached filename # Remove file from staging only
# Move/rename files
git mv old-name new-name
Committing Changes
# Create a new commit
git commit -m "Your message"
# Add and commit in one step (for tracked files)
git commit -am "Your message"
# Modify the last commit
git commit --amend -m "New message"
# Add more changes to the last commit
git commit --amend --no-edit
# Create an empty commit (useful for CI triggers)
git commit --allow-empty -m "Empty commit"
Branch Management
Working with Branches
# List all branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create a new branch
git branch branch-name
# Create and switch to new branch
git checkout -b branch-name
# Switch branches
git checkout branch-name
# Delete branch
git branch -d branch-name # Safe delete
git branch -D branch-name # Force delete
# Rename current branch
git branch -m new-name
Remote Operations
# Add remote repository
git remote add origin https://github.com/username/repository.git
# List remote repositories
git remote -v
# Push to remote
git push origin branch-name
git push -u origin branch-name # Set upstream branch
# Fetch changes from remote
git fetch origin
git fetch --all # Fetch all remotes
# Pull changes from remote
git pull origin branch-name
git pull --rebase origin branch-name # Pull with rebase
# Remove remote
git remote remove origin
History and Logs
Viewing History
# Show commit history
git log
# Show compact log
git log --oneline
# Show graph view
git log --graph --oneline --decorate
# Show changes in commits
git log -p
# Show stats
git log --stat
# Show commits by author
git log --author="username"
# Show commits between dates
git log --since="2024-01-01" --until="2024-12-31"
Investigating Changes
# Show who changed each line
git blame filename
# Show commit that introduced a line
git log -S "search-string" filename
# Find lost commits (after reset)
git reflog
Merging and Rebasing
Merge Operations
# Merge branch into current branch
git merge branch-name
# Merge without fast-forward
git merge --no-ff branch-name
# Abort merge
git merge --abort
Rebase Operations
# Rebase current branch onto another
git rebase branch-name
# Interactive rebase
git rebase -i HEAD~3 # Modify last 3 commits
git rebase -i branch-name # Rebase onto branch interactively
# Continue rebase after resolving conflicts
git rebase --continue
# Abort rebase
git rebase --abort
Advanced Operations
Stashing Changes
# Save changes for later
git stash
# Save with description
git stash save "Work in progress"
# List stashes
git stash list
# Apply most recent stash
git stash apply
git stash pop # Apply and remove stash
# Apply specific stash
git stash apply stash@{2}
# Create branch from stash
git stash branch branch-name stash@{0}
# Remove stashes
git stash drop stash@{0} # Remove specific stash
git stash clear # Remove all stashes
Tags
# Create tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"
# List tags
git tag
# Push tags to remote
git push origin v1.0.0
git push origin --tags # Push all tags
# Delete tag
git tag -d v1.0.0
Reset and Restore
# Unstage changes
git restore --staged filename
# Discard changes in working directory
git restore filename
# Reset to specific commit
git reset commit-hash # Soft reset (keep changes)
git reset --hard commit-hash # Hard reset (discard changes)
# Reset to remote branch state
git reset --hard origin/branch-name
Cherry-picking
# Apply specific commit to current branch
git cherry-pick commit-hash
# Cherry-pick without committing
git cherry-pick -n commit-hash
# Continue after resolving conflicts
git cherry-pick --continue
# Abort cherry-pick
git cherry-pick --abort
Maintenance and Cleanup
Repository Maintenance
# Check repository integrity
git fsck
# Cleanup unnecessary files
git clean -n # Dry run
git clean -f # Force clean
git clean -fd # Clean directories too
# Compress repository
git gc
# Optimize repository
git optimize-db
Finding and Fixing Issues
# Find broken references
git fsck --full
# Verify objects in database
git verify-pack -v .git/objects/pack/pack-*.idx
# Show corrupted objects
git fsck --lost-found
# Recover lost commits
git reflog expire --expire=now --all
git gc --prune=now
Tips for Better Git Usage
- Always check status before committing:
git status
- Write meaningful commit messages
- Commit early and often
- Use branches for new features
- Keep main/master branch stable
- Pull before pushing to avoid conflicts
- Use .gitignore for project-specific files
- Regularly prune old branches
- Back up important repositories
- Use SSH keys for authentication
Remember: This cheatsheet covers the most commonly used Git commands, but Git has many more features and options. Always consult the official Git documentation (git –help) for complete information about any command.
Tags: Git, Command Line, Version Control, Development Tools, Reference Guide