Git Commands Cheat Sheet

Copy-ready commands for everyday Git work. Replace the words in <angle brackets> with your own values.

Set up and start

CommandWhat it does
git config --global user.name "Your Name"Sets the name on your commits
git config --global user.email "you@example.com"Sets the email on your commits
git initTurns the current folder into a repository
git clone <url>Copies a remote repository to your computer

Save changes

CommandWhat it does
git statusShows changed, staged and untracked files
git diffShows unstaged changes line by line
git add <file>Stages one file for the next commit
git add .Stages every change in the current folder
git commit -m "message"Saves the staged changes as a commit
git log --onelineShows a short history of commits

Branches

CommandWhat it does
git branchLists local branches
git switch -c <name>Creates a branch and moves to it
git switch <name>Moves to an existing branch
git merge <name>Merges that branch into the current one
git branch -d <name>Deletes a merged branch

Sync with a remote

CommandWhat it does
git fetchDownloads new commits without changing your files
git pullFetches and merges the remote changes
git pushUploads your commits
git push -u origin <branch>Pushes a new branch and remembers it

Undo and fix

CommandWhat it does
git restore <file>Discards unstaged changes in a file (cannot be undone)
git restore --staged <file>Unstages a file but keeps the changes
git commit --amendEdits the last commit message or adds to it
git revert <commit>Makes a new commit that undoes an earlier one
git stashSets your changes aside temporarily
git stash popBrings the stashed changes back

Be careful with commands that rewrite history, such as git reset --hard and git push --force. They can destroy work, especially on branches other people use.

↑ ↓ navigateEnter openEsc close