Git Cheatsheet

kuniga.me > Docs > Git Cheatsheet

Git Cheatsheet

Local Change management

Revert changes to uncommitted file

git checkout -- <file>

Discard the most recent local commit:

git reset --hard HEAD~1

Revert all local changes:

git checkout .

Remove uncommitted files:

# (optional) first, see what files will be purged
git clean -n
# purge
git clean -fd

Stash / Shelve single file:

git stash -- <file>

Un-stash:

git stash pop

(Local) Branch

Creates new branch foo:

git branch foo

Changes to branch foo:

git checkout foo

List existing branches:

git branch

Delete branch foo (move away from it first):

git branch -d foo

Remote Branch

Add new remote repo shorcut/alias. For example, I want public to refer to the repository at git@github.com:kunigami/my-repo.git.

git remote add public git@github.com:kunigami/my-repo.git

List remote repos associated to this local repo:

git remote -v

Send changes to a remote repo + branch, for example, the remote repository aliased as origin, branch test:

git push origin test

“Pull” remote branch (e.g. my_remote_branch) that does not exist in your local repo and switch to it. Note: this only works if my_remote_branch only appears in exactly one of the remote repos:

git switch my_remote_branch