ITK/Git/TipsAndTricks

From KitwarePublic
< ITK‎ | Git
Revision as of 17:37, 18 July 2011 by Matt.mccormick (talk | contribs) (→‎Bash prompt)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Tips

Editor support

Emacs users: if you put this line in your .emacs file:

(setq auto-mode-alist (cons '("COMMIT_EDITMSG$" . auto-fill-mode) auto-mode-alist))

Git will automatically wrap your commit messages, which is what good git etiquette requires.

Shell customization

Bash completion

Bash users: git comes with a set of completion options that are very useful. The location of the file varies depending on your system:

 source /opt/local/share/doc/git-core/contrib/completion/git-completion.bash  # Mac with git installed by Mac Ports
 source /usr/share/bash-completion/git       # Linux
 source /etc/bash_completion.d/git           # Linux debian/gentoo

Bash prompt

If you are using the bash shell, you can customize the prompt to show which git branch is active. Here are the commands for your ~/.bashrc file:

source /etc/bash_completion.d/git   # or one of the appropriate paths from the above section
export GIT_PS1_SHOWDIRTYSTATE=1  
export GIT_PS1_SHOWUNTRACKEDFILES=1
export GIT_PS1_SHOWUPSTREAM="verbose" 
export PS1="[\[\e[01;34m\]\W\[\e[31m\]\$(__git_ps1 " (%s)")\[\e[00m\]]\[\e[00m\]
  1. For more information on the options, see the comments in the top of the bash completion script.

Renaming

Git does not explicitly track renames. The command

$ git mv old new

is equivalent to

$ mv old new
$ git add new
$ git rm old

Neither approach records the rename outright. However, Git's philosophy is "dumb add, smart view". It uses heuristics to detect renames when viewing history after-the-fact. It even works when the content of a renamed file changes slightly.

In order to help Git efficiently detect the rename, it is important to remove the old file and add the new one in one commit, perhaps by using git mv or the above 3-step procedure. If the new file were added in one commit and the old file removed in the next, Git would report this as a copy followed by a removal. It's copy-detection heuristics are more computationally intensive and must be explicitly enabled with the -C option to relevant operations (such as git blame).