Git tips from Jaka Kranjc

Jaka “lynx” Kranjc sent us this helpful bit of information on working with Git and our grimoires, mostly for developers.

I noticed a long drought on magepower, so here are some git tips (mostly) for developers:

You can see the contents of files in other branches or tags without checkout-ing them first:

navaden@lynxlynx main-grim 0 $ git show :VERSION
0.19-test
navaden@lynxlynx main-grim 0 $ git show my-stable-rc:VERSION
0.18-rc

All git commands are accessible directly. So by using dashes instead of spaces, you can reap the benefits of tab completition. It shines for longer commands like git-update-index or git-cherry-pick. Also all the man pages need to be called this way.

Through long time use, the git repository gains some dead weight. You can remove it and reoptimise the repo by running git-gc.

Here’s a handy git-commit wrapper. It will shorten the writting of the commit message by adding the basename of the first argument and a colon as the initial commit message:

git-commit(){
  command git-commit -e -m "$(basename -- $1):" "$@";
}

git-commit smgl/guru-tools/ ChangeLog would result in “guru-tools:”.

Not many people do cherry-picking, but for those who do, we have an informal policy that the -x option needs to be passed to git (adds the originating commit to the commit message). In older git 1.4 versions it was on by default, but this has changed since.

alias git-cherry-pick="git-cherry-pick -x"

If you want to reset only a part of the repository, you’ll have to use a trick (git-reset --hard can’t work on single files):

git diff some/path | patch -p1 -R

Which should do it in most cases, but will likely fail for binaries. Here is an alternative approach which doesn’t have that problem:

git-dir-reset(){
  rm -r $1/*
  git-archive --format=tar ${2:-HEAD}:$1 | tar -x -C $1
}

You could also do it via git-checkout, which can do single files too.

Comments are closed.