Skip to content

Instantly share code, notes, and snippets.

@nicholashagen
Created March 7, 2012 15:22
Show Gist options
  • Save nicholashagen/1993769 to your computer and use it in GitHub Desktop.
Save nicholashagen/1993769 to your computer and use it in GitHub Desktop.
Git Commands
# control color output via ASCII codes for better visual aspsect in terminal
git config --global color.ui auto
# update line-ending support for win/unix cross-platform (cr/cflf)
# force to be LF in the repo (regardless of OS)
git config --global core.autocrlf input
# force windows t oconvert to platform on checkout and back on commit
git config --global core.autocflf true
# individually control portions of a file (patch mode)
# - then use 's' to split the full file hunk into smaller hunks to individually control one by one
# - then git status will report file in both
git add -p myfile.txt
# diff'ing from working dir to staged
git diff
# diff'ing from staging to committed to repo
git diff --staged
# diff'ing from working/staged to last committed point in repo
git diff HEAD
# git log w/ stats (file changes, etc)
git log --stat
# git log w/ diff output
git log -p
# only show last X commits
git log -5
# only show last 10 mins
git log --since=10.minutes.ago
# only show based on particular author (only portion of name needed)
git log --author=nicholas -10
# show all output on single line
git log --pretty=oneline -2
# graphical view of the log
gitk --all
# show git branches with stored commit info
# stored in .git/refs/heads/
git branch -vv
# use mergetool to manage merges
git mergetool -t <path-of-tool>
# create git-based aliases
git config --global alias.s "status -u -s"
git s
git config --global alias.lol 'log --pretty=oneline --graph --abbrev-commit --all'
git lol -2
# rev parsing
git rev-parse HEAD
rev rev-parse AB23EF
# rev parsing (1 commit before)
git rev-parse HEAD^
# rev parsing (2 commits before)
git rev-parse fea23c^^
# rev parsing 5 commits before HEAD
git rev-parse HEAD~5
# stash branch updates w/o committing to checkout other branches
... update some files ...
git stash
git status
git checkout master
# show stashes (WIP = work in progress)
git stash list
# merge a stash into working dir again, but keep it stashed
git stash apply
# or merge a stash and remove it
# you can even have later editted those files, committed it, and then re-merge the stash
# ie: working on new feature, stash and fix a bug in that file and commit, pop the stash to merge
git stash pop
# revert a single file (restore from a previous commit)
# the -- delimits the start of files from the command
git checkout HEAD -- file1.txt
# stash the contents under a specific message rather than default
git stash save "message"
# show the contents of a particular stash
git show stash@{0}
# git bisect to step through revisions to find where a broken commit occured
# as long as the 'run' command returns proper response codes (0, 1, etc)
# then branch/revert/merge etc at that commit that caused a good to bad response
git bisect start
git bisect good HEAD~14
git bisect bad master
git bisect run mvn test
git bisect log
git bisect visualize
gitk --all [visual tool]
git bisect reset
git revert [revision of bad]
[merge as necessary]
git commit
# fetch just remote repository info (does not merge or update your working directory)
git fetch
git log --pretty=oneline origin/branch
# pull the repository info and merge the active branch based on those states
git pull
# branching (local branches vs remote branches)
git branch
git branch -r
git branch -a
# create a tracking branch
git branch branchname
git checkout branchname
git push -u origin branchname
# diff between local branch and remote branch
git log origin/snicholashagen..snicholashagen
git diff origin/snicholashagen..snicholashagen
# fetch and merge
git fetch
git checkout branchname
git merge origin/branchname
# merge w/o committing
git merge --no-commit origin/branch
git diff --staged
git merge --abort or git commit
git reset --hard HEAD
# checkout a remote branch locally
# Git automatically associates and tracks the remote branch of same name
git checkout remotebranch
# add remote
git remote add bangalore url
git fetch bangalore
git push bangalore
# checkout branch and track to a specific branch when on multiple remotes
git checkout branch --track origin/branch
# purge remote branches no longer in upstream
git remote prune <remotename>
# deleting branches (only local)
git branch -d branch
git branch -D branch (force delete of remote)
# push the deletion to the branch
git push origin :<branchname>
git push origin local:<branch>
# misc branching
git show-branch
git branch --merged
git branch --no-merged
bit branch older HEAD~3
git checkout older
git commit -am 'changes'
# removing files (remove from file system and stage for removal)
git rm file
# removing file via OS and then stage all files not in working copy
rm file
git add -u .
# commands to use when repackaging in Java (dir and files changed)
git add -A .
# detect moves in log files based on similarity index
# there is no actual move operation, but you can simulate approximation in log based on similar scores
git log --stat
git log --stat -M80
# detect copies in log files based on similarity index
git log -1 --stat -C --find-copies-harder
# blame (view source file w/ username and hash of commmit)
git blame file
# blame with copy/paste detection
# good way to see where it came from so you know who to ask rather than who copied
git blame -C file
# Fix and reset repository to good states
git reflog
git rev-parse HEAD@{0}
git rev-parse HEAD^^
# Reset to a given state
git reset --hard HEAD@{1}
# Squash commits...similar to rebase?
git merge --squash branch
# force pull
git pull origin +master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment