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?
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 # ...
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.
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.
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?"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With