angelamisa's thinkery

Git Cheatsheet

Git Cheatsheet

#git Aug 9, 2014
  • # show git location
  • which git

# show version
git --version

# git help
git help [log|othercommands]

# git configuration
git config --[global|system|user] "Angela Misa"
git config --global ""
git config --global core.editor "editor"

# create git alias
git config --global alias.alias_name command
git config --global status

# make directory git repository
git init

# git navigation keys
fold result: [minus] + [shift] + [s] + [return]
next result: [spacebar]
forward: [f]
backward: [b]
quit: [q]

# show git logs
git log
git log [--oneline] [-$numOfLogs] [--all] [--graph] [--decorated] [--stat] [--summary]
git log --since="2014-07-11"
git log --until="2014-07-11"
git log --since="2 weeks ago" --until="3.days"
git log --author="Kevin"
git log --grep="$regexPattern"

# show git logs in between commits
git log sha101..sha102

# show git logs for file between commit SHA until recent
git log initSha.. index.html

# locally ignore files with created .gitignore file
*.log ; ignore all anyfile.log
log/*.log.[0-9] ; ignore directory Log files with format anyfile.log.$digit
assets/ ; ignore all inside dir/
!index.php ; don't ignore (!)file

# globally ignore files with created .gitignore_global file
git config --global core.excludesfile ~/.gitignore_global

# ignore file that is already added before it's set to be ignored
git rm --cached ignore.file

# show files to be committed, staged and unstaged

git status

# three status stages
Staged - Changes to be committed
Unstaged - Changes not staged for commit
Untracked - Untracked files

# checkout original/given commit copy of the modified files then replace staged copy
git checkout -- .
git checkout -- filename
git checkout sha10chars -- filename

# add all modified and untracked changes to commit stage
git add .

# add all changes to commit stage including removed files
git add --all

# unstage file
git reset HEAD filename

# remove file to repository
git rm filename

# rename file
git mv filename new_filename

# test-delete untracked files
git clean -n

# remove untracked files
git clean -f

# remove ignored untracked files too
git clean -x

# remove only ignored untracked files

git clean -X

# remove whole untracked directory
git clean -d

# push changes in commit staged
git commit -m "Comment Message"

# add changes to last commit HEAD and forced commit, comment can just be changed
git commit -am "Comment Message"
git commit --ammend -m "Comment Message"

# parent commit
HEAD^, sha10^, master^, HEAD~1, HEAD~

# grandparent commit
HEAD^^, sha10^^, master^^, HEAD~2

# show list of files in given pointer
git ls-tree [HEAD|SHA10|master]

# show all commit file difference
git show [--format=oneline] [HEAD|SHA10|master]

# show formatted difference result
git diff [--summary] [--stat] [--color-words] [--oneline] [pointer1..pointer2] [filename]

# show difference between staged and working directory
git diff [filename]

# show difference between staged and repository
git diff --staged [filename]

# show difference between branches
git diff branch1..branch3

# make all changes before pointed commit to staged
git reset --soft [SHA10|HEAD]

# make all changes before pointed commit to unstaged
git reset --mixed [SHA10|HEAD]

# force remove all changes before pointed commit
git reset --hard [SHA10|HEAD]

# revert resetting commit made
git revert [SHA10|HEAD]

# force revert branch

git branch -f master HEAD~3

# show all branches
git branch

# create branch
git branch branch_name

# create and switch to new branch
git branch -b branch_name

# switch to new branch

git checkout branch_name

# rename branch
git branch -m old_name new_name

# delete branch with reminder if it has changes to be committed
git branch -d branch_name

# force delete branch
git branch -D branch_name

# merge given branch to current branchgit merge branch_name

# treats merge of given branch to current branch as new commit
git merge --no-ff branch_name

# use git mergetool for conflicts
git mergetool --tool=tool_name

# abort merge conflict
git merge --abort
git reset --merge

# show all stashes
git stash list

# show status of stash in given index position
git stash show stash@{$index}

# show changes in stash of given index
git stash show -p stash@{$index}

# save to be committed files to current stash
git stash save "stashing changes save message"

# copy given stash to current branch
git stash apply stash@{$index}

# copy given stash to current branch and remove stash
git stash pop stash@{$index}

# remove given stash
git stash drop stash@{$index}

# remove all stashes
git stash clear


git tag -a v1.0 -m "Version 1"


git tag


git push origin v1.0


git push --tags


git tag -d v1.0


git push origin :v1.0


git tag -a v1.0 8bad64f6e9 -f