ITK/Git/TipsAndTricks

From KitwarePublic
< ITK‎ | Git
Revision as of 14:53, 11 October 2010 by Blezek (talk | contribs) (Created page with "=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 ...")
(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


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:

parse_git_branch()
  {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
  }
export PS1="[\[\e[01;34m\]\W\[\e[31m\]\$(parse_git_branch)\[\e[00m\]]\[\e[00m\]

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).