git 如何将当前分支合并到另一个分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3672073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge the current branch into another branch
提问by Chris
I have two branches, master and dev. I always work on dev and only check code into the master branch once it's been approved for production use. When I do so, I have to do the following:
我有两个分支,master 和 dev。我总是在开发上工作,并且只有在批准用于生产用途后才将代码签入主分支。当我这样做时,我必须执行以下操作:
git checkout master
git merge dev
git checkout dev
That's awfully verbose, and since I do it frequently, I'd like to minimize it. Is there any one git command I can use to merge from my current branch dev to the other branch master without having to checkout the master branch first? Something maybe like:
这太冗长了,因为我经常这样做,所以我想尽量减少它。是否有任何一个 git 命令可以用来从我当前的分支 dev 合并到另一个分支 master 而不必先检出 master 分支?可能是这样的:
git merge dev to master
would be awesome. I looked through the git documentation and didn't see anything.
会很棒。我查看了 git 文档,没有看到任何内容。
采纳答案by zerome
1.Add a remote alias for your local repository, ex:
1.为您的本地存储库添加远程别名,例如:
git remote add self file:///path/to/your/repository
(Or on windows git remote add self C:\path\to\your\repository
)
(或在窗户上git remote add self C:\path\to\your\repository
)
2.Push to the self remote, ex:
2.推到自遥控器,例如:
git push self dev:master
回答by Kevin Lyda
The current highest-voted answer by @zerome is a good one, but is a bit needlessly verbose.
@zerome 目前投票最高的答案是一个很好的答案,但有点不必要的冗长。
In the base of your git repo you can just do this: git push . dev:master
在你的 git repo 的基础上,你可以这样做: git push . dev:master
A more generalised solution that would work anywhere in the tree would be:
适用于树中任何位置的更通用的解决方案是:
git push $(git rev-parse --show-toplevel) dev:master
回答by Cascabel
Your best bet would be to just use an alias, placed in your global gitconfig (~/.gitconfig
):
最好的办法是使用别名,放置在全局 gitconfig ( ~/.gitconfig
) 中:
[alias]
merge-to = "!f() { git checkout && git merge && git checkout -; }; f"
so that you can invoke it from any repository as
以便您可以从任何存储库调用它
git merge-to master dev
回答by Dmytrii Nagirniak
A little modification from Jefromi alias that doesn't require you to type in the current branch.
Jefromi 别名的一些修改不需要您输入当前分支。
So you use it like: git merge-to dev
.
所以,你使用它像:git merge-to dev
。
This will switch over to the dev
branch, merge it with CURRENT and then will switch back.
这将切换到dev
分支,将其与 CURRENT 合并,然后切换回来。
For example, assuming you are on master
branch, it will merge the master into dev and you will still be on the master.
例如,假设您在master
分支上,它会将 master 合并到 dev 中,而您仍然在 master 上。
It definitely goes to my dotfiles :)
它肯定会进入我的 dotfiles :)
[alias]
merge-to = "!gitmergeto() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git checkout && git merge $tmp_branch && git checkout $tmp_branch; unset tmp_branch; }; gitmergeto"
回答by tField
This is old, but...
这是旧的,但是...
Combining the solutions from @kevin-lyda and @dmytrii-nagirniak above. this alias merges the current branch into the specified branch. It uses the remotes method with and uses git commands to get the context.
结合上面@kevin-lyda 和@dmytrii-nagirniak 的解决方案。此别名将当前分支合并到指定分支。它使用 remotes 方法和 git 命令来获取上下文。
[alias]
merge-to = "!gitmergeto() { git push \"`git rev-parse --show-toplevel`\" `git rev-parse --abbrev-ref HEAD`:; } && gitmergeto"
To be used like:
像这样使用:
git merge-to other-branch-name
回答by ADTC
To merge the current branch into another branch without checking out the other branch:
将当前分支合并到另一个分支而不检出另一个分支:
Fast-forward merge
快进合并
This is really easy. By definition, a fast-forward merge simply means the branch pointer is moved ahead in the commit tree. So all you need to do is just simulatethat:
这真的很容易。根据定义,快进合并只是意味着分支指针在提交树中向前移动。所以你需要做的就是模拟:
git branch -f master dev
Caveats:This assumes that
master
points to a commit that is also indev
branch or some other branch. If it doesn't, you risk losing work!Unlikegit merge
which will create a merge commit (or complain) when fast-forward is not possible, this method silently forcesthe branch pointer to point to another commit.This also assumes you're the only one working on the repo, and/or you know what you're doing.
注意事项:这假设
master
指向也在dev
分支或其他某个分支中的提交。否则,您将面临失去工作的风险!与git merge
在无法进行快进时创建合并提交(或抱怨)不同,此方法静默地强制分支指针指向另一个提交。这也假设您是唯一一个在 repo 上工作的人,和/或您知道自己在做什么。
Tip:If you did a git fetch
and you have new commits in origin/master
, you can move the master
branch without checking out using:
提示:如果你做了 agit fetch
并且你有新的提交origin/master
,你可以移动master
分支而无需签出使用:
git branch -f master origin/master
Merge via merge commit
通过合并提交合并
This is not always possible. To create a merge commit, you have to do a merge operation. And to do a merge operation, you should have commits in the other branch that are not in the current branch.
这并不总是可能的。要创建合并提交,您必须执行合并操作。并且要进行合并操作,您应该在其他分支中提交不在当前分支中的提交。
If you do have commits in the master
branch which are notin dev
branch, you can:
如果你有在提交master
分支,它是不是在dev
分行,您可以:
Disclaimer:This is merely a proof-of-concept, just to show it's sometimespossible to do a merge to the other branch without checking out. If you would want to use it everyday, you probably want to make an alias for it using shell redirection or make a shell script for it. Then again, you can also make a shell script for the shorter process shown in the question.
免责声明:这只是一个概念验证,只是为了表明有时可以在不检查的情况下合并到另一个分支。如果您想每天使用它,您可能想使用 shell 重定向为其创建别名或为它创建一个 shell 脚本。再说一次,您还可以为问题中显示的较短过程制作一个 shell 脚本。
git checkout -b temp
git merge --no-ff -e master
git branch -f master temp
git checkout dev
git branch -D temp
Explanation:
解释:
- Check out a temporary branch that points to the same commit as current branch.
Merge
master
into the temporary branch and launch commit message editor. If you want the merge commit to look likeyou had merged thedev
branch intomaster
, edit it from this:Merge branch 'master' into temp
to this:
Merge branch 'dev'
Tip:You can use
-m "Merge branch 'dev'"
instead of-e
to be quicker.- Update the
master
branch pointer to point to the merge commit. - Check out the
dev
branch. - Force delete the temporary branch.
- 签出一个临时分支,该分支指向与当前分支相同的提交。
合并
master
到临时分支并启动提交消息编辑器。如果您希望合并提交看起来像您已将dev
分支合并到 中master
,请从以下位置对其进行编辑:Merge branch 'master' into temp
对此:
Merge branch 'dev'
提示:您可以使用
-m "Merge branch 'dev'"
而不是-e
更快。- 更新
master
分支指针以指向合并提交。 - 检查
dev
分支。 - 强制删除临时分支。
This still touches your working tree, but minimally so. It doesn't roll back the tree all the way to state of the original master
just to bring in the development changes once again. Some may not care, but for others it may be important.
这仍然会触及您的工作树,但至少如此。它不会将树完全回滚到原始状态,master
只是为了再次引入开发更改。有些人可能不在乎,但对其他人来说可能很重要。
回答by artm
My solution is similar to the other answers, with the following differences:
我的解决方案与其他答案类似,但有以下区别:
- the function is split into multiple lines for readability
- the function calls
set -ex
so each command is printed and if command fails function exits right away - alias passes its arguments except the first (target branch) to
git merge
- the function includes a null command
: git merge
which makes sure tab-completion works with some shell setups (e.g.gitfast
from oh-my-zsh)
- 该函数被分成多行以提高可读性
- 函数调用
set -ex
所以打印每个命令,如果命令失败,函数立即退出 - 别名将除第一个(目标分支)以外的参数传递给
git merge
- 该函数包括一个空命令
: git merge
,它确保制表符补全适用于某些 shell 设置(例如gitfast
来自 oh-my-zsh)
[alias]
merge-to = "!f() { : git merge ; \
set -ex ; \
local this=$(git rev-parse --abbrev-ref HEAD) ; \
local target= ; \
shift ; \
git checkout $target ; \
git merge $this \"$@\" ; \
git checkout $this ; \
} ; f"
回答by Tieme
A lot of times you're coming from the branch you would like to merge the current branch into. In that case you could do:
很多时候,您来自希望将当前分支合并到的分支。在这种情况下,你可以这样做:
git co - && git merge @{-1}
git co - && git merge @{-1}
for example:
例如:
git checkout somebranch // (while on master)
// add some commits
git co - && git merge @{-1} // will merge somebranch into master