Search code examples
gitmerge

Merge branches without checking out branch


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.


Solution

  • 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