Cheat Sheet: Git
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 started this cheat sheet based 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>

Or:

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 https://github.com/<org>/<repo>.git
git fetch upstream
git checkout master
git merge upstream/master
git push

Create annotated tag:

git tag -a v0.9.0 -m "Message for version 0.9.0"
git push origin v0.9.0

Tag later:

git log --pretty=oneline
git tag -a v1.2 9fceb02

Clone specific branch/tag

git clone <repo-url> --branch=work # branch
git clone <repo-url> --branch=0.22 # tag

Clone into specific folder

git clone <repo-url> <path>

Found something inaccurate or plain wrong? Was this content helpful to you? Let me know!

šŸ“§ codez@deedx.cz