简体   繁体   English

Git 与 master 合并,无需结帐到 master

[英]Git merge with master without checkout to master

Every time I need to merge develop with master I do:每次我需要merge developmaster merge ,我都会这样做:

git checkout master

git merge develop

Sometimes I forget to switch out of master .有时我忘记切换出master Due to this, I mistakenly make changes to code while still on master .因此,我在master上错误地更改了代码。 It can be messy.它可能很乱。 I've committed to master a few times by mistake.我已经错误地承诺master了几次。

Is there a way to merge to master without switching first to master ?有没有办法在不先切换到master情况下mergemaster

If you want to do a real merge into master, no.如果你想真正合并到 master,不。 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).它还使用索引,它需要匹配操作外部的当前分支(参见下面的git worktree add方法)。

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 . (注意缺少+符号或--force选项)将尝试将您的master快进到与您的develop相同的提交。 Use HEAD to mean the current branch:使用HEAD表示当前分支:

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 .这告诉你你需要一个工作树,它的分支是master来运行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).要在不更改当前工作树的分支的情况下执行此操作,请使用git worktree add ,如果您的 Git 至少为 2.5(最好至少为 2.15)。 For instance, suppose you are in the top level of your repository, on branch feature/X :例如,假设您位于存储库的顶级分支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) .添加的工作树有它自己的索引(和它自己的HEAD )所以git merge现在有一个索引和工作树来完成它的工作,或者让你修复它的混乱,以实际发生的为准(见kostix 的评论) .

No.不。

I suppose you could write a small shell script to do the three steps:我想你可以写一个小的 shell 脚本来完成三个步骤:

#!/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.您可能还想在最后一次结帐之前添加一个git push origin master

You may give a try to a command called git graft provide by git-extras .您可以尝试使用git-extras提供的名为git graft的命令。

It is not properly a git command, but git-extras provides a bunch of tools for working with git repositories.它不是一个 git 命令,但git-extras提供了一堆用于处理 git 存储库的工具。

In your case git graft develop master may give you the result you're looking for.在你的情况下git graft develop master可能会给你你正在寻找的结果。

git alias that merges the current branch to master and returns to the current branch:将当前分支合并到 master 并返回到当前分支的 git 别名:

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

Call it as git merge2master .将其称为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 .称之为git merge-togit merge-to somebranch Default is master默认是master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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