Skip to content

Instantly share code, notes, and snippets.

@rmandvikar
Last active March 9, 2017 08:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rmandvikar/36768c5de60d4bf163c5968afcc267a4 to your computer and use it in GitHub Desktop.
git commands
##list##
// pull up man page for [anything]
git [anything] --help
git status
git branch
git tag
git log
git version
type q to exit out from git's paging
##branch##
// create
git branch [branchname]
git checkout [branch]
// create and checkout branch
git checkout -b [branchname]
// delete unmerged branch
git branch -D [branchname]
// verbose
git branch -v
// more verbose
git branch -vv
// add changes to index
git add .
git commit -m "[message]"
// add changes to index and commit
git commit -m "[message]" -a
// rename branch
git branch -m old_branch new_branch
// show which branches were merged into
git branch --merged
// status with remote branch status
git status -b
##reset##
git reset # clear index (unstage)
git reset --soft head^ # go to prev commit and stage current commit
git reset --mixed head^ # go to prev commit and unstage current commit
same as git reset --soft head^; git reset --mixed
git reset --hard <commit> #
##merge/rebase/cherrypick##
// merge / rebase current branch with master
git rebase master #from dev branch
git merge master
// octopus strategy only if no conflicts
// for conflicts only recursive strategy
git merge branch1 branch2
// reset after a merge (even clean merge)
git reset --merge ORIG_HEAD
// transplant dev2 (created from dev1) onto master
git rebase --onto master dev1 dev2
// rebase dev on master and take dev’s file if conflict (-s recursive only)
git merge|rebase master -X theirs # take master's if conflict
git merge|rebase master -X ours # take current branch's if conflict
// cherrypick a commit
git cherry-pick <commit>
// see ours or theirs in conflict
git difftool [--ours|theirs] file
// bring all changes from branch as non-merge commit
git merge --squash branch
// ignore work on dev just merge but take all of current branch
git merge -s ours dev
##tag##
git tag
ex: git tag <tagname> <branch> # tagname can be x/y to create dir
##diff##
git diff
git difftool # git dt
// diff current branch with master (master + diff deltas = current)
git diff master # same as git diff master..[head]
// diff current branch with master with file names only
git diff master --name-only
git diff master --name-status
// .. and ... with log
// in origin but not local
git log dev..origin/dev
// in github and local from ancestor
git log dev...origin/dev # after fetch, check what’s new on origin
// in origin and local
git log dev origin/dev
// .. and ... with diff
// compare dev to origin
git diff dev origin/dev
git diff dev..origin/dev // same as above
// in origin but not in dev
git diff dev...origin/dev
// note: diff ... is same as log ..
// show changes for a commit
git diff <commit>^!
// unstaged
git difftool
// staged
git difftool --staged
// staged + unstaged
git difftool head
##log##
git log
git log --pretty=oneline
git log --oneline (shorthand for git log --pretty=oneline)
// ain't that cool
git log --oneline --graph --decorate --all
// blame
git blame <file>
// show line 12 with 1 line before or 2 lines after
git blame -L12,-1 [-L12,+2] file.ext
##stash##
git stash save|pop|apply
// untracked files included
git stash save -u|--include-untracked
// preserve index
git stash pop|apply --index
// v2.11+
git stash apply <i>
##config##
// global config
// set %HOME% to a local dir (d:\git), default location is H: which has permission issues
%HOME%\.gitconfig
or c:\Users\<user_dir>\.gitconfig for home machines
// system config
// local config
<project>\.git\config
// list
git config --global user.email
git config --global user.name
// set
git config --global user.email "me@abc.com"
git config --global user.name "first last"
// edit
git config --global -e
##gitconfig##
see gist https://gist.github.com/rmandvikar/dee01b7b1f7aa22a5406eba7088125eb
##poshgit config##
# poshgit colors
$GitPromptSettings.WorkingForegroundColor = [ConsoleColor]::Red
$GitPromptSettings.UntrackedForegroundColor = [ConsoleColor]::Red
$GitPromptSettings.IndexForegroundColor = [ConsoleColor]::Green
##remote branches##
// take a dir and create a bare repo at target
git clone --bare d:\public_html d:\public_html.git
git init --bare
// list remotes
git remote
git remote -v
// if you cloned a bare repo from already existing git repo, add remote to it
git remote add remote_name repository
ex: git remote add origin d:\depot\public_html.git [--update]
// or just clone the bare repo
git clone d:\public_html.git
// .gitconfig after adding remote
[remote "origin"]
url = d:\public_html
fetch = +refs/heads/*:refs/remotes/origin/*
// updating origin will create tracking branches (remote, local)
git remote update remote_name
ex: git remote update origin
// show all (local, remote -r) branches
git branch -a
// show remote origin info
git remote show origin
// rename remote origin to github
git remote rename origin github
// push / pull / fetch changes
git push | pull | fetch remote_name localbranch_name
ex: git push | pull | fetch origin master
// cause local branch to track remote branch to push with just ‘git push’
git branch -u origin/dev
git branch -u github/dev
// branch alias
[branch "dev"]
remote = origin
merge = refs/heads/dev
rebase = true # omit this and use git pull --rebase
// with branch alias, current branch is used for push / pull
git push | pull | fetch
git pull --rebase
// delete a remote tracking branch
git branch -r -d origin/dev
git remote prune
git remote update origin
// add a new remote
git config remote.publish.url 'd:\depot\repo.git'
git config remote.publish.push '+refs/heads/*:refs/remotes/publish/*'
[remote "publish"]
url = d:\depot\repo.git
fetch = +refs/heads/*:refs/remotes/publish/*
// set an already existing local branch to track a remote tracking branch
git branch --set-upstream-to origin/dev dev
git branch -u github/dev # for current branch
git branch -u github/dev dev # for dev branch
// commits in origin/master and not in master
git log master..origin/master
// push local branch mystuff (creates remote tracking branch)
git push origin mystuff
git push origin mystuff:dev # remote name as dev
// delete remote branch dev
git push origin --delete dev
##filter-branch##
// remove file from master
git filter-branch --tree-filter 'rm -f file.txt' master
or
git filter-branch --index-filter \
'git rm --cached --ignore-unmatch file.txt' master
##rev-list##
// all commits
git rev-list --all
// all commits count
git rev-list --all --count
// dev branch commits count
git rev-list --count dev
// show how far away current branch is from master
git rev-list --left-right --count @...master
// last commit of 2011
git rev-list -n 1 --before="Jan 1, 2012 00:00:00" master
// git query tool for commit db (ex: date based checkout)
git rev-list --before="Jan 1, 2012 00:00:00" master -n 10
// change name and email
git filter-branch --commit-filter '
if [ "$GIT_COMMITTER_NAME" = "RM" ];
then
GIT_COMMITTER_NAME="<name>";
GIT_AUTHOR_NAME="<name>";
GIT_COMMITTER_EMAIL="<name>@email.com";
GIT_AUTHOR_EMAIL="<name>@email.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
## advanced, tips, tricks ##
// similar to status
git whatchanged
// search
git grep -i <"search">
// do not hand delete refs etc
git update-ref <ref> SHA1
git update-ref -d <ref>
// track filename commits (or renames)
git log –-follow <file>
git log –-follow --name-only <file>
// keep but don’t track
git update-index --no-assume-unchanged <filename>
git update-index --assume-unchanged <filename>
git ioc <filename>
git unioc <filename>
// interactive hunk staging
git add -i
//select p/patch
p
//select file/files
1,2
//enter for options
Stage this hunk [y,n,a,d,/,j,J,g,e,?]?
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
//stash unstaged changes only (staged changes are kept untouched)
git stash --keep-index
// delete merged branches except some
git branch | egrep -v "(\*|master|release/next|dev|temp)" | xargs git branch -d
// find file and open
git ls-files *BitSet.cs | xargs o
##grep##
git grep -e "--name-status"
git grep -e "--name-status" -- '*.c' '*.h'
git log --grep '^Fix' -5
git log --grep '\.$' -5
git log --grep '\.$' --invert-grep|-v -3
git log --author=rmandvikar -10
# find commits with 52+ chars subject
git log --all --pretty="%H##hack##%s" \
| awk -F "##hack##" 'length($2) > 52 {print $1}' \
| xargs git show -s --pretty="%h %s - %an"
##shortlog##
git shortlog --summary|-s --count|-n --email|-e
# use .mailmap to map different users to single user
name <goodemail> <otheremail>
##prepare-commit-msg##
# remove 1st line's 1st occurrence of "remote-tracking" for merge (merge_msg)
sed -i -e '1 s/Merge remote-tracking branch /Merge branch /' "$1"
##mailmap##
# create a temp file as new.mailmap and diff existing .mailmap and new.mailmap to get only new lines in new.mailmap.
git shortlog --all -se
| awk -F'\t' '{print $2}'
| awk -F' <' '{print $1,"<"$2, "<"$2}'
> new.mailmap
diff .mailmap new.mailmap
| grep '^>'
| sed 's/^> //'
# copy the new lines to .mailmap at proper place.
# make <email> lowercase.
cat .mailmap
| awk -F' <' '{if ($1!=$0) print $1, "<"tolower($2), "<"$3; else print $0;}'
> temp.mailmap
&& cat temp.mailmap
> .mailmap
##upper case branches by user##
id=${1-$(git config user.name)}
git for-each-ref --sort=committerdate \
--format='%(refname:short)##hack##%(authorname)##hack##%(committername)' refs/remotes/ \
| awk -F '##hack##' -v id="$id" '{ if($2==id || $3==id) {print $1} }' \
| grep [A-Z]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment