Replace abc1234 with a commit ID, which you can find with git log --oneline. Branch, file and repository names are examples too.

Setup #

  • Check which version of Git is installedLinux, macOS, Windows
    git --version
  • Set the name on your commitsLinux, macOS, Windows
    git config --global user.name "Your Name"
  • Set the email on your commitsLinux, macOS, Windows
    git config --global user.email "[email protected]"
  • Name the first branch main in new repositoriesLinux, macOS, Windows
    git config --global init.defaultBranch main
  • Use VS Code to write commit messagesLinux, macOS, Windows
    git config --global core.editor "code --wait"
  • Rebase instead of merge when you pullLinux, macOS, Windows
    git config --global pull.rebase true
  • Create a shortcut, so git st runs git statusLinux, macOS, Windows
    git config --global alias.st status
  • Show all your settings and where they come fromLinux, macOS, Windows
    git config --list --show-origin

Start a repository #

  • Turn the current folder into a repositoryLinux, macOS, Windows
    git init
  • Copy a repository to your computerLinux, macOS, Windows
    git clone https://github.com/user/repo.git
  • Copy a repository into a folder with a different nameLinux, macOS, Windows
    git clone https://github.com/user/repo.git my-folder
  • Copy only the latest version of a repositoryLinux, macOS, Windows
    git clone --depth 1 https://github.com/user/repo.git
  • Copy a repository and switch to a specific branchLinux, macOS, Windows
    git clone --branch develop https://github.com/user/repo.git

See what has changed #

  • See what has changedLinux, macOS, Windows
    git status
  • See what has changed, in short formLinux, macOS, Windows
    git status -s
  • Show changes you haven't staged yetLinux, macOS, Windows
    git diff
  • Show changes that are staged for the next commitLinux, macOS, Windows
    git diff --staged
  • List only the names of changed filesLinux, macOS, Windows
    git diff --name-only
  • Compare two branchesLinux, macOS, Windows
    git diff main..my-branch
  • Show what a commit changedLinux, macOS, Windows
    git show abc1234
  • Show a file as it was in an earlier commitLinux, macOS, Windows
    git show abc1234:path/to/file.txt

Stage and commit #

  • Stage a file for the next commitLinux, macOS, Windows
    git add file.txt
  • Stage every change, including new and deleted filesLinux, macOS, Windows
    git add -A
  • Choose which parts of a file to stageLinux, macOS, Windows
    git add -p
  • Save staged changes as a commitLinux, macOS, Windows
    git commit -m "Describe the change"
  • Stage changes to tracked files and commit in one stepLinux, macOS, Windows
    git commit -am "Describe the change"
  • Add staged changes to the last commitLinux, macOS, Windows
    git commit --amend --no-edit

    Warning: Rewrites history. Don't use it on commits you've already pushed and shared.

  • Change the last commit messageLinux, macOS, Windows
    git commit --amend -m "New message"

    Warning: Rewrites history. Don't use it on commits you've already pushed and shared.

  • Make an empty commit, for example to rerun a workflowLinux, macOS, Windows
    git commit --allow-empty -m "Rerun checks"
  • Unstage a file but keep the editsLinux, macOS, Windows
    git restore --staged file.txt
  • Stop tracking a file but keep it on diskLinux, macOS, Windows
    git rm --cached file.txt
  • Delete a file and stage the deletionLinux, macOS, Windows
    git rm file.txt
  • Rename a file and stage the renameLinux, macOS, Windows
    git mv old-name.txt new-name.txt

Branches #

  • List your branchesLinux, macOS, Windows
    git branch
  • List every branch, including ones on GitHubLinux, macOS, Windows
    git branch -a
  • Show the latest commit on each branchLinux, macOS, Windows
    git branch -v
  • Create a branch and switch to itLinux, macOS, Windows
    git switch -c my-branch
  • Switch to an existing branchLinux, macOS, Windows
    git switch main
  • Switch back to the previous branchLinux, macOS, Windows
    git switch -
  • Rename the current branchLinux, macOS, Windows
    git branch -m new-name
  • List branches already merged into mainLinux, macOS, Windows
    git branch --merged main
  • Delete a branch that has been mergedLinux, macOS, Windows
    git branch -d my-branch
  • Delete a branch even if it isn't mergedLinux, macOS, Windows
    git branch -D my-branch

    Warning: Any commits only on that branch will be lost.

  • Delete a branch on GitHubLinux, macOS, Windows
    git push origin --delete my-branch

Merge and rebase #

  • Merge a branch into the current branchLinux, macOS, Windows
    git merge my-branch
  • Merge and always create a merge commitLinux, macOS, Windows
    git merge --no-ff my-branch
  • Combine a branch's changes into one commitLinux, macOS, Windows
    git merge --squash my-branch
  • List files with merge conflictsLinux, macOS, Windows
    git diff --name-only --diff-filter=U
  • Mark a conflict as fixedLinux, macOS, Windows
    git add file.txt
  • Cancel a merge that has conflictsLinux, macOS, Windows
    git merge --abort
  • Move your branch on top of the latest mainLinux, macOS, Windows
    git rebase main

    Warning: Rewrites history. Don't use it on commits you've already pushed and shared.

  • Continue a rebase after fixing conflictsLinux, macOS, Windows
    git rebase --continue
  • Cancel a rebaseLinux, macOS, Windows
    git rebase --abort
  • Tidy up or combine the last 3 commitsLinux, macOS, Windows
    git rebase -i HEAD~3

    Warning: Rewrites history. Don't use it on commits you've already pushed and shared.

  • Copy one commit onto the current branchLinux, macOS, Windows
    git cherry-pick abc1234

Work with GitHub #

  • List the remote repositories you're connected toLinux, macOS, Windows
    git remote -v
  • Connect a repository to GitHubLinux, macOS, Windows
    git remote add origin https://github.com/user/repo.git
  • Change the GitHub address of a repositoryLinux, macOS, Windows
    git remote set-url origin [email protected]:user/repo.git
  • Download new changes without applying themLinux, macOS, Windows
    git fetch
  • Download changes and forget branches deleted on GitHubLinux, macOS, Windows
    git fetch --prune
  • Get the latest changes for your branchLinux, macOS, Windows
    git pull
  • Get the latest changes and put yours on topLinux, macOS, Windows
    git pull --rebase
  • Push your commits to GitHubLinux, macOS, Windows
    git push
  • Push a new branch to GitHubLinux, macOS, Windows
    git push -u origin my-branch
  • Check out a branch someone else pushedLinux, macOS, Windows
    git switch their-branch
  • List commits on your branch that aren't on main yetLinux, macOS, Windows
    git log --oneline origin/main..HEAD
  • Overwrite the branch on GitHub with yoursLinux, macOS, Windows
    git push --force-with-lease

    Warning: Replaces commits on GitHub. Only use it on your own branches.

Undo changes #

  • Throw away changes to a fileLinux, macOS, Windows
    git restore file.txt

    Warning: Uncommitted changes to the file are lost.

  • Throw away all uncommitted changesLinux, macOS, Windows
    git restore .

    Warning: Uncommitted changes are lost. New files you haven't staged are kept.

  • Undo the last commit but keep the changesLinux, macOS, Windows
    git reset --soft HEAD~1
  • Undo the last commit and unstage its changesLinux, macOS, Windows
    git reset HEAD~1
  • Delete the last commit and its changesLinux, macOS, Windows
    git reset --hard HEAD~1

    Warning: The commit and any uncommitted changes are lost.

  • Make your branch match GitHub exactly (run git fetch first)Linux, macOS, Windows
    git reset --hard origin/main

    Warning: Local commits and uncommitted changes are lost.

  • Undo a commit you've already pushed, with a new commitLinux, macOS, Windows
    git revert abc1234
  • Bring back a file from an earlier commitLinux, macOS, Windows
    git restore --source abc1234 file.txt
  • Find a lost commit, for example after a resetLinux, macOS, Windows
    git reflog
  • Preview which untracked files would be deletedLinux, macOS, Windows
    git clean -n
  • Delete untracked files and foldersLinux, macOS, Windows
    git clean -fd

    Warning: Deletes files Git isn't tracking, and they can't be recovered. Run git clean -n first.

Stash #

  • Put uncommitted changes aside for laterLinux, macOS, Windows
    git stash
  • Put changes aside with a note, including new filesLinux, macOS, Windows
    git stash push -u -m "Half-finished login form"
  • List the changes you've put asideLinux, macOS, Windows
    git stash list
  • Show what the latest stash changesLinux, macOS, Windows
    git stash show -p
  • Bring back the latest stash and remove it from the listLinux, macOS, Windows
    git stash pop
  • Bring back the latest stash and keep it in the listLinux, macOS, Windows
    git stash apply
  • Delete the latest stashLinux, macOS, Windows
    git stash drop

    Warning: The stashed changes are lost.

  • Create a branch from the latest stashLinux, macOS, Windows
    git stash branch my-branch

History #

  • View history as a compact graphLinux, macOS, Windows
    git log --oneline --graph
  • View every branch as a graphLinux, macOS, Windows
    git log --oneline --graph --all
  • Show the last 5 commitsLinux, macOS, Windows
    git log -5 --oneline
  • Show every change to a file, including renamesLinux, macOS, Windows
    git log --follow -p file.txt
  • Find commits by words in their messageLinux, macOS, Windows
    git log --grep="login" --oneline
  • Find commits that added or removed some textLinux, macOS, Windows
    git log -S "apiKey" --oneline
  • Show commits by one personLinux, macOS, Windows
    git log --author="Gerard" --oneline
  • Show commits from the last two weeksLinux, macOS, Windows
    git log --since="2 weeks ago" --oneline
  • See who last changed each line of a fileLinux, macOS, Windows
    git blame file.txt
  • Count commits by each personLinux, macOS, Windows
    git shortlog -sn

Find a broken commit #

  • Start searching for the commit that broke somethingLinux, macOS, Windows
    git bisect start
  • Mark the current commit as brokenLinux, macOS, Windows
    git bisect bad
  • Mark a commit as workingLinux, macOS, Windows
    git bisect good abc1234
  • Finish searching and go back to where you wereLinux, macOS, Windows
    git bisect reset

Tags and releases #

  • List tagsLinux, macOS, Windows
    git tag
  • Tag the current commit as a releaseLinux, macOS, Windows
    git tag -a v1.0.0 -m "Version 1.0.0"
  • Tag an earlier commitLinux, macOS, Windows
    git tag -a v0.9.0 abc1234 -m "Version 0.9.0"
  • Push a tag to GitHubLinux, macOS, Windows
    git push origin v1.0.0
  • Push all your tags to GitHubLinux, macOS, Windows
    git push --tags
  • Delete a tag on your computerLinux, macOS, Windows
    git tag -d v1.0.0
  • Delete a tag on GitHubLinux, macOS, Windows
    git push origin --delete v1.0.0

Ignore files #

  • Ignore a folder by adding it to .gitignoreLinux, macOS
    echo "node_modules/" >> .gitignore
  • Find out why a file is being ignoredLinux, macOS, Windows
    git check-ignore -v file.txt
  • List ignored filesLinux, macOS, Windows
    git status --ignored

Worktrees and submodules #

  • Open another branch in a separate folderLinux, macOS, Windows
    git worktree add ../hotfix hotfix
  • List the folders open as worktreesLinux, macOS, Windows
    git worktree list
  • Remove a worktree folderLinux, macOS, Windows
    git worktree remove ../hotfix
  • Add another repository inside this oneLinux, macOS, Windows
    git submodule add https://github.com/user/library.git libs/library
  • Copy a repository and its submodulesLinux, macOS, Windows
    git clone --recurse-submodules https://github.com/user/repo.git
  • Download submodules after cloningLinux, macOS, Windows
    git submodule update --init --recursive