search by tags

for the user

adventures into the land of the command line

git cheatsheet

clone a repo

$ sudo git clone https://[email protected]/user/myrepo.git

sync your instance of the repo

$ git pull

discard any changes you’ve made in your version of the repo (from repo root directory)

$ git checkout .

add a local directory to a git repo

$ cd localdir
$ git init
$ git add .
$ git commit -m 'message'
$ git remote add origin https://[email protected]
$ git push -u origin +master

add files to a repo with git

$ git add *
$ git status
$ git commit -a -m "a groovy comment"
$ git push

refresh your git page and the files should be all present

remove a file from git source control, but dont delete it locally

$ sudo git rm --cached /path/to/file/


$ sudo git rm -r --cached /path/to/file/

add multiple directories at once

$ sudo git add hieradata manifests modules
$ git push

colourize git output

$ git config --global color.ui always

other options:


auto - colors output when it’s going straight to a terminal, but omits color when output is redirected to a pipe or a file
always - ignore the difference between terminals and pipes
false - color off

check your username

$ sudo git config

set your username

$ sudo git config something

check which branch you are on

$ sudo git branch -a

create a branch from the one you are currently on

$ sudo git checkout -b newbranch

switch back to your previous branch

$ sudo git checkout previousbranch

delete a local branch

$ sudo git branch -d somebranch

force delete a local branch in case of merge warning

$ sudo git branch -D somebranch

delete a remote branch

$ sudo git push origin --delete somebranch

push a branch to the remote repo, even when you are on a different branch

$ sudo git push origin branchname

compare changes in tracked file

$ sudo git diff /path/to/file

compare changes between two branches

$ sudo git diff branch1 branch2

discard changes to a file and revert it back to its latest commited version, the version from the HEAD in otherwords

$ sudo git checkout -- somefile

destroy any local changes that haven’t been committed yet

$ sudo git reset --hard c453051c777ebe9c9bcc3f3cf9c8e7f8397988fe

create a new commit which reverts the previous commit, sort of like rolling forward, the commit message will be “Reverting ‘message of previous commit’”

$ sudo git revert c453051c777ebe9c9bcc3f3cf9c8e7f8397988fe --no-edit

fixing an untracked remote branch if you see this

git pull
Username for 'https://blah':
Password for 'https://[email protected]':
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull remote branch

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/branch master

just do like git tells you

git branch --set-upstream-to=origin/master master

Branch master set up to track remote branch master from origin.

edit a local commit message before you push

git commit --amend -m "my new message"

check summary of changes between diffs of two branches

git diff --shortstat master bfdc5b5437e

stash changes you don’t want to commit yet so that you can change branches (maybe you forgot to change branches before editting a lot of stuff and want to move the changes to another existing branch)

git stash

recover the changes you just stashed (on the same or different branch)

git stash pop

pretty printing of your git history

git log --graph --decorate --oneline

i’ve come across this neat visualisation here created by Nina Jaeschke. its a pretty neat cheat sheet. (that rhymes)