Skip to content

git Cheatsheet


git gets easier once you get the basic idea that branches are homeomorphic endofunctors mapping submanifolds of a Hilbert space.

Basics #

  • git init: start a new repository.
  • git status: check current status.
  • git commit -am "<commit-message>: commit all changes in the working directory with a message added on.
  • git switch -c <branch-name>: create a branch and switch to it (git checkout -b will do the same, omit -c to just switch branches, use -t to set origin as upstream).
  • git merge [branch]: merge named branch into current branch.
  • git branch <branch-name> -d : delete branch if not tracked (-D to force, caution!).
  • git log --pretty=oneline: show a graph of commit history.
  • git remote -v : check origins.
  • git branch | grep -v "master" | xargs git branch -D : delete all local branches except master.
  • git add . : add all files of the current directory to track (including untracked).
  • git rev-list --count --since="Jan 1 2020" --before="Jan 1 2021" --all number of commits over timeframe.
  • git log --format='%aN' | sort -u : list all contributors by name.
  • git log --all --full-history -- "**/some-file.*": search git history for a specific file. Useful when something got deleted.
  • git fetch origin <your-branch>:<your-branch>: fetch a branch.
  • git grep <regexp> $(git rev-list --all): search git history for a specific string/regexp.

config #

  • git config --global color.ui auto: helpful colors.
  • git config --global user.name "vinckr": set user name globally for commits.
  • git config --global user.email "vinckr@example.com": set email globally for commits.

Troubleshooting #

Use the following with caution!

  • git revert <commit-id> : revert a commit that's already pushed.
  • git commit --amend -m "<new-commit-message>" : fix commit message (do not use if you already pushed your changes).
  • git rm --cached -r . : remove everything from git cache.
  • git reset --soft HEAD~N : undo last N commits, but keep changes locally.
  • git reset --hard HEAD~N : undo and delete last N commits.
  • git reset --hard origin/<branch-name> : undo all local commits to a branch.
  • add files to last commit (do not use if you already pushed your changes).
git add <file_name>
git commit --amend HEAD~1
  • delete local copy of branch and fetch upstream:
git switch master
git branch -d <branch-name>
git fetch origin <branch-name>
git checkout -b <branch-name> origin/<branch-name>

Further reference #