Git
Git cheat sheet: configuration, aliases, tokens, cloning, branches, commits, tags, submodules, remotes, undoing changes and merge strategies.
8 min readUpdated
Git is a distributed version control system. This note collects the commands needed day to day and the ones that are easy to forget: configuration and aliases, authentication with tokens, branch and tag housekeeping, rewriting and undoing commits, working with forks, and the four common ways to merge a pull request.
Commands use main as the default branch name; replace it if your repository uses another.
Everyday commands
The basic cycle of staging, committing and inspecting changes:
git init # create a new repository in the current directorygit add <path> # stage filesgit commit # commit staged filesgit diff # show unstaged changesgit diff --cached # show staged changesgit log # show commit historygit merge <branch> # merge a branch into the current oneConfiguration
Create an alias
An alias is a shortcut for a long command. This defines git loagd for git log --oneline --all --graph --decorate:
git config --global alias.loagd "log --oneline --all --graph --decorate"The scope option chooses which config file is written: --global (your user), --system (all users), --local (the repository, the default), --worktree or --file <path>. --blob reads config from a blob and cannot be written to.
List, locate and inspect configuration:
# List all global configgit config --global --list
# Show which file defines a keygit config --show-origin alias.loagd
# Show the value of a keygit config --get alias.loagdAuthenticate with a personal access token
GitHub does not accept account passwords for Git over HTTPS; use a personal access token (PAT) instead. GitLab requires one over HTTPS when two-factor authentication is enabled. Create the token in your account settings (see References), then enter it when Git asks for a password:
$ git clone https://github.com/user-or-organisation/myrepo.gitUsername: <my-username>Password: <my-personal-access-token>A credential helper, or SSH keys instead of HTTPS, saves you from typing the token every time.
Sign off your commits
Some projects require a Signed-off-by: line in each commit message, certifying that you have the right to submit the change (the Developer Certificate of Origin). --signoff (short form -s) adds it using your configured name and email:
git commit --signoff --message "This is my commit message"git commit -s -m "This is my commit message"Sign-off is not a cryptographic signature; that is git commit -S with a GPG or SSH key.
Cloning
Clone a specific branch
git clone -b <branch> <remote_repo>Clone including submodules
git clone --recurse-submodules -j8 <remote_repo>-j8 fetches up to eight submodules in parallel.
Branches
Git 2.23 added git switch for changing branches; git checkout still works and appears in older documentation. Both forms are shown below.
List branches
git branch # local branchesgit branch -a # all branches (local and remote-tracking)Create a branch
git branch <branch-name> # from the current commitgit branch <branch-name> <sha1-of-commit> # from a specific commitgit switch -c <branch-name> # create and switchgit checkout -b <branch-name> # create and switch (older form)Switch branches
git switch <branch-name>git checkout <branch-name>Push a branch to the remote
git push -u origin <branch-name>-u sets the remote branch as upstream, so later git push and git pull need no arguments.
Move uncommitted changes to a new branch
Creating a branch keeps the working tree as it is, so uncommitted changes come along to the new branch:
git switch -c <new-branch>git statusRename a branch
# Rename the current branchgit branch -m <new-name>
# Rename another branchgit branch -m <old-name> <new-name>
# Delete the old name on the remote and push the new onegit push origin :<old-name> <new-name>git push origin -u <new-name>To rename the default branch, do the same, then change the default branch in the GitHub or GitLab project settings before deleting the old name on the remote.
Delete a branch
# Delete locally (refuses if the branch is not merged; -D forces)git branch -d <branch-name>
# Delete on the remotegit push origin --delete <branch-name># or shortergit push origin :<branch-name>Prune stale remote-tracking branches
Remove remote-tracking branches (origin/...) whose branch no longer exists on the remote:
# Preview what will be removedgit remote prune origin --dry-run
# Prunegit remote prune originCommits
Amend the latest commit message
git commit --amend -m "New message"git push --force-with-lease origin <branch-name>The push is only needed if the commit was already pushed. --force-with-lease refuses to overwrite the remote branch if someone else pushed to it in the meantime, unlike plain --force.
Remove an unpushed commit and keep its changes
git reset --soft HEAD~The commit is removed and its changes stay staged.
Remove a pushed commit
On a shared branch, prefer git revert <commit-hash>: it adds a new commit that undoes the change without rewriting history.
To remove commits from history, find the last commit you want to keep:
git logThen either force push that commit as the new tip of the remote branch:
git push origin +<commit-hash>:mainor reset the local branch to it and force push:
git reset --hard <commit-hash>git push --force-with-lease origin <branch-name>Everyone else working on the branch must then reset their copy:
git fetchgit reset --hard origin/<branch-name>Check out a specific commit
git switch --detach <commit-hash>git checkout <commit-hash> # older form
# Or create a branch starting at that commitgit switch -c <new-branch> <commit-hash>Tags
Rename a tag
git tag <new> <old>git tag -d <old>git push origin :refs/tags/<old>git push origin <new>git push --tags also works for the last step but pushes every local tag.
Delete a tag
git push origin :refs/tags/<tag-name>git tag --delete <tag-name>Submodules
Delete a submodule
Unregister the submodule, which removes its entry from .git/config:
git submodule deinit -f path/to/submoduleRemove its stored repository from .git/modules:
rm -rf .git/modules/path/to/submoduleRemove its entry from .gitmodules and the submodule directory:
git rm -f path/to/submoduleRemotes and forks
View remote URLs
git remote -vFetch everything
Fetch all remotes and tags, and prune deleted branches:
git fetch --all --tags --prunePull with rebase
Rebase your local commits on top of the latest remote changes instead of creating a merge commit:
git pull --rebaseFork and pull request workflow
- Fork the repository and clone your fork.
- Add the original repository as the
upstreamremote. - Bring your
mainup to date withupstream. - Create a topic branch, commit, push it to your fork and open a pull request.
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.gitgit fetch upstreamgit switch maingit merge upstream/maingit push origin maingit switch -c ISSUE1# ... make changes ...git commit -am "Fix for ISSUE1"git push -u origin ISSUE1# Open a pull request from ISSUE1 to upstream mainRepeat the fetch and merge steps to keep the fork in sync later.
Undoing changes
Discard uncommitted changes
git reset --hard discards all staged and unstaged changes to tracked files. git clean -fd deletes untracked files and directories. Neither can be undone.
git reset --hardgit clean -fdgit clean -nd shows what would be deleted without deleting it.
Reset the local branch to match the remote
Save your current state on a branch first, in case you need it again:
git commit -a -m "Saving my work, just in case"git branch my-saved-workThen reset:
git fetch origingit reset --hard origin/mainMerge strategies
A pull request can be integrated in four common ways. They differ in what the history of main looks like afterwards.
Merge commit (git merge --no-ff) always creates a merge commit, even when a fast-forward is possible, so the branch stays visible in the history.

A typical sequence that commits on development and merges it into main with a merge commit:
git add .git commit -m "Some message"git push origin development
git switch maingit pull origin maingit merge --no-ff developmentgit push origin main
git switch developmentFast-forward merge moves main forward to the branch tip without a new commit. It is only possible when main has no commits that the branch lacks. git merge fast-forwards by default when it can; --ff-only refuses to merge otherwise.

Rebase replays the branch’s commits on top of the current main (git rebase main on the branch), giving a linear history. The replayed commits get new hashes.

Squash merge (git merge --squash <branch> followed by git commit) combines all changes of the branch into one new commit on main. The individual commits of the branch do not appear in main’s history.

Inspection
View commit history
git loggit log --oneline --all --graph --decorateShow the current commit hash
git rev-parse --short HEADgit log --pretty=format:'%h' -n 1List the 10 largest files
List every file in HEAD with its size, sort by size and show the last ten:
git ls-tree -r -t -l --full-name HEAD | sort -n -k 4 | tail -n 10References
- Git reference documentation: git-commit —signoff
- Git reference documentation: git-config
- Git reference documentation: git-merge
- Git reference documentation: git-submodule
- Pro Git: Rebasing
- GitHub Docs: Managing your personal access tokens
- GitLab Docs: Personal access tokens
- Sign off your commits (video)
- Dangit, Git!?! Fixes for common mistakes