Skip to content

Instantly share code, notes, and snippets.

@lushone
Forked from chrismccoy/gitcheats.txt
Created March 24, 2014 15:16
Show Gist options
  • Save lushone/9742183 to your computer and use it in GitHub Desktop.
Save lushone/9742183 to your computer and use it in GitHub Desktop.
# shortform git commands
alias g='git'
# generate file list modified since last commit and export to tar file
git diff-tree -z -r --no-commit-id --name-only --diff-filter=ACMRT COMMID_HASH | xargs -0 tar -rf list.tar
# export unpushed files list
git log -z origin/master..master --name-only --pretty="format:" | sort -zu | xargs -0 tar -rf list.tar
# Count the lines of each file extenion in a list of files
git ls-files | xargs wc -l | awk -F ' +|\\.|/' '{ sumlines[$NF] += $2 } END { for (ext in sumlines) print ext, sumlines[ext] }'
# Show git commit history
git reflog show | grep '}: commit' | nl | sort -nr | nl | sort -nr | cut --fields=1,3 | sed s/commit://g | sed -e 's/HEAD*@{[0-9]*}://g'
# Restore deleted file from GIT repository
git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"
# Number of commits per day in a git repo
git log | grep Date | awk '{print " : "$4" "$3" "$6}' | uniq -c
# Remove git branches that do not have a rmote tracking branch anymore
git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
# Remove .git dirs
find . -name ".git" -type d -exec rm -rf {} \;
# Top Ten of the most active committers in git repositories
git shortlog -s | sort -rn | head
# git - create a local branch that tracks with the remote branch
git checkout -tb mybranch origin/mybranch
# Prints per-line contribution per author for a GIT repository
git ls-files | xargs -n1 git blame --line-porcelain | sed -n 's/^author //p' | sort -f | uniq -ic | sort -nr
# Git Tree Command with color and tag/branch name
git log --graph --oneline --all --decorate --color
# Open the current project on Github by typing gh
git remote -v | grep fetch | sed 's/\(.*github.com\)[:|/]\(.*\).git (fetch)/\2/' | awk {'print "https://github.com/" $1'} | xargs open
# Show git branches by date - useful for showing active branches
for k in $(git branch | sed /\*/d); do echo "$(git log -1 --pretty=format:"%ct" $k) $k"; done | sort -r | awk '{print $2}'
# Update (pull commits from) all submodules
git submodule foreach git pull --ff-only origin master
# commit message generator - whatthecommit.com
curl http://whatthecommit.com/index.txt
# Create tarball of files modified in git
tar czf git_mods_circa_dec23.tgz --files-from <(git ls-files -m)
# Sequential revision numbers in Git
git rev-list --reverse HEAD | awk "/$(git log -n 1 --pretty="format:%h")/ {print NR}"
# commit message generator - whatthecommit.com
curl -s 'http://whatthecommit.com/' | grep '<p>' | cut -c4-
# Show git branches by date - useful for showing active branches
for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k" --`\\t"$k";done|sort
# git Output remote origin from within a local repository
git config --local --get remote.origin.url
# delete local *and* remote git repos if merged into local master
git branch | cut -c3- | grep -v "^master$" | while read line; do git branch -d $line; done | grep 'Deleted branch' | awk '{print $3;}' | while read line; do git push <target_remote> :$line; done
# Using Git, stage all manually deleted files.
git add -u
# Pull git submodules in parallel using GNU parallel
parallel -j4 cd {}\; pwd\; git pull :::: <(git submodule status | awk '{print $2}')
# bash script to zip a folder while ignoring git files and copying it to dropbox
git archive HEAD | gzip > ~/Dropbox/archive.tar.gz
# Push each of your local git branches to the remote repository
git push origin --all
# Deleting a remote git branch (say, by name 'featureless')
git push origin :featureless
# git-rm for all deleted files, including those with space/quote/unprintable characters in their filename/path
git ls-files -z -d | xargs -0 git rm --
# GIT: list unpushed commits
git log --oneline <REMOTE>..<LOCAL BRANCH>
# commit message generator - whatthecommit.com
lynx -dump -nolist http://whatthecommit.com/|sed -n 2p
# commit message generator - whatthecommit.com
curl -s http://whatthecommit.com | html2text | sed '$d'
# commit message generator - whatthecommit.com
curl -s http://whatthecommit.com | sed -n '/<p>/,/<\/p>/p' | sed '$d' | sed 's/<p>//'
# telling you from where your commit come from
function where(){ COUNT=0; while [ `where_arg $1~$COUNT | wc -w` == 0 ]; do let COUNT=COUNT+1; done; echo "$1 is ahead of "; where_arg $1~$COUNT; echo "by $COUNT commits";};function where_arg(){ git log $@ --decorate -1 | head -n1 | cut -d ' ' -f3- ;}
# Show the changed files in your GIT repo
git status | perl -F'\s' -nale 'BEGIN { $a = 0 }; $a = 1 if $_ =~ /changed but not updated/i; print $F[-1] if ( $a && -f $F[-1] )'
# Search git repo for specified string
git grep "search for something" $(git log -g --pretty=format:%h -S"search for something")
# Get first Git commit hash
git log --pretty=format:%H | tail -1
# Get first Git commit hash
git log --format=%H | tail -1
# List all authors of a particular git project
git log --format='%aN <%aE>' | awk '{arr[$0]++} END{for (i in arr){print arr[i], i;}}' | sort -rn | cut -d\ -f2-
# See all the commits for which searchstring appear in the git diff
git log -p -z | perl -ln0e 'print if /[+-].*searchedstring/'
# List every file that has ever existed in a git repository
git log --all --pretty=format:" " --name-only | sort -u
# git pull all repos
find ~ -maxdepth 2 -name .git -print | while read repo; do cd $(dirname $repo); git pull; done
# Add .gitignore files to all empty directories recursively from your current directory
find . \( -type d -empty \) -and \( -not -regex ./\.git.* \) -exec touch {}/.gitignore \;
# Display condensed log in a tree-like format.
git log --graph --pretty=oneline --decorate
# List all authors of a particular git project
git log --format='%aN' | sort -u
# List all authors of a particular git project
git shortlog -s | cut -c8-
# Show git branches by date - useful for showing active branches
for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k"`\\t"$k";done|sort
# Move all files untracked by git into a directory
git clean -n | sed 's/Would remove //; /Would not remove/d;' | xargs mv -t stuff/
# Prints per-line contribution per author for a GIT repository
git ls-files | while read i; do git blame $i | sed -e 's/^[^(]*(//' -e 's/^\([^[:digit:]]*\)[[:space:]]\+[[:digit:]].*/\1/'; done | sort | uniq -ic | sort -nr
# Prints per-line contribution per author for a GIT repository
git ls-files | xargs -n1 -d'\n' -i git-blame {} | perl -n -e '/\s\((.*?)\s[0-9]{4}/ && print "$1\n"' | sort -f | uniq -c -w3 | sort -r
# Makes a project directory, unless it exists; changes into the dir, and creates an empty git repository, all in one command
gitstart () { if ! [[ -d "$@" ]]; then mkdir -p "$@" && cd "$@" && git init; else cd "$@" && git init; fi }
# git Revert files with changed mode, not content
git diff --numstat | awk '{if ($1 == "0" && $2 == "0") print $3}' | xargs git checkout HEAD
# Show changed files, ignoring permission, date and whitespace changes
git diff --numstat -w --no-abbrev | perl -a -ne '$F[0] != 0 && $F[1] !=0 && print $F[2] . "\n";'
# Show (only) list of files changed by commit
git show --relative --pretty=format:'' --name-only HASH
# Stage only portions of the changes to a file.
git add --patch <filename>
# Show log message including which files changed for a given commit in git.
git --no-pager whatchanged -1 --pretty=medium <commit_hash>
# search string in _all_ revisions
for i in `git log --all --oneline --format=%h`; do git grep SOME_STRING $i; done
# git remove files which have been deleted
git ls-files -z --deleted | xargs -0 git rm
# Show git branches by date - useful for showing active branches
for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r
# add forgotten changes to the last git commit
git commit --amend
# git remove files which have been deleted
git rm $(git ls-files --deleted)
# git diff of files that have been staged ie 'git add'ed
git diff --cached
# add untracked/changed items to a git repository before doing a commit and/or sending upstream
git status|awk '/modified:/ { printf("git add %s\n",$3) }; NF ==2 { printf("git add %s\n",$2) }'|sh
# Better git diff, word delimited and colorized
git config alias.dcolor "diff --color-words"
# Better git diff, word delimited and colorized
git diff -U10|dwdiff --diff-input -c|less -R
# Better git diff, word delimited and colorized
git diff -U10 |wdiff --diff-input -a -n -w $'\e[1;91m' -x $'\e[0m' -y $'\e[1;94m' -z $'\e[0m' |less -R
# extracts 64 bytes of random digits from random lines out of /dev/random sent to stdio
cat /dev/urandom|od -t x1|awk 'NR > line { pos=int(rand()*15)+2;printf("%s",$pos);line=NR+(rand()*1000);digits = digits+2 } digits == 64 { print("\n");exit }'
# Count git commits since specific commit
git log --pretty=oneline b56b83.. | wc -l
# Count git commits since specific commit
git log --summary 223286b.. | grep 'Author:' | wc -l
# Execute git submodule update in parallel with xargs
git submodule status | awk '{print $2}' | xargs -P5 -n1 git submodule update --init
# Incorporating a finished feature on develop
git checkout develop; git merge --no-ff myfeature
# Creating a feature branch
git checkout -b myfeature develop
# My Git Tree Command!
git log --graph --oneline --all
# show git logging
git log --stat
# Create a git archive of the latest commit with revision number as name of file
git archive HEAD --format=zip -o `git rev-parse HEAD`.zip
# List files under current directory, ignoring repository copies.
function have_here { find "${@:-.}" -type d \( -name .git -o -name .svn -o -name .bzr -o -name CVS -o -name .hg -o -name __pycache__ \) -prune -o -type f -print; }
# revert the unstaged modifications in a git working directory
git diff | git apply --reverse
#commit message generator
curl -s http://whatthecommit.com/ | tr -s '\n' ' ' | grep -so 'p>\(.*\)</p' | sed -n 's/..\(.*\)..../\1/p'
# random git commit message
git-random(){ gitRan=$(curl -L -s http://whatthecommit.com/ |grep -A 1 "\"c" |tail -1 |sed 's/<p>//'); git commit -m "$gitRan"; }
# rename a branch
git branch -m old_branch new_branch
# set upstream for existing branch
git branch --set-upstream <branch> <remote>/<branch>
# checkout remote branch
git checkout -b test origin/test
# pretty git commit log
git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
# make git HEAD same as origin/master
git reset --hard origin/master
# delete a remote branch
git push origin :heads/branch_name
# revert uncommited git changes
git reset --hard HEAD
# add .gitignore to enable add empty directory to git
for i in $(find . -type d -regex ``./[^.].*'' -empty); do touch $i"/.gitignore"; done;
# list files between git commits
git diff --name-only 4ce07ee 7cdf78b
# list all branches
git branch -a
# install a new git repo
function gitinstall(){ git init; git remote add origin "$@"; git config branch.master.remote origin; git config branch.master.merge refs/heads/master; git pull;}
# git recursive rm
git ls-files -d -z | xargs -0 git update-index --remove
# undo last git commit
git reset --soft HEAD^
# find deleted stashes and other lost commits in git
git fsck --no-reflog | awk '/dangling commit/ {print $3}'
# git apply patch
git format-patch -k --stdout rev1-1..rev2 | git am -k -3
# git cat
git cat-file -p $(git ls-tree $1 "$2" | cut -d " " -f 3 | cut -f 1)
# list unmerged files
git ls-files -u|awk '{print $4}'|sort -u
# list added files in the index
git diff-index HEAD|awk '{print $5 " " $6}'|sed -n -e's/^A //p'
# print number of modified files
git status --porcelain | cut -c 1-2 | grep M | wc -l | tr -d " "
# show all remote git branches
git remote show origin
# fancy git prompt
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(git::\1)/'
}
export PS1="\[\033]0;\h \w \$(parse_git_branch) \007\][\[\033[01;35m\]\h \[\033[01;34m\]\w \[\033[31m\]\$(parse_git_branch)\[\033[00m\]]$ "
# Recursively remove all untracked files in the tree.
git clean -f
# throw out all of your changes to existing files, but not new ones
git reset --hard
# remove file from staging area
git rm --cached [file]
# see diff of files in staging area
git diff --staged
# see tracked files
git ls-files
# see a branch graph
git log --graph
# see all tags
git tag
# see list of deleted files
git ls-files -d
# restore all deleted files
git ls-files -d | xargs git checkout --
# view commits not yet pushed to remote
git log --branches --not --remotes
# difference between two branches
git diff --stat --color master..branch
# see a list of all objects
git rev-list --objects --all
# remove file from index
git rm --cached filename.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment