Jump to content

Git in Cheatsheet

From Wikibase

Languages: English · français · Esperanto

Quick reference for smart people — part of our dev cheatsheets collection.

Git is a distributed version control system that tracks changes to files and coordinates work between people on the same codebase.

New to Git? Learn Git Branching is a visual, interactive tutorial; Pro Git is the free, thorough book.

Basic example: clone, edit, commit, push

git clone https://github.com/user/repo.git
cd repo
git switch -c my-feature          # branch off main
# ... edit files ...
git add .                         # stage all changes
git commit -m "Add my feature"    # snapshot them
git push -u origin my-feature     # share on the remote, remember upstream

Every change follows the same loop: edit → stage (git add) → commit → push. The working tree is what you edit, the index is what you stage, the remote is where you share.

Setup

git config --global user.name "Ada Lovelace"
git config --global user.email [email protected]
git config --global init.defaultBranch main
git config --global alias.co checkout     # `git co` works from now on
git config --global pull.rebase true      # pull = fetch + rebase
Command Effect
git config --global user.name "Ada" who you are, for every repo — needed before the first commit
git config --global init.defaultBranch main new repos default to main
git config --global alias.co checkout create a short alias
git config --global --list show all config

Start a repository:

git init                # new repo in the current directory
git init my-project     # ... in a subdirectory
git clone <url>         # copy an existing repository
git clone <url> dir     # ... into a given directory

Daily workflow

git status                  # what changed?
git diff                    # unstaged changes
git diff --staged           # staged changes
git add file.py             # stage one file
git add .                   # stage all changes
git add -p                  # stage hunks interactively
git commit -m "Fix typo"    # commit the staged changes
git commit -am "Fix typo"   # stage tracked changes + commit in one go

git commit -am only stages tracked files. A brand-new file is untracked and still needs an explicit git add.

Inspecting history

git log --oneline                   # one line per commit
git log --oneline --graph --all     # branch topology
git log -p                          # full diffs
git log --follow -- file.py         # history of a single file
git show <hash>                     # one commit in detail
git blame file.py                   # who changed each line

Branching & merging

git branch                  # list local branches
git branch -a               # include remote-tracking branches
git branch feature-branch   # create a copy of current branch named feature-branch and switch to it
git branch main-backup main # create a copy of main named main-backup and switch to it
git switch -c feature-branch  # = branch feature-branch + switch to it
git switch -c main-backup main # = git branch main-backup main + switch to it
git switch feature          # switch to an existing branch
git switch -                # switch back to the previous branch
git merge feature           # merge feature into the current branch
git branch -d feature       # delete a merged branch
git branch -D feature       # force-delete an unmerged branch

Git before 2.23 has no git switch / git restore. Use git checkout -b feature to branch, git checkout feature to switch, and git checkout -- file.py to discard changes.

Rebase

First create a backup of the branch to be rebased:

git branch main-backup main # main-backup is an exact copy of main
git switch main # switches back to target the main branch
git rebase main          # replays current branch on top of main
git rebase -i HEAD~3     # interactively reorganises the last 3 commits on current branch

Merge conflicts

# git status shows "both modified"; files contain markers:
#   <<<<<<< HEAD  ...  =======  ...  >>>>>>> feature
# edit the file to keep the right lines, then:
git add <resolved files>
git commit              # after a merge
git rebase --continue   # after a rebase
git merge --abort       # back out of the merge entirely
git rebase --abort      # ... or of the rebase

Remotes

git remote -v                    # list remotes
git remote add <remote name:e.g., origin> <url>      # connect a remote
git fetch <remote name>               # download commits, don't touch your work
git pull                         # fetch + merge
git push                         # upload your commits
git push -u origin feature       # first push of a branch: set upstream
git push origin --delete feature # delete a branch on the remote
git tag v1.0                     # tag the current commit
git push origin v1.0             # share the tag

pull is fetch + merge. It can create merge commits. Prefer a linear history with git pull --rebase, or set it once with git config --global pull.rebase true.

Undoing

git restore file.py                    # discard unstaged changes to a file
git restore --staged file.py           # unstage a file
git commit --amend                     # fix the last commit (message / forgotten file)
git reset HEAD~1                       # undo the last commit, unstage the changes
git reset --soft HEAD~1                # undo the last commit, keep changes staged
git reset --hard HEAD~1                # undo the last commit and drop everything
git revert <hash>                      # undo a commit that is already pushed

git reset --hard can be destructive: it trashes the resetted commit and uncommitted edits.

However, partial recovery immediately after accidental trigger is possible.

git reflog              # lists previous HEAD locations

Expected output:

abc1234 HEAD@{0}: reset: moving to HEAD~3
def5678 HEAD@{1}: commit: Latest work you want back

Recover committed changes

git reset --hard "HEAD@{1}" # reset once more to the desired commit just before reset.

Attempt to recover staged changes:

git fsck --unreachable      # search for dangling tip objects (often leftovers from reset --hard)

Changes to untracked files are normally kept on disk. Unstaged changes to tracked files are permanently lost.

amend rewrites history (new commit hashes). Normally, it should only be used on local commits that are not yet shared.

Stash

Put uncommitted work aside to switch branches, then bring it back. Stashes are not tied to a branch — a stack you can pop anywhere.

git stash              # stash tracked changes
git stash -u           # also stash untracked files
git stash -m "WIP"     # stash with a message
git stash list         # see what is stashed
git stash show -p      # inspect the newest stash's diff

pop vs apply — pop restores and drops the stash; apply restores and keeps it:

git stash pop              # restore newest stash, then drop it
git stash apply            # restore, keep the stash
git stash apply stash@{1}  # restore a specific stash
git stash drop stash@{1}   # delete a specific stash
git stash clear            # delete all stashes
git stash branch fix       # new branch at the stash point, pop onto it

Untracked files are left behind by default. git stash only stashes tracked changes — a brand-new file survives in the working tree. Add -u for untracked files too, or -a to also include ignored files.

A conflicting pop keeps the stash. Restoring onto a branch that changed the same lines leaves the stash in place: resolve like a merge conflict, then git stash drop once the work is safely committed.

Further reading