如何将当前分支合并到另一个分支
我有两个分支,master 和 dev。我总是从事开发工作,只有在代码被批准用于生产用途后才将代码签入主分支。当我这样做时,我必须执行以下操作:
git checkout master
git merge dev
git checkout dev
这非常冗长,并且由于我经常这样做,所以我想尽量减少它。有没有一个 git 命令可以用来从当前的分支 dev 合并到另一个分支 master,而无需先签出 master 分支?也许是这样的:
git merge dev to master
那就太棒了。我查看了 git 文档,没有看到任何内容。
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:
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 merge dev to master
would be awesome. I looked through the git documentation and didn't see anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
@zerome 当前得票最高的答案是一个很好的答案,但有点不必要的冗长。
在 git 存储库的基础上,您可以执行以下操作:
git push 。 dev:master
一个可以在树中任何地方工作的更通用的解决方案是:
The current highest-voted answer by @zerome is a good one, but is a bit needlessly verbose.
In the base of your git repo you can just do this:
git push . dev:master
A more generalised solution that would work anywhere in the tree would be:
你最好的选择是只使用一个别名,放置在你的全局 gitconfig (
~/.gitconfig
) 中:这样你就可以从任何存储库调用它
Your best bet would be to just use an alias, placed in your global gitconfig (
~/.gitconfig
):so that you can invoke it from any repository as
对 Jefromi 别名进行一点修改,不需要您输入当前分支。
所以你可以像这样使用它:
git merge-to dev
。这将切换到 dev 分支,将其与 CURRENT 合并,然后再切换回来。
例如,假设您在
master
分支上,它将把 master 合并到 dev 中,而您仍然在 master 上。它肯定会进入我的点文件:)
A little modification from Jefromi alias that doesn't require you to type in the current branch.
So you use it like:
git merge-to dev
.This will switch over to the
dev
branch, merge it with CURRENT and then will switch back.For example, assuming you are on
master
branch, it will merge the master into dev and you will still be on the master.It definitely goes to my dotfiles :)
这是旧的,但是......
结合上面@kevin-lyda 和@dmytrii-nagirniak 的解决方案。此别名将当前分支合并到指定分支。它使用远程方法并使用 git 命令来获取上下文。
使用方式如下:
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.
To be used like:
将当前分支合并到另一个分支而不检查另一个分支:
快进合并
这非常简单。根据定义,快进合并仅意味着分支指针在提交树中向前移动。因此,您需要做的只是模拟:
提示:如果您执行了
git fetch
并且在origin/master
中有新提交,则可以移动master
> 不签出的分支使用:通过合并提交合并
这并不总是可能的。要创建合并提交,您必须执行合并操作。要执行合并操作,您应该在另一个分支中进行不在当前分支中的提交。
如果您确实在
master
分支中有提交,但在dev
分支中没有,您可以:说明:
将
master
合并到临时分支并启动提交消息编辑器。如果您希望合并提交看起来像,您已将dev
分支合并到master
中,请从以下位置进行编辑:对此:
提示:您可以使用
-m "Mergebranch 'dev'"
而不是-e
来加快速度。dev
分支。这仍然会影响你的工作树,但影响很小。它不会将树一直回滚到原始
master
的状态,只是为了再次引入开发更改。 有些人可能不在乎,但对其他人来说可能很重要。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 simulate that:
Tip: If you did a
git fetch
and you have new commits inorigin/master
, you can move themaster
branch without checking out using: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 not indev
branch, you can:Explanation:
Merge
master
into the temporary branch and launch commit message editor. If you want the merge commit to look like you had merged thedev
branch intomaster
, edit it from this:to this:
Tip: You can use
-m "Merge branch 'dev'"
instead of-e
to be quicker.master
branch pointer to point to the merge commit.dev
branch.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.很多时候,您来自想要将当前分支合并到的分支。在这种情况下,你可以这样做:
git co - && git merge @{-1}
例如:
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}
for example:
我的解决方案与其他答案类似,但有以下区别:
set -ex
,因此每个命令都会被打印,如果命令失败,函数会立即退出: git merge ,它确保制表符补全适用于某些 shell 设置(例如 < a href="https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/gitfast/git-completion.bash#L26-L30" rel="nofollow noreferrer">
gitfast< /code> 来自 oh-my-zsh)
My solution is similar to the other answers, with the following differences:
set -ex
so each command is printed and if command fails function exits right awaygit merge
: git merge
which makes sure tab-completion works with some shell setups (e.g.gitfast
from oh-my-zsh)1. 为本地存储库添加远程别名,例如:(
或在 Windows 上
git remote add self C:\path\to\your\repository
)2 . 推送到自我远程,例如:
1. Add a remote alias for your local repository, ex:
(Or on windows
git remote add self C:\path\to\your\repository
)2. Push to the self remote, ex: