Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fast-forward other tracking branches in git?

Tags:

git

We are working with the model of a single remote repository that all of us use. We branch for new features and reintegrate into a trunk branch. Our workflow is that we should integrate from trunk into our working branches when other features are integrated into the trunk.

As such, it's not uncommon for us to do:

(branch) $ git commit -a -m"blah blah blah" (branch) $ git fetch  # origin/trunk is updated (branch) $ git checkout trunk (trunk) $ git pull  # trunk is fast-forwarded to current version of origin/trunk. (trunk) $ git checkout branch (branch) $ git merge trunk (branch) $ git push 

I don't like the "git checkout trunk/git pull/git checkout branch" cycle. It's usually combined with Visual Studio complaining that all my files and projects have changed on disk, and should it reload them. For both checkouts. And the pull. And the merge. The merge is unavoidable, but because of how git works, it should be able to do the fast-forward on trunk without actually needing to check it out.

But I don't know the command, and my google-foo has failed me on this. Anyone know how?

like image 721
Blaise Pascal Avatar asked Feb 28 '11 21:02

Blaise Pascal


People also ask

What is Fast forward pull in git?

With git pull --ff-only , Git will update your branch only if it can be “fast-forwarded” without creating new commits. If this can't be done (if local and remote have diverged), git pull --ff-only simply aborts with an error message: $ git pull --ff-only upstream master # ...

What does it mean to fast forward a branch?

Fast-forward merges literally move your main branch's tip forward to the end of your feature branch. This keeps all commits created in your feature branch sequential while integrating it neatly back into your main branch.


2 Answers

I think the easiest way to avoid the git checkout trunk, git pull, git checkout branch cycle is to use this answer:

git fetch upstream trunk:trunk 

This does exactly what you want - fast-forward your local branch trunk to the remote branch's HEAD.

like image 127
Oliver Avatar answered Sep 23 '22 02:09

Oliver


Do you really need to update a local trunk branch?

Just fetch origin/trunk and merge it directly in the branch you are working on.
And that is Mark Longair's advice: git: fetch and merge, don’t pull.


Oliver mentions in his answer (upvoted):

git fetch upstream trunk:trunk 

As he comments:

you can skip the merge and simply fast-forward, using a one-liner.

It does fast-forward the local branch trunk to the remote branch's HEAD.

See more at "git: update a local branch without checking it out?"

like image 35
VonC Avatar answered Sep 21 '22 02:09

VonC