Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Tips and tricks -- part 3: Git

Parent Feed: 

Git is great! Git is essential if you want to be a contributor to Drupal. Use it to work on a project, even without being connected to the internet. Make commits, merge branches, roll patches. Take the time to learn its very powerful features. Then use these tips to speed up your life! For example, why would you choose to type three characters when you can type one? Or, why type nine when you can type three?

Enter aliases…

Did you know about shell aliases? I have one in my ~/.profile (akin to a ~/.bashrc file for Linux users):

alias g='git '

This means I can type "g" and it really means "git".

Just like shell aliases, Git has its own form of aliases. There are commands you can type in the terminal to make configurations, but I find it easier to edit the ~/.gitconfig file directly. Begin the listing of aliases with [alias], then follow with a list of aliases, one per line. I use the following time-saving aliases in my .gitconfig file, picked up from various sources around the web:

[alias]
  s = status
  l = log --graph --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %s %C(white)- %an, %ar%Creset'
  ll = log --stat --abbrev-commit
  d = diff
  co = checkout
  a = add
  ap = add -p
  patch = format-patch --stdout HEAD~1
  rpatch = reset --hard HEAD~1

Interestingly enough, using the "g" bash alias in conjunction with an alias like "l" that might also have a bash alias defined results in a conflict. I don't know what to do about that. Oh, well. The "patch" and "rpatch" aliases are some of my favorite. Let's say I modified a contrib module and made a commit on it locally. (Just one commit.) I want to submit this commit as a patch to the module's issue queue. In the module's root directory, I type "g patch > /tmp/issue_description-1234567-4.patch". This takes the diff between the latest commit and the project before that, and formats a patch, which also includes authorship information! Next, I type "git rpatch". This removes all trace of the last commit (the one I made), so it now matches the way it still looks on drupal.org.

Git branch name in command prompt

See my post a few weeks ago about showing the Drush site alias in the command prompt. This also works with the current Git branch as well. I believe Adam's dotfiles repo has an implementation of this.

MOD stands for module

In the ~/.gitconfig file, add this:

[url "git+ssh://[email protected]/project/"]
  insteadOf = mod:

Now, when you type "git checkout mod:views" it clones views into your current directory, down from drupal.org. No need to visit the project page and hunt for the git cloning info. Make sure to replace the "jessehs" part with your drupal.org git username.
Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web