ITK/Git/TipsAndTricks: Difference between revisions
(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 ...") |
|||
Line 15: | Line 15: | ||
source /opt/local/share/doc/git-core/contrib/completion/git-completion.bash # Mac with git installed by Mac Ports | 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 /usr/share/bash-completion/git # Linux | ||
source /etc/bash_completion.d/git # Linux debian/gentoo | |||
===Bash prompt=== | ===Bash prompt=== |
Revision as of 17:35, 18 July 2011
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:
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
).