Cheat sheet · Tools
Git cheat sheet
The Git commands you use every day: save work, branch, merge, share and undo mistakes safely.
- Sections
- 6
- Items
- 33
- Area
- Tools
- Download
- PNG, PDF
Cheat sheet
Git
The Git commands you use every day: save work, branch, merge, share and undo mistakes safely.
Start
git init- Make the current folder a Git repository
git clone <url>- Copy a remote repository to your computer
git config --global user.name "Your Name"- Set the name on your commits
git config --global user.email "you@example.com"- Set the email on your commits
git status- Show changed, staged and untracked files
Save your work
git add <file>- Stage one file for the next commit
git add -p- Choose which parts of a file to stage
git commit -m "message"- Save the staged changes as a commit
git commit --amend- Change the last commit (message or content)
git diff- Show changes that are not staged yet
git diff --staged- Show changes that are staged
Branches
git branch- List local branches
git switch -c <name>- Create a branch and move to it
git switch <name>- Move to an existing branch
git merge <name>- Bring another branch into the current one
git rebase main- Replay your commits on top of main
git branch -d <name>- Delete a branch that is already merged
Look at history
git log --oneline --graph- Short history with the branch shape
git show <commit>- Show one commit and its changes
git blame <file>- See who last changed each line
git reflog- Every place HEAD has been: a safety net
Share
git remote -v- List the remotes and their addresses
git fetch- Download new commits without changing your files
git pull- Fetch, then merge into your current branch
git push -u origin <branch>- Upload a branch and remember it
git push --force-with-lease- Safer force push: stops if someone else pushed
Undo
git restore <file>- Throw away changes to a file
git restore --staged <file>- Unstage a file but keep its changes
git revert <commit>- Make a new commit that cancels an old one
git reset --soft HEAD~1- Undo the last commit, keep changes staged
git reset --hard HEAD~1- Undo the last commit and delete its changes
git stash / git stash pop- Put changes aside, then bring them back
git cherry-pick <commit>- Copy one commit onto the current branch
Remember
- git reset --hard and a force push destroy work. If unsure, use git revert or make a backup branch first.
- Commit small and often, with a message that says why, not only what.
- Lost a commit? git reflog usually still has it for a few weeks.
Learn it properly
A cheat sheet helps you remember. These lessons explain the ideas step by step, with examples.