Git is the free and open source distributed version control system that's responsible for everything GitHub-related that happens locally on your computer. This comprehensive cheat sheet features the most important and commonly used Git commands for easy reference.
1. Getting Started & Configuration π
Installation & GUIs
With platform specific installers for Git, GitHub also provides the ease of staying up-to-date with the latest releases of the command line tool while providing a graphical user interface for day-to-day interaction, review, and repository synchronization.
Installation Setup
Configuring user information used across all local repositories:
# Check Git version
git --version
# Set username
git config --global user.name "[name]"
# Set email
git config --global user.email "[email]"
# List all configurations
git config --list
# Enable helpful colorization
git config --global color.ui auto
# Display help documentation
git help
Repository Initialization
# Initialize a new Git repository
git init
# Create repository in specific directory
git init [directory]
# Clone a repository
git clone [url]
# Clone into specific directory
git clone [url] [directory]
# Clone specific branch
git clone --branch [branch_name] [repository_url]
2. Basic Snapshotting πΈ
Adding Files
# Add file to staging
git add [file]
# Add all new and changed files
git add .
# Add all files (including deleted)
git add -A
# Interactive staging
git add -p
Committing Changes
# Commit with message
git commit -m "[descriptive message]"
# Add and commit tracked files
git commit -am "[message]"
# Modify most recent commit
git commit --amend
# Amend without changing message
git commit --amend --no-edit
# Create new note
git notes add
# Restore file to last commit
git restore <file>
Status & Differences
# Show modified files
git status
# Show ignored files
git status --ignored
# Show working directory changes
git diff
# Show staged changes
git diff --staged
git diff --cached
# Compare branches
git diff [branch1] [branch2]
# Unstage file
git reset [file]
# Compare with last commit
git diff HEAD
3. Branching and Merging π²
Branch Management
# List local branches
git branch
# List all branches
git branch -a
# Create new branch
git branch [branch-name]
# Delete branch locally
git branch -d [branch-name]
# Force delete branch
git branch -D [branch-name]
# Rename branch
git branch -m [old-name] [new-name]
# Switch to branch
git switch -c [branch-name]
Navigation
# Switch branches
git checkout [branch-name]
# Create and switch
git checkout -b [branch-name]
# Switch to last branch
git checkout -
# Check out commit
git checkout [commit-hash]
Merging
# Merge branch
git merge [branch]
# Abort merge
git merge --abort
# Create merge commit
git merge --no-ff [branch]
4. Remote Operations π
Remote Management
# List remote repositories
git remote -v
# Add remote
git remote add [name] [url]
# Remove remote
git remote remove [name]
# Delete file
git rm [file]
# Rename remote
git remote rename [old-name] [new-name]
Syncing
# Fetch branches
git fetch [remote]
# Remove obsolete branches
git fetch --prune
# Fetch and merge
git pull
# Fetch and rebase
git pull --rebase
# Push changes
git push [branch]
# Push and set upstream
git push -u origin [branch]
# Force push
git push --force
5. History and Comparison π
Logging
# Show commit history
git log
# One line format
git log --oneline
# Show as graph
git log --graph
# Show with stats
git log --stat -M
# Show file changes by author
git blame [file]
# Compare branch commits
git log branchB..branchA
# Show file history
git log --follow [file]
Inspection
# Show commit details
git show [commit]
# Show current commit hash
git rev-parse HEAD
# Show reference logs
git reflog
# Show object details
git show [SHA]
6. Undoing Changes β©οΈ
Working Directory
# Discard changes
git checkout -- [file]
# Preview cleanup
git clean -n
# Remove untracked files
git clean -f
# Remove untracked directories
git clean -fd
Staged Changes
# Unstage file
git reset [file]
# Unstage all
git reset
# Reset to commit
git reset --hard [commit]
Commits
# Undo commits
git reset [commit]
# Create undo commit
git revert [commit]
# Revert without commit
git revert --no-commit <commit>
# Soft reset
git reset --soft [commit]
# Hard reset
git reset --hard [commit]
7. Ignoring Patterns π«
# Common .gitignore patterns
logs/
*.notes
pattern*/
# Set global ignore file
git config --global core.excludesfile [file]
8. Advanced Operations π§
Stashing
# Save changes
git stash
# List stashes
git stash list
# Apply top stash
git stash pop
# Apply without removing
git stash apply
# Remove top stash
git stash drop
# Clear all stashes
git stash clear
Rebasing
# Rebase onto branch
git rebase [branch]
# Interactive rebase
git rebase -i HEAD~[n]
# Abort rebase
git rebase --abort
# Continue rebase
git rebase --continue
# Move file
git mv [existing-path] [new-path]
Tags
# List tags
git tag
# Create lightweight tag
git tag [tag-name]
# Create annotated tag
git tag -a [tag-name] -m "[message]"
# Push tag
git push origin [tag-name]
9. Maintenance and Data Recovery π οΈ
Maintenance
# Check integrity
git fsck
# Clean up repository
git gc
# Remove unreachable objects
git prune
# Verify packed objects
git verify-pack -v .git/objects/pack/pack-*.idx
Recovery
# Expire reflog
git reflog expire --expire=now --all
# Recover commit
git checkout [lost-commit-hash]
# Cherry-pick commit
git cherry-pick [commit]
10. GitHub Specific Commands π
Pull Requests
# Fetch PR
git fetch origin pull/[PR-number]/head:[branch-name]
# Check out PR
git checkout pr/[PR-number]
# Push to PR
git push origin [branch]:[PR-branch]
GitHub CLI
# Create repository
gh repo create
# Create pull request
gh pr create
# Create issue
gh issue create
# Fork repository
gh repo fork
11. Configuration and Info βοΈ
Help and Information
# Get command help
git help [command]
# Alternative help
git [command] --help
# Quick help
git -h
Advanced Configuration
# Set default editor
git config --global core.editor "[editor]"
# Create alias
git config --global alias.[shortcut] "[command]"
# Configure line endings
git config --global core.autocrlf true
# Cache credentials
git config --global credential.helper cache
# Set default branch
git config --global init.defaultBranch main
π Glossary
- Git: An open-source, distributed version control system that tracks changes in code.
- GitHub: A web-based platform for hosting, sharing, and collaborating on Git repositories.
- Commit: A snapshot of the entire repository at a given point in time, identified by a unique SHA (hash).
- Branch: A movable pointer to a commit, allowing parallel development without affecting the main codebase.
- Clone: A local copy of a Git repository, including all commits, branches, and history.
- Remote: A shared repository (typically on GitHub) that team members use to push and pull changes.
- Fork: A personal copy of someone else's repository on GitHub, allowing independent modifications.
- Pull Request (PR): A request to merge changes from one branch into another, often reviewed with comments, tests, and approvals.
- HEAD: The current reference point in a Git repository, typically pointing to the latest commit on the active branch.
π Resources for Further Learning
Official Resources
Educational Platforms
- FreeCodeCamp's Git Cheat Sheet
- Codecademy's Git Cheat Sheet
- GeeksforGeeks Git Reference
- GeeksforGeeks GitHub Commands
Additional References
π€ Contributing
Found this helpful?
Consider sharing it with your network!
If you spot any errors or have suggestions for improvement, feel free to leave a comment below.
Happy coding! π
Top comments (0)