An occasional outlet for my thoughts on life, technology, motorcycles, backpacking, kayaking, skydiving...

Wednesday, March 9, 2011

git Oneliners

This started out as a tweet but quickly became a collection of one-liners. Some of this may be able to be accomplished with switches to regular git commands, but I can write awk faster than I can read a man page. Sad but true.

# add files to the index which `git status` lists as untracked:
git add $(git status | awk 'p==1{print $2}; /git add /{p=1}')

# remove files from the index which `git status` lists as deleted:
git rm $(git status | awk '$2=="deleted:" && p==1{print $3}; /git add\/rm /{p=1}')

# add files to the index which `git status` lists as ``Changed but not updated``/modified:
git add $(git st | awk '$2=="modified:" && 'p==1{print $3}; /git add /{p=1}')

# checkout the penultimate commit (used for stepping backwards through commit to find the introduction of a bug)
git checkout $(git log | awk '/^commit /{if(i++>0){print $2; exit}}')
# That was my first day git user approach. Now I know to use
git checkout HEAD~1
# Yes, I know about HEAD^, but because HEAD^2, HEAD^3, etc. don't work I chose tilde notation.

# create a quick git repo with 50 commits for trying out git hackery
d=/tmp/git_hack; rm -rf $d; git init $d; cd $d; for i in {1..50}; do git log | md5 >test.txt; git add .; git commit -m "commit $i"; done >/dev/null

# rebase 1 commit out of a branch
read -p "hash? " hash; git rebase --onto $hash~1 $hash HEAD; files=$(git st -s | sed '/^UU/!d;s/^UU //'); [[ -n $files ]] && (git co --theirs $files; git add $files; git rebase --continue)

Followers