如何将当前分支合并到另一个分支

发布于 2024-09-18 11:22:03 字数 345 浏览 5 评论 0原文

我有两个分支,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

野侃 2024-09-25 11:22:04

@zerome 当前得票最高的答案是一个很好的答案,但有点不必要的冗长。

在 git 存储库的基础上,您可以执行以下操作: git push 。 dev:master

一个可以在树中任何地方工作的更通用的解决方案是:

git push $(git rev-parse --show-toplevel) 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:

git push $(git rev-parse --show-toplevel) dev:master
白况 2024-09-25 11:22:04

你最好的选择是只使用一个别名,放置在你的全局 gitconfig (~/.gitconfig) 中:

[alias]
    merge-to = "!f() { git checkout $1 && git merge $2 && git checkout -; }; f"

这样你就可以从任何存储库调用它

git merge-to master dev

Your best bet would be to just use an alias, placed in your global gitconfig (~/.gitconfig):

[alias]
    merge-to = "!f() { git checkout $1 && git merge $2 && git checkout -; }; f"

so that you can invoke it from any repository as

git merge-to master dev
泪痕残 2024-09-25 11:22:04

对 Jefromi 别名进行一点修改,不需要您输入当前分支。

所以你可以像这样使用它:git merge-to dev

这将切换到 dev 分支,将其与 CURRENT 合并,然后再切换回来。

例如,假设您在 master 分支上,它将把 master 合并到 dev 中,而您仍然在 master 上。

它肯定会进入我的点文件:)

[alias]
  merge-to = "!gitmergeto() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git checkout $1 && git merge $tmp_branch && git checkout $tmp_branch; unset tmp_branch; }; gitmergeto"

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

[alias]
  merge-to = "!gitmergeto() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git checkout $1 && git merge $tmp_branch && git checkout $tmp_branch; unset tmp_branch; }; gitmergeto"
不必你懂 2024-09-25 11:22:04

这是旧的,但是......

结合上面@kevin-lyda 和@dmytrii-nagirniak 的解决方案。此别名将当前分支合并到指定分支。它使用远程方法并使用 git 命令来获取上下文。

[alias]
    merge-to = "!gitmergeto() { git push \"`git rev-parse --show-toplevel`\" `git rev-parse --abbrev-ref HEAD`:$1; } && gitmergeto"

使用方式如下:

git merge-to other-branch-name

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.

[alias]
    merge-to = "!gitmergeto() { git push \"`git rev-parse --show-toplevel`\" `git rev-parse --abbrev-ref HEAD`:$1; } && gitmergeto"

To be used like:

git merge-to other-branch-name
我只土不豪 2024-09-25 11:22:04

将当前分支合并到另一个分支而不检查另一个分支:

快进合并

这非常简单。根据定义,快进合并仅意味着分支指针在提交树中向前移动。因此,您需要做的只是模拟

git branch -f master dev

注意事项:这假设 master 指向也在 dev 分支或其他分支中的提交。如果没有,你就有失去工作的风险!git merge 不同,git merge 会在无法快进时创建合并提交(或抱怨),此方法 >默默地强制分支指针指向另一个提交。

这还假设您是唯一在该存储库上工作的人,并且/或者您知道自己在做什么。

提示:如果您执行了 git fetch 并且在 origin/master 中有新提交,则可以移动 master > 不签出的分支使用:

git branch -f master origin/master

通过合并提交合并

这并不总是可能的。要创建合并提交,您必须执行合并操作。要执行合并操作,您应该在另一个分支中进行不在当前分支中的提交。

如果您确实在 master 分支中有提交,但在 dev 分支中没有,您可以:

免责声明:这只是一个概念验证,只是为了表明有时可以在不签出的情况下合并到另一个分支。如果您想每天使用它,您可能需要使用 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

说明:

  1. 签出指向与当前分支相同的提交的临时分支。
  2. master 合并到临时分支并启动提交消息编辑器。如果您希望合并提交看起来像,您已将 dev 分支合并到 master 中,请从以下位置进行编辑:

    将分支 'master' 合并到 temp 中
    

    对此:

    合并分支“dev”
    

    提示:您可以使用 -m "Mergebranch 'dev'" 而不是 -e 来加快速度。

  3. 更新 master 分支指针以指向合并提交。
  4. 查看 dev 分支。
  5. 强制删除临时分支。

这仍然会影响你的工作树,但影响很小。它不会将树一直回滚到原始 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:

git branch -f master dev

Caveats: This assumes that master points to a commit that is also in dev branch or some other branch. If it doesn't, you risk losing work! Unlike git merge which will create a merge commit (or complain) when fast-forward is not possible, this method silently forces the 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.

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:

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 not in dev branch, you can:

Disclaimer: This is merely a proof-of-concept, just to show it's sometimes possible 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.

git checkout -b temp
git merge --no-ff -e master
git branch -f master temp
git checkout dev
git branch -D temp

Explanation:

  1. Check out a temporary branch that points to the same commit as current branch.
  2. Merge master into the temporary branch and launch commit message editor. If you want the merge commit to look like you had merged the dev branch into master, 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.

  3. Update the master branch pointer to point to the merge commit.
  4. Check out the dev branch.
  5. Force delete the temporary 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.

少女净妖师 2024-09-25 11:22:04

很多时候,您来自想要将当前分支合并到的分支。在这种情况下,你可以这样做:

git co - && git merge @{-1}

例如:

git checkout somebranch      // (while on master)

// add some commits

git co - && git merge @{-1}  // will merge somebranch into master

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:

git checkout somebranch      // (while on master)

// add some commits

git co - && git merge @{-1}  // will merge somebranch into master
執念 2024-09-25 11:22:04

我的解决方案与其他答案类似,但有以下区别:

  • 为了可读性,该函数被分成多行
  • 函数调用set -ex,因此每个命令都会被打印,如果命令失败,函数会立即退出
  • 别名将除第一个(目标分支)之外的参数传递给 git merge
  • 该函数包含一个空命令 : 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)
[alias]
  merge-to = "!f() { : git merge ; \
      set -ex ; \
      local this=$(git rev-parse --abbrev-ref HEAD) ; \
      local target=$1 ; \
      shift ; \
      git checkout $target ; \
      git merge $this \"$@\" ; \
      git checkout $this ; \
    } ; f"

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)
[alias]
  merge-to = "!f() { : git merge ; \
      set -ex ; \
      local this=$(git rev-parse --abbrev-ref HEAD) ; \
      local target=$1 ; \
      shift ; \
      git checkout $target ; \
      git merge $this \"$@\" ; \
      git checkout $this ; \
    } ; f"
唔猫 2024-09-25 11:22:03

1. 为本地存储库添加远程别名,例如:(

git remote add self file:///path/to/your/repository

或在 Windows 上git remote add self C:\path\to\your\repository

2 . 推送到自我远程,例如:

git push self dev:master

1. Add a remote alias for your local repository, ex:

git remote add self file:///path/to/your/repository

(Or on windows git remote add self C:\path\to\your\repository)

2. Push to the self remote, ex:

git push self dev:master
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文