简体   繁体   中英

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 ?

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) .

No.

I suppose you could write a small shell script to do the three steps:

#!/bin/sh
set -e
git checkout master
git merge develop
git checkout develop

You might also want to add a git push origin master before the last checkout.

You may give a try to a command called git graft provide by git-extras .

It is not properly a git command, but git-extras provides a bunch of tools for working with git repositories.

In your case git graft develop master may give you the result you're looking for.

git alias that merges the current branch to master and returns to the current branch:

git config alias.merge2master '!curbr=$(git rev-parse --abbrev-ref HEAD) && git checkout master && git merge $curbr && git checkout -'

Call it as git merge2master . To make it completely parametrized:

git config alias.merge-to '!f() { !curbr=$(git rev-parse --abbrev-ref HEAD) && destbr="{$1:-master}" && git checkout $destbr && git merge $curbr && git checkout -; }; f'

Call it git merge-to or git merge-to somebranch . Default is master

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM