Git Skinny

Posted by Lucas Hedding on June 8, 2013

 like Git way better than all the alternatives. I learn more things about it all the time. Even today, I wondered if there was an easier way to create nicely named patches automatically; and there is. Here's a run down of the more common tips and tricks I've learned.

  • git init .

    Create a git repo.

  • git add .

    (Adds all files recursively into git.

  • git add -u

    (only adds or "stages" removed/changed files.

  • git commit -m "Commit message"

    I tend to use double quotes so I'm able to use contractions in the commit message.

  • git commit --amend

    Adds more changes to the most recent commit. Use if you haven't pushed yet and missed a couple files or changes in the last commit.

  • git rebase

    Stages all the changes at the tip of HEAD.

  • git rebase -i

    Interactive rebase to allow for picking and choosing what to pull to the HEAD and re-arrange into multiple commits that make more sense.

  • git apply --index

    Applys a patch and stages all newly created files to the index so you don’t “miss” committing a file.

  • .gitignore

    All files and paths mentioned in here are ignored by git.

  • gitk

    UI on Linux/Mac for viewing and searching the tree.

Common git aliases (from ~/.gitconfig):

[alias]
        diff = diff --word-diff
        a = apply --index
        st = status
        ci = commit
        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

The last three lines in my aliases creates a patch named after the branch. This is particularly useful because I tend to name my branches after the Drupal project and issue description and number. So the patches names are cleanly built as so: htmlpurifier-fixFormError-123456.patch. To use it from the branch, simply call git branch-diff 8.x

Another thing I like to do is remove old branches from time to time. Here's a one liner I use: git branch -D $(git branch | awk '{ if ($0 !~ /7.x|8.x|7.x-1.x|7.x-2.x|8.x-1.x|8.x-2.x/) printf "%s", $0 }')

Are you looking to build or customize a Drupal site? Write us about your project, and we’ll get back to you within 48 hours.

If you want to learn more about why Drupal is the most widely used open-source content management system in the world, visit drupal.org/about.