Search code examples
gitrebase

One-liner for git rebase and git pull


Is there a one-liner for pulling a branch and rebasing to it?

For example, when working on a feature branch, one checks out master, pulls changes, and then checkouts to feature before running git rebase master.

# currently on feature branch
git checkout master
git pull
git checkout feature
git rebase master

What's the one-liner for this?


Solution

  • To pull and rebase on the same branch:

    git pull --rebase
    

    To pull from master and rebase feature onto master:

    git pull --rebase origin master:master
    

    NOTE: I'm not sure if this works for non-fast-forward rebases.


    If you're doing it frequently, you can create a shell alias:

    gprb() {
      local branch_a="$1"
      local branch_b="$2"
      git checkout "$branch_a" && \
      git pull && \
      git checkout "$branch_b" && \
      git rebase "$branch_a"
    }
    

    Usage:

    gprb master feature