Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge with master without checkout to master

Every time I need to merge develop with master I do:

git checkout master

git merge develop

Sometimes I forget to switch out of master. Due to this, I mistakenly make changes to code while still on master. It can be messy. I've committed to master a few times by mistake.

Is there a way to merge to master without switching first to master?

like image 236
IMB Avatar asked Jan 30 '19 19:01

IMB


People also ask

How do I force merge into master?

run the command git flow release finish <version_number> it will merge everything into master and change the branch to master. run the command git push to publish the changes to the remote master.

How do I manually merge a branch to master?

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch.

How do I push to a different branch without checkout?

Push Branch to Another Branch In some cases, you may want to push your changes to another branch on the remote repository. In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.


1 Answers

If you want to do a real merge into master, no. A real merge needs, at least potentially, a work-tree in which to do work. It also uses the index, which needs to match the current branch around the outside of the operation (see git worktree add method below).

If you want to do a fast-forward operation (not an actual merge), it is possible. For instance:

git push . develop:master

(note the lack of + sign or --force option) will attempt to fast-forward your master to the same commit as your develop. Use HEAD to mean the current branch:

git push . HEAD:master

These only work if the fast-forward is possible. If not, you will get an error of the form:

 ! [rejected]              [name] -> master (non-fast-forward)

which tells you that you need a work-tree whose branch is master in which to run git merge.

To do that without changing the branch of your current work-tree, use git worktree add, if your Git is at least 2.5 (preferably at least 2.15). For instance, suppose you are in the top level of your repository, on branch feature/X:

git worktree add ../master   # NB: assumes ../master is available as a directory name
cd ../master
git merge feature/X
...                 # do what it takes to complete the merge here

cd -                # return to your main repository
rm -rf ../master    # remove added worktree
git worktree prune  # clean up list of added worktrees

The added work-tree has its own index (and its own HEAD) so git merge now has an index and work-tree in which to do its job, or leave its mess for you to fix, whichever actually occurs (see kostix's comment).

like image 135
torek Avatar answered Sep 17 '22 21:09

torek