Git Tips & Tricks
I don’t have to start this article with some cheap filler about how great Git is, right? or how everyone should use it every day? Great.
Here are some great tricks. All of these rely on creating a file called .gitconfig in your home directory (in my case /Users/kennymeyers).
CP
In my .gitconfig, I have the following:
[alias]
cp = commit -am 'cp'
Anything after the [alias] divider creates an alias or shortcut for a Git action. When I’m in terminal and I type git cp, it will automatically add changes to files with a commit message of ‘cp’. UPDATE: Some clarity in response to comments. The phrase “cp” stands for checkpoint. I use it as a quick save method. When I’m satisfied with what I’m working on I do a rebase as described below. I remove all “cp” messages and squash them down into a single commit with a clear descriptive commit message.
What it will not do: It won’t add newly created files to a repository, meaning, it only works on files already in your git repo.
Commit template
I also have the following:
[commit]
template = /path/to/commit-template.txt
This creates a commit message from a commit template (.txt file on my hard drive). Whenever you type git commit the .txt file will be loaded automatically into your message and you can add, replace, or delete text. Here’s a sample commit-template.txt:
Title: [ Fill this in ]
Author: Kenny Meyers
Description: [ Fill in description ]
This keeps things consistent, and consistency is the king of useful commit logs.
What it will not do: It will not work with above “cp” alias because the message "cp" takes priority over a template.
Rebase
One of my favorite things to do with Git is an interactive rebase. When I’m working in different branches I will us the above "cp" command a lot. Then, I will type the following into terminal:
`git rebase -i [branch to compare against]`
This gives me a list of changes I made to the repository as compared to another branch. Normally, my first commit of changes will use a fancy template then the rest will use the ‘cp’ command. The interactive rebase allows me to squash all the commits I made into one commit with a beautiful message.
Topic branches
Not really a tip (or trick) but I’m a huge fan of topic branches. Use them!
git checkout -b the-feature-or-fix-you-are-creating
DO IT FOR EVERY LITTLE THING, GOD DAMN IT!
What are your favorite tricks?
November, 29th 2011
Comments
Love the tip about commit templates. Awesome!
Posted at 09:48 AM on November 29, 2011
Interesting list of tips. I’m wondering what’s the meaning of “cp” here. “Commit P…”?
Posted at 10:36 AM on November 29, 2011
“with a commit message of ‘cp’” This is horrible advice. People should use descriptive commit messages. Using this “tip” will just result in a log of messages that read “cp” which is completely useless.
Posted at 11:00 AM on November 29, 2011
I have updated the text with some clarity, thanks all!
Posted at 12:14 PM on November 29, 2011
Dan, instead of saying things are horrible and useless, why not offer solutions? I find the checkpoint method kenny describes to be amazingly useful. I tend to ‘over commit’ and instead of littering the logs with many small ‘useless’ commits, you can rebase with a descriptive commit message.
Thanks for the fantastic and useful tips, Kenny.
Posted at 03:55 PM on November 29, 2011
git flow is perfect for the last tip.
Posted at 09:31 PM on December 13, 2011
ok iasdkjb lahsd kj123 s
Posted at 06:37 AM on January 18, 2012
Post a Comment