Cheat Sheet: Git

An overview of commands that can be useful when working with Git.
July 30, 2018

Visual Studio (both Code and 2017) has a nice set of tools to work with Git, but when it comes to more complex source control magic, I still prefer to roll my sleeves up and dive into the console. Here is my cheat sheet with commands that are not used that often, but come handy from time to time.

I based this cheat sheet on a great article written by Michael Kohl. Go check it out, if you’re interested in explanations and different variants of these commands.

Discard local changes:

git checkout -- <path>

Undo local commits:

git reset HEAD~2 # undo last 2 commits

Remove file from Git and file system:

git rm <filename>

Remove file from Git only:

git reset <filename>


git remove --cached <filename>

Change commit message of last commit:

git commit --amend -m "Changed message"

Add forgotten file to commit:

git add <filename>
git commit --amend

Sync fork with upstream:

git remote -v # check
git remote add upstream<org>/<repo>.git
git fetch upstream
git checkout master
git merge upstream/master
git push
comments powered by Disqus