127

I was wondering if anyone would have a better suggestion for keeping a feature branch in sync with it's parent branch.

We typically have multiple feature branches that are being worked on at a time, each derived from our develop branch. It's fairly common for feature branches to be merged into develop a couple times a day.

In order to stay on top of changes (i.e. resolve conflicts) I find I need to keep the feature branches I'm actively working on up to date with develop.

To do this I run these commands a couple times a day:

git checkout develop
git pull
git checkout feature/foo 
git merge develop 
git push

The last git push I usually only do if I'm working with someone else on the feature branch.

Is there a better or more convenient way to do this?

hafichuk
  • 10,351
  • 10
  • 38
  • 53

3 Answers3

117

Well, git is really set up for wrapping things you do frequently into a script and then calling the script instead. There isn't a huge number of variations on the above. They all will require pulling the upstream branch, and then integrating your changes with the upstream's. Some things that may shorten it for you might be:

git checkout feature/foo
git fetch --all
git merge --into-name=develop origin/develop
git rebase develop

That way you can simply pull all the upstream branches in one shot without switching to the other branch. And rebasing is possibly an operation you may prefer over merging, but that's frequently a personal choice depending on how you want the history to look.

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69
  • 4
    as an addition, if you name any script git-somename and put it on the path, then you can call it directly as a normal git command, so making the above 3 line script "git-featurefoo" would allow you to type `git featurefoo` to perform all the actions in one command. – Mark Fisher Mar 06 '12 at 17:51
  • Mark is right. And not only that, but you might check out `git aliases` too. – Wes Hardaker Mar 06 '12 at 17:56
  • afaik there is also the possibility to automatically rebase after a pull although I never used it. Maybe that's the way to go for you? – nanoquack Mar 06 '12 at 18:17
  • 13
    A word of warning, rebase is best used on local feature branches that haven't been pushed, see https://www.atlassian.com/git/tutorials/merging-vs-rebasing#the-golden-rule-of-rebasing – Richard Pursehouse Sep 11 '18 at 09:36
  • 1
    `git pull --all` didn't pull `develop` for me :s I had to manually `checkout develop`, `pull`, then `checkout feature/foo` again and `rebase develop` in order to get it working. – alete Oct 16 '20 at 19:27
  • 3
    @alete: `git pull --all` is ... not exactly *wrong* but the `--all` flag here is pointless as it doesn't do anything normal people will ever find *useful*. It's highly misleading: it makes people think that this is a "pull all branches" operation, and it's not. Don't use `--all` here. – torek Mar 25 '22 at 20:19
  • 1
    Wow. This answer has over 100 upvotes, is the accepted answer, and is pretty much incorrect because as written, the `pull` has no effect on `develop`. My edit would be to change the pull to `git fetch` and change the rebase to `git rebase origin/develop`, but 10 years later, I'm kind of afraid to touch it. Hehe. – TTT Jul 15 '22 at 19:32
  • 1
    I think at the time I wrote this merge --all may have actually done more merging of branches. But my memory isn't that good. Anyway, I think to reduce the number of commands run (which is sort of the goal) and to assume that the author wanted to track develop locally too, the right hack while in a different branch is `git merge --into-name=develop origin/develop` before doing the rebase. So check the above changes which I believe are correct. – Wes Hardaker Jul 15 '22 at 20:11
  • Actually- I don't think that works as intended. Maybe something like `git fetch origin develop:develop` ? Or `git branch -f develop origin/develop` ? – TTT Jul 15 '22 at 20:44
  • What's "git merge --into-name=develop origin/develop" for? :) – Alexis Delrieu Apr 06 '23 at 08:58
  • It lets you do a `git merge` without switching to the branch you want to merge into. – Wes Hardaker Apr 12 '23 at 20:20
40

Why not make a script that would run the commands?

Create merge-develop.sh with text

#!/bin/bash
git checkout develop
git pull
git checkout feature/$1
git merge develop 
git push

Then simply run merge-develop.sh foo.

Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
6

Another option is to fetch with target, which allows you to pull develop without checkout:

git checkout feature/foo
git fetch origin develop:develop
git rebase develop
nomadoda
  • 4,561
  • 3
  • 30
  • 45