Helpful git command chaining

I am currently working in a team of two using a git repository. In that environment, you want to have a clean history which can only achieved with the git rebase command. Using this small code snippet, I can fetch the changes online, rebase my code according to the changes fetched and push my changes automatically. If an error occurs, the command will abort.

git fetch && git rebase && git push

However, this does not work if you still have files locally which are not committed, e.g. some changed files in your working directory. For this purpose, the stash can be used. The git stash is a stack for storing changes temporarily. By saving the uncommitted changes before fetching and rebasing, you can integrate them back afterwards.

git stash save && git fetch && git rebase && git push && git stash pop

What git tricks do you use?

PS: The commands above work on *nix and Windows.

Advertisements

3 thoughts on “Helpful git command chaining

  1. Hey Simon,

    if I may nitpick: “&&” is a shell operator (bash in my case) and not a git feature. Thus, you can use it to chain arbitrary expressions, e.g. “pdflatex mydoc.tex && acroread mydoc.pdf” (I use this a lot.)
    Another nitpick: “git stash” will stash the changes, no need for the “save”.

    Here’s a few git “tricks” I often use:
    1) the somewhat lesser known “git shortlog” that summarizes “git log”
    2) “git log –grep test” to grep for a regular expression
    3) aliases that allow me to work around old habits (e.g. “st”)

  2. Hi Felix,

    hehe, yeah, I know that. The title may be a bit misleading, but it probably creates higher click rates. 😉

    That git stash defaults to git stash save is good to know, however, I like to make scripts explicit.

    For git shortlog, I did a blog post a while ago. This is really a nice feature. 🙂

    I use git mostly in smartgit to commit and in the console to do fetching and rebasing. In smartgit, I can select which changes I want in the next commit more easily.

  3. This actually changed my life. I always have some local file modified, which means to pull the latest code I have to:
    git stash
    git pull origin master
    git stash apply

    Using this post, plus bash aliases, I’ve turned this to:
    alias ums=’git stash && git pull origin master && git stash apply’
    And can now type:
    ums

    I use this at least a couple of times a day, every day. You’ve save me hundreds of future keystrokes my friend. Thanks!

    Ps. ums = “update my shit”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s