46

I have 3 branches.

 master [ Live Server] \ stage [ Stage Server Where we test changes; merge commits ] \ Dev [ Local Machine ] 

I would like to downstream the changes to. Each of these branches are set to tracking each other.

Normally, to downstream the changes i do this:

git checkout stage && git merge master

Then i checkout dev and i do the same

git checkout dev && git merge stage

Then push them all: git push origin --all

Is there a way to downstream those changes without checking out into each branch?

I maybe using the wrong terminology. I'm not totally sure if i'm using upstream/downstream terminology correctly.

15

3 Answers 3

84
+200

You can indeed "merge" a branch B into branch A without having to check out branch A, but only if it's a fast-forward merge.

You can use a refspec with fetch to do the "merge". If merging branch B into branch A using git merge would result in a fast-forward merge, then you can do the following without having to checkout A:

git fetch <remote> B:A 

If <remote> is . then this will operate from within the local repo.

The Documentation

The above matches the refspec format

git fetch <remote> <source>:<destination> 

From the documentation for git fetch (emphasis mine):

The remote ref that matches <src> is fetched, and if <dst> is not empty string, the local ref that matches it is fast-forwarded using <src>.

See Also

  1. Git checkout and merge without touching working tree

  2. Merge, update, and pull Git branches without using checkouts

  3. Merging without changing the working directory

  4. Merging Branches Without Checkout

Sign up to request clarification or add additional context in comments.

5 Comments

(For the lazy who don't want to dig through the other answers): You can also use a . in place of the the remote for operation on local branches.
This is such a good find. I've haphazardly passed this in the documentation a handful of times and never picked up on this.
@CarrieKendall it's really easy to miss. It's been a long while, but I think that the only reason I discovered this was because my bash auto-completed the refspec one day, and being curious, I decided to hit enter and see what would happen...
Here's a practical example: git fetch . foo:master this will do a fast-forward merge of foo into master no matter what branch you are on. The . refers to the local repository.
git fetch . origin/main:main will fast-forward merge origin/main into main if possible. This is useful knowledge for folks trying to "pull all branches".
18
+500

Enter git-forward-merge:

Without needing to checkout destination, git-forward-merge <source> <destination> merges source into destination branch.

https://github.com/schuyler1d/git-forward-merge

Comments

6

You can't merge into a branch in the general case without having it checked out. There's a good reason for this, however. You need the proper working tree in order to denote and resolve merge conflicts.

1 Comment

This isn't a new comment, just editing out the link to my answer because, in hindsight, linking to my own answer like that was distasteful. Edited comment: This isn't entirely correct. If the merge would result in a fast-forward merge, then there will be no conflicts, and thus it's not necessary to have a working copy, and thus it's possible to merge without having to have the destination branch checked-out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.