2

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?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
eja
  • 4,677
  • 3
  • 21
  • 30
  • https://stackoverflow.com/questions/2472254/when-should-i-use-git-pull-rebase – ANIK ISLAM SHOJIB Jan 06 '22 at 07:42
  • @eja, not exactly a one-liner but, if you do want to save on some keystrokes, you can use [git-flow](https://git-flow.readthedocs.io/en/latest/features.html). `git flow feature rebase` Of course, this is assuming that you have a practice of keeping your configured development branch up to date. Or, you can set up a single alias for a combination of the mentioned commands. – SNikhill Jan 06 '22 at 07:57
  • 2
    `git fetch && git rebase origin/master` is equivalent to what’s in the question. If you adapt your workflow to not involve local branches unnecessarily, it’s a lot simpler. – AD7six Jan 06 '22 at 08:03
  • https://stackoverflow.com/a/65817305/7976758 – phd Jan 06 '22 at 08:07
  • If it is possible that `git checkout master; git pull` is *not* a fast-forward situation (i.e., it would do a real merge), then there is no one-liner for this case. – j6t Jan 06 '22 at 08:07
  • 1
    Well, sometimes wanting a oneliner means that you want something shorter _to type_ without deeper realizing the core problem and probably rethinking it with first-class terms. I'd remove the master branch locally, and would simply do `git fetch origin` to fetch the changes from the remote repository, and then rebase the current branch on top of the base branch by simple `git rebase origin/master`. Not really a oneliner, but using first-class git commands without doing a checkout and not creating more and more commands just to make them oneliners. – terrorrussia-keeps-killing Jan 06 '22 at 08:09
  • @fluffy @AD7six by this you basically suggest not keeping a local `master` branch and rebasing to `origin/master`, did I get this right? this also means that when creating a `feature` branch, you'd branch out from `origin/master` instead the local `master`? – eja Jan 06 '22 at 08:47
  • 2
    Kinda. You very likely will still want a local master branch - but its existence/absence is not a factor here because it’s not referenced. Likewise `git checkout -b new-thing origin/master` creates a new branch from the remote branch, and again is indifferent to the existence of a local master branch. – AD7six Jan 06 '22 at 09:10
  • @eja Yeah, it might be an option if you find it convenient. The branch you off your new branch from is simply a pointer to a commit, and if the commit is identified by a hash or a name (`master`, or `origin/master`, any ref name), you can still rebase on top of it. Of course, if you need master locally, it can be still in your local repository, there is nothing wrong with it, but you'll need constantly update it like you do in your post. I do the same, especially when I have many topic branches locally off the master branch. There is never a (simple) oneliner. – terrorrussia-keeps-killing Jan 06 '22 at 09:33
  • 2
    A bit of off-topic: I personally don't really like oneliners, aliases or git-scripts: they introduce author's rational model of thinking of the problem, and they are not that "scalable" (on a new machine, pair programming, remote shell, etc, one has either copy his/her git toolkit, or use git builtin-ins if the former is not possible). I use those for more or less complex tasks (like making git work piped to other tools), and I never remember my whole git toolkit and there is no documentation for it online, of course. This is why I prefer using git first-class commands whenever I can. – terrorrussia-keeps-killing Jan 06 '22 at 09:39
  • Got it, thanks a lot guys, also that's actually an awesome point for why not use one-liners. – eja Jan 06 '22 at 10:37

1 Answers1

3

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
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • doesn't `git pull --rebase` only rebase you to the remote tracked branch ie origin/feature-branch? – eja Jan 06 '22 at 07:46
  • @eja : not if you explicitly specify which branch you pull from the remote. – LeGEC Jan 06 '22 at 09:01