git 如何用master覆盖某个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25561379/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:20:47  来源:igfitidea点击:

How to overwrite a certain branch with master

gittortoisegit

提问by Hyman

I have two branches devand master. I want my dev branch to be completely overwritten by masterbranch as I know master is latest. How can I do that using TortoiseGit UI?

我有两个分支devmaster. 我希望我的 dev 分支被master分支完全覆盖,因为我知道 master 是最新的。如何使用 TortoiseGit UI 做到这一点?

I tried to do merge using TortoiseGit but that would result in many conflicts.

我尝试使用 TortoiseGit 进行合并,但这会导致许多冲突。

Please let me know the answers in Tortoise GIT only as I am only using UI and not familiar with actual GIT commands.

请让我知道 Tortoise GIT 中的答案,因为我只使用 UI 并且不熟悉实际的 GIT 命令。

回答by RomainValeri

Disclaimer : this is not a TortoiseGit solution, but a CLI one, I hope it'll help someone anyway.

免责声明:这不是 TortoiseGit 解决方案,而是 CLI 解决方案,我希望它无论如何都能帮助某人。

Since nobody suggested it already, let's also note this quite simple way to do it :

由于没有人已经建议它,让我们也注意这个非常简单的方法:

git branch -f dev master

It's a short way to set the first given ref (here : dev) to the same point where the second ref (here : master) is currently pointing to. (As a sidenote, they're not "linked" in any way after this command, and stay independant for all intents and purposes.)

这是将第一个给定的 ref(此处为:dev)设置为第二个 ref(此处为:master)当前指向的同一点的简便方法。(作为旁注,在此命令之后,它们不会以任何方式“链接”,并且出于所有意图和目的保持独立。)

As it rewrites the history of branch dev, if it has a remote counterpart you'll have to push it with force :

因为它重写了分支 dev 的历史,如果它有一个远程副本,你必须用 force 推送它:

git checkout dev
git push -f origin HEAD

In any case, if you happen to have other people working with you on that branch, any of the solutions here are ways to rewrite history so be sure to discuss this with them beforehand!

无论如何,如果您碰巧在该分支上有其他人与您一起工作,这里的任何解决方案都是重写历史的方法,因此请务必事先与他们讨论!

回答by MrTux

In TortoiseGit you have several options depending on your scenario:

在 TortoiseGit 中,您有多种选择,具体取决于您的情况:

  1. You are on the devbranch: Go to the log dialog, open the context menu on the master branch entry and select "Reset to" and choose "hard".

  2. You are not on the devbranch: Create a new branch with the name devchoose the masterbranch as origin and select "Override branch if exists" (or go to the log dialog, open the context menu on the dev branch and select delete branch and open the context menu on the masterbranch and create a new branch called dev).

  1. 您在dev分支上:转到日志对话框,打开主分支条目上的上下文菜单,然后选择“重置为”并选择“硬”。

  2. 您不在dev分支上:创建一个名为的新分支,dev选择master分支作为原点并选择“如果存在则覆盖分支”(或转到日志对话框,打开开发分支上的上下文菜单并选择删除分支并打开master分支上的上下文菜单并创建一个名为dev)的新分支。

回答by EpicPandaForce

I have noidea if this is right or wrong, but I seem to have overwritten another branch with master like this:

知道这是对还是错,但我似乎用 master 覆盖了另一个分支,如下所示:

git push origin master:otherbranch

And then when I switched I had to pull, and it was good to go?

然后当我切换时我不得不拉,这很好吗?