Skip to content

Git cheat list

list of all affected files both tracked/untracked (for automation)

1
git status --porcelain

name of the current banch and nothing else (for automation)

1
git rev-parse --abbrev-ref HEAD

all commits that your branch have that are not yet in master

1
git log master..<HERE_COMES_YOUR_BRANCH_NAME>

setting up a character used for comments

1
git config core.commentchar <HERE_COMES_YOUR_COMMENT_CHAR>

fixing fatal: Could not parse object after unsuccessful revert

1
git revert --quit

view diff with inline changes (by lines)

1
git diff --word-diff=plain master

view diff of changes in a single line file (per char)

1
git diff --word-diff-regex=. master

view quick stat of a diff

1
2
3
git diff --shortstat master
git diff --numstat master
git diff --dirstat master

undo last just made commit

1
git reset HEAD~

list last 20 hashes in reverse

1
git log --format="%p..%h %cd %<(17)%an %s" --date=format:"%a %m/%d %H:%M" --reverse -n 20

list commits between dates

1
git log --format="%p..%h %cd %<(17)%an %s" --date=format:"%a %m/%d %H:%M" --reverse --after=2016-11-09T00:00:00-05:00 --before=2016-11-10T00:00:00-05:00

try a new output for diffing

1
2
git diff --compaction-heuristic ...
         --color-words ...

enable more thorough comparison

1
git config --global diff.algorithm patience

restoring a file from a certain commit relative to the latest

1
git checkout HEAD~<NUMBER> -- <RELATIVE_PATH_TO_FILE>

restoring a file from a certain commit relative to the given commit

1
git checkout <COMMIT_HASH>~<NUMBER> -- <RELATIVE_PATH_TO_FILE>

restoring a file from a certain commit

1
git checkout <COMMIT_HASH> -- <RELATIVE_PATH_TO_FILE>

creating a diff file from unstaged changes for a specific folder

1
git diff -- <RELATIVE_PATH_TO_FOLDER> changes.diff

applying a diff file, go to the root directory of your repository, run:

1
git apply changes.diff

show differences between last commit and currrent changes:

1
git difftool -d

referring to:

  • last commits ... HEAD~1 ...
  • last 3 commits ... HEAD~3 ...

show the history of changes of a file

1
git log -p -- ./Scripts/Libs/select2.js

ignoring whitespaces

1
git rebase --ignore-whitespace <BRANCH_NAME>

pulling for fast-forward only (eliminating a chance for unintended merging)

1
git pull --ff-only
  • list of all tags
1
2
git fetch
git tag -l

archive a branch using tags

1
2
git tag <TAG_NAME> <BRANCH_NAME>
git push origin --tags

you can delete your branch now

get a tagged branch

1
git checkout -b <BRANCH_NAME> <TAG_NAME>

list of all branches that haven’t been merged to master

1
git branch --no-merge master

enable more elaborate diff algorithm by default

1
git config --global diff.algorithm histogram

list of all developers

1
git shortlog -s -n -e

display graph of branches

1
git log --decorate --graph --all --date=relative

or

1
git log --decorate --graph --all --oneline 

remembering the password

1
2
git config --global credential.helper store
git fetch

the first command tells git to remember the credentials that you are going to provide for the second command

path to the global config

1
C:\Users\Bykov\.gitconfig

example of a global config

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
[user]
   email = *****
   name = Aleksey Bykov
   password = *****
[merge]
   tool = p4merge
[mergetool "p4merge"]
   cmd = p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
   path = \"C:/Program Files/Perforce\"
   trustExitCode = false
[push]
   default = simple
[diff]
   tool = meld
   compactionHeuristic = true
[difftool "p4merge"]
   cmd = p4merge.exe \"$LOCAL\" \"$REMOTE\"
   path = C:/Program Files/Perforce/p4merge.exe
[difftool "meld"]
   cmd = \"C:/Program Files (x86)/Meld/Meld.exe\" \"$LOCAL\" \"$REMOTE\"
   path = C:/Program Files (x86)/Meld/Meld.exe

viewing differences between current and other branch

1
git difftool -d BRANCH_NAME

viewing differences between current and stash

1
git difftool -d stash

viewing differences between several commits in a diff tool

1
git difftool -d HEAD@{2}...HEAD@{0}

view all global settings

1
git config --global -l

delete tag

1
2
git tag -d my-tag
git push origin :refs/tags/my-tag

pushing tags

1
git push --tags

checking the history of a file or a folder

1
git log -- <FILE_OR_FOLDER>

disabling the scroller

1
git --no-pager <...>

who pushed last which branch

1
git for-each-ref --format="%(committerdate) %09 %(refname) %09 %(authorname)"

deleting remote branch

1
git push origin :<BRANCH_NAME>

deleting remote branch localy

1
git branch -r -D <BRANCH_NAME>

or to sync with the remote

1
git fetch --all --prune

deleting local branch

1
git branch -d <BRANCH_NAME>

list actual remote branchs

1
git ls-remote --heads origin

list all remote (fetched) branches

1
git branch -r

list all local branches

1
git branch -l

find to which branch a given commit belongs

1
git branch --contains <COMMIT>

updating from a forked repository

1
2
3
git remote add upstream https://github.com/Microsoft/TypeScript.git
git fetch upstream
git rebase upstream/master

add gitignore, stop tracking some ignore files

1
2
git rm -r --cached .
git ci -m "update git ignore"

Github based Comments, sign in Github required.