New Things I Learned About git…
One of the sessions I attended at That Conference was the “Understanding Git Part-2” talk by Keith Dahlby. I have worked with git and github a lot over the last 5 years, but I learned quite a bit from this talk. In this post, I will highlight some of the most useful takeaways.
git has numerous configuration settings which fall into three categories: Global, User, and Repository. You can view your git settings with:
git config -l
I have always edited configuration settings on the command line one at a time but I learned that
git config -e --global
will open up a text editor with the config settings. This is much easier than viewing them from the command line or editing one at a time.
Other useful default settings I was unaware of are:
merge.keepBackup = false diff.renames = copies pull.rebase = true
merge.keepBackup should be false otherwise you get useless .orig files when there are merge conflicts. diff.renames should be set to “copies” which tells git explicitly to track renamed files.
pull.rebase should be set to true to avoid getting merge commits when you do a pull and it is not a fast-forward update.
I have a few aliases setup for the simple commands that I use most commonly (status, branch, commit, and checkout) but Dahlby showed how to create aliases that combine more than one command. For example, when you have new files the standard
git commit -am "message..."
does not work because the -a (for Add) will not stage the new files. A useful alias that will include new files as well as changes would look like:
git alias.call "!git add -A && git commit"
(Note the use of “!” when combing multiple git commands). So now if you have new files you can commit everything in one command with:
git call -m "message"
“call” stands for commit-all but you can use another alias name if that make sense for you.
Another useful alias he suggests is:
git config alias.new = log master.. --reverse
So “git new” would should show all commits on the current branch that are not in master starting with the first added commit.
Other than listing commits with
git log --oneline
searching through history is not something I have used much. But there are actually some pretty powerful search capabilities for finding commits. For example:
git log --since 8/20/2016 //only shows commits on or after 8/20/2016 git log --until 7-31-2016 //limits commits to on or before 7/31/2016 git log --author firstname.lastname@example.org //limits commits to a specific user git log --committer email@example.com //the committer may be different than the author git log --branches //includes commits in all branches git log --grep="cool report" //finds all commits with the test "cool report" in the commit message
Each of these switches is useful on it’s own, but they become very powerful when they are combined. For example:
git log --branches --author firstname.lastname@example.org --until 5/31/2016 //shows all commits by me in all branches prior to 6/1/2016 git log --grep="cool report" --branches --since 7/1/2016 //shows all commits with the text "cool report" in all branches since 7/1/2016
There are also options for searching within the code itself instead of just the commit message.
git log -S "controller" -p //displays all commits where the change diff included the word "controller" and displays the changed code.
In my next post I will look at some interesting ways to manipulate history in git.