Git aliases for creating development branch patches

Now that everyone should be getting comfortable with Git (even me, one of the crazy people that actually liked CVS), we're all finding little tricks and tips on our workflow. I wanted to share some Git aliases I figured out today with everyone, and I'd encourage you to share your own as well!

When working on core issues, I have only one core checkout per major Drupal version. For Drupal 8, that directory is ~/Sites/drupal8dev. When working on an issue, I create a local development branch with the name issuenumber-description-of-issue. For example, for http://drupal.org/node/1174630, I ran git checkout -b 1174630-html5-url-element to create my local issue branch.

I commit changes to that branch only when working on the issue. Side note, I usually use git commit --amend to add the current commit onto the previous commit. I usually make smaller incremental changes, or fix small things that don't require a separate commit or history. I also use git pull --rebase so my commits are always last. Since I'm the only one working on this, it's ok. Randy Fay and Sam Boyer will forgive me eventually.

After a bit of progress, I want to create a patch that I can upload to the issue located in the root of my Git repo checkout. I like using git format-patch as it lists each commit made with attribution. I've been running each command manually for a while and finally figured out some 'aliases' to save me some time and typing.

In your command line, run the following commands to add these three aliases to your ~/.gitconfig file:

[alias]
branch-name = !git for-each-ref --format='%(refname:short)' `git symbolic-ref HEAD`
cd-root = rev-parse --show-cdup
branch-diff = !git format-patch --stdout $1 > `git cd-root``git branch-name`.patch

So now to generate a patch file against core's 8.x branch when working in my local branch 1174630-html5-url-element, all I have to do is run this command from any directory in the git repo:

git branch-diff origin/8.x

And then I can just upload the file 1174630-html-url-element.patch that's located right inside ~/dave/Sites/drupal8dev/. And yes, I end up with a lot of patch files in my repo directories. I could probably have the patches generated inside a 'patches' sub-folder. In which case the branch-diff alias would be !git format-patch --stdout $1 > `git cd-root`patches/`git branch-name`.patch

Hopefully others find this useful! Also this is not limited to just core, it works on contrib module Git checkouts as well.

References

http://blogs.gnome.org/danni/2011/03/07/useful-git-aliases/
http://stackoverflow.com/questions/957928/is-there-a-way-to-get-to-the-g...
https://twitter.com/#!/caroltron/status/17108240293

Dave Reid's picture

If you found this post early, I had to make some corrections:
1. Edit your ~/.gitconfig file directly. Don't add these aliases via the command line - it doesn't work.
2. The cd-root alias actually works now.

Josh The Geek's picture

@Dave Reid: Were you using git config --global?
I keep a separate folder outside of my clones for patches.

Dave Reid's picture

Yeah my original instructions said to use git config --global to add the aliases, but since they're complex, I had problems adding them that way rather than just editing ~/.gitconfig.

I should have noted that I have *.patch files ignored in my ~/.gitignore so my repos aren't cluttered by patches. I figure if I have to add a patch to a repo I should add them manually anyway.

ao2's picture

Just a general suggestion about shell scripting: use $() for command substitution instead of back-quotes, the former can be nested without problems, and always quote stuff; for instance the last alias would be more robustly written as:

branch-diff = !git format-patch --stdout "$1" > "$(git cd-root)$(git branch-name).patch"

I think git forbids white spaces in branches names, so in this particular case the result is equivalent, but good practices never hurt.

Regards,
Antonio

yched's picture

For d.o issues with several patch iterations, it's common practice to append the comment number to each uploaded patch. I've always found this super useful to compare patch iterations and get a sense of chronology.
--> [issue_number]-[issue_description]-[comment-number].patch

Maybe a 2nd argument to your "branch-diff" command ?

Dave Reid's picture

Frankly, I have never cared about adding comment number into the patch name because I never know what comment number I will have until I have actually added the comment. :)

Syndicate content