A few Git tips
I don’t do that much that’s clever with git, but I’ve found the following helpful.
Automatically prune
When you do a git fetch or git pull, you can ask it to remove remote tracking branches for a branch that has been removed on the remote by using the --prune flag.
This can be automated globally with:
git config --global fetch.prune true
and if you only want it for a specific repository, you can use:
git config remote.origin.prune true
One line log
I like a sensible one line log format, so I regularly use:
git log --oneline --decorate --all --graph
This can be aliased to `oneline` using:
git config --global alias.oneline 'log --oneline --decorate --all --graph'
Limit log to a certain number of commits
Just add dash, followed by the number of commits. e.g. to limit to the last half dozen commits:
git log -6
I do this all the time!
No paging when diffing
I don’t need my diff paged as I can use my terminal to scroll up. Hence, I use cat as my pager which doesn’t do anything clever!.
git config --global core.pager cat
See diff while writing the commit message
When writing the commit message, I sometimes find it helpful to have the diff in the editor window both as a reminder of what I’m committing and as a final check that it’s what I expect. This is done with the -v flag and the diff is not part of the commit message that’s saved.
git commit -v
Rebase with confidence
I regularly rebase my local branches before pushing to tidy them up or move them up to the latest commit on develop. I can’t recommend learning how git rebase works enough. This is not the article for it though. Try Chris Tankersley‘s Git rebase is your friend as a starting point.
Elsewhere on the Internet
I’m sure that there’s many many more tips and tricks available for git that make workflows easier. In my experience, try to add only a couple at a time until they stick. Many more than that and you’ll forget the to use them!
If you are into vidoes, then Lorna Mitchell‘s Advanced Git for Developers is a must-watch.
Gold! Thank you for these and especially the last one (seeing the diff while you write your commit message). I found you through PHP Weekly.
Thank you; a nice summary of some git gems.
One typo to be fixed:
old: "you can ask it to remote remote tracking branches…"
new: "you can ask it to remove remote tracking branches … "
Thanks. Fixed.