Merging to a branch in git without switching to it
I have an application running in a git repository on a branch (say dev
). The application modifies the content in some the repository and commits them. I now have to merge these changes into another branch (say master
) but the snag is that I don't want to git checkout master
before doing this. Is there some way to say "merge current branch into master"?
The "master" in your case appears to be "fast-forwardable". You could "push" the branch to master.
cd /path_to_dir_with_no_branch_switch/
git push . appbranch:master
This works very well for me when wanting to merge two branches without having to checkout either of them:
git fetch . <modified branch>:<destination branch>
Other options are described here.
One possible solution would be to clone make another working tree from the same local repo and perform the merge in said working tree (with main
checked out), being able to solve potential conflicts there.
Creating a separate working tree is much faster than making a separate clone (which was my original suggestion back in 2011): it uses the Git 2.5+ (July 2015) git worktree
command.
Then, at a later date, when able to switch to main
in the local repo (your first working tree, where you are working), you would then restore the updated main
branch from the separate working tree.
Alternatively, Bernardo Dal Corno suggests in the comments a 2014 tool/script: schuyler1d/git-forward-merge
from Schuyler Duveen.
git forward-merge
creates a temporary Git index file and working directory to be used only for the merge, without interfering with the actual index file and working directory.
(Unless the merge is a fast-forward, in which case the merge is done trivially by a local push.)