Git Commands Cheatsheet

Essential commands for version control with Git

Quick Reference

Git is a distributed version control system for tracking changes in source code.

1 Basics

git init
Initialize a new Git repository
git init # Initialize in current directory
git init project-name # Create new directory and initialize
git clone [url]
Clone a repository into a new directory
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-project # Clone with custom directory name
git status
Show the working tree status
git status
git status -s # Short format
git add [file]
Add file contents to the index
git add file.txt # Add specific file
git add . # Add all files
git add *.js # Add all JavaScript files
git commit
Record changes to the repository
git commit -m "Commit message"
git commit -am "Commit message" # Add and commit tracked files
git commit --amend # Modify last commit
git diff
Show changes between commits, commit and working tree, etc.
git diff # Unstaged changes
git diff --staged # Staged changes
git diff HEAD~1 # Compare with previous commit
git rm [file]
Remove files from the working tree and from the index
git rm file.txt # Remove file
git rm --cached file.txt # Remove from index but keep locally
git mv [old] [new]
Move or rename a file, a directory, or a symlink
git mv oldname.txt newname.txt

2 Branching

git branch
List, create, or delete branches
git branch # List local branches
git branch -a # List all branches (local and remote)
git branch new-branch # Create new branch
git branch -d branch-name # Delete branch
git checkout [branch]
Switch branches or restore working tree files
git checkout main # Switch to main branch
git checkout -b new-branch # Create and switch to new branch
git checkout file.txt # Discard changes to file
git switch [branch]
Switch branches (Git 2.23+)
git switch main # Switch to main branch
git switch -c new-branch # Create and switch to new branch
git merge [branch]
Join two or more development histories together
git merge feature-branch # Merge feature-branch into current branch
git merge --no-ff feature-branch # Merge with explicit merge commit
git rebase [branch]
Reapply commits on top of another base tip
git rebase main # Rebase current branch onto main
git rebase -i HEAD~3 # Interactive rebase of last 3 commits
git cherry-pick [commit]
Apply the changes introduced by some existing commits
git cherry-pick abc1234 # Apply changes from commit abc1234
git cherry-pick abc1234..def5678 # Apply range of commits

3 Remote Repositories

git remote
Manage set of tracked repositories
git remote -v # List remotes with URLs
git remote add origin https://github.com/user/repo.git # Add remote
git remote remove origin # Remove remote
git fetch [remote]
Download objects and refs from another repository
git fetch # Fetch from default remote
git fetch origin # Fetch from specific remote
git fetch --prune # Remove remote-tracking branches that no longer exist
git pull [remote] [branch]
Fetch from and integrate with another repository or local branch
git pull # Pull from default remote
git pull origin main # Pull specific branch
git pull --rebase # Pull with rebase instead of merge
git push [remote] [branch]
Update remote refs along with associated objects
git push # Push to default remote
git push origin main # Push specific branch
git push -u origin main # Push and set upstream
git push --force # Force push (use with caution!)
git remote show [remote]
Gives some information about the remote
git remote show origin
git remote update
Fetch updates for all remotes
git remote update # Fetch all remotes
git remote update --prune # Prune remote-tracking branches
git submodule
Initialize, update or inspect submodules
git submodule add https://github.com/user/repo.git path/to/submodule
git submodule update --init --recursive # Initialize and update submodules

4 Stashing

git stash
Stash the changes in a dirty working directory away
git stash # Stash changes
git stash push -m "message" # Stash with message
git stash -u # Include untracked files
git stash list
List stashed changes
git stash list
git stash pop
Remove and apply a single stashed state
git stash pop # Apply most recent stash
git stash pop stash@{1} # Apply specific stash
git stash drop
Remove a single stashed state
git stash drop # Drop most recent stash
git stash drop stash@{1} # Drop specific stash

5 History

git log
Show commit logs
git log # Show commit history
git log -p # Show diffs
git log --oneline # Compact format
git log --graph # ASCII graph of branch history
git show [commit]
Show various types of objects
git show abc1234 # Show commit details
git show HEAD~1:file.txt # Show file content from previous commit
git blame [file]
Show what revision and author last modified each line of a file
git blame file.txt
git blame -L 10,20 file.txt # Limit to lines 10-20
git bisect
Use binary search to find the commit that introduced a bug
git bisect start
git bisect bad # Current version is bad
git bisect good abc1234 # Commit abc1234 is known to be good
git bisect reset # End bisect session
git reflog
Manage reflog information
git reflog # Show reference logs
git reflog show main # Show reflog for main branch
git reset [commit]
Reset current HEAD to the specified state
git reset --soft HEAD~1 # Undo commit but keep changes staged
git reset --mixed HEAD~1 # Undo commit and unstage changes (default)
git reset --hard HEAD~1 # Discard commit and all changes (careful!)

6 Configuration

git config
Get and set repository or global options
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global core.editor "code --wait"
git config --list # List all configurations
git alias
Create shortcuts for commands
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git ignore
Manage .gitignore file
echo "*.log" >> .gitignore # Add pattern to ignore
git check-ignore -v file.log # Check why file is ignored
git attributes
Define attributes per path
echo "*.ps1 text eol=lf" >> .gitattributes # Force LF line endings for PowerShell files
git hooks
Client-side or server-side scripts that run on specific Git events
# See .git/hooks/ directory for examples
chmod +x .git/hooks/pre-commit # Make hook executable

7 Advanced

git filter-branch
Rewrite branches (use with caution!)
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD # Remove file from history
git gc
Cleanup unnecessary files and optimize the local repository
git gc # Run garbage collection
git gc --aggressive # More thorough cleanup
git fsck
Verify the connectivity and validity of objects in the database
git fsck # Check repository integrity
git worktree
Manage multiple working trees
git worktree add ../feature-branch feature-branch # Add new worktree
git worktree list # List worktrees
git bundle
Move objects and refs by archive
git bundle create repo.bundle HEAD main # Create bundle
git clone repo.bundle # Clone from bundle
git replace
Create, list, delete refs to replace objects
git replace bad-commit good-commit # Replace commit without rewriting history