git 在 Sourcetree 上删除/撤消合并

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

Removing/undoing a merge on Sourcetree

gitgit-mergeatlassian-sourcetree

提问by Rameez Hussain

I made quite few changes in my project (I was working on a remote branch and not the master), I committed them and created a pull request on BitBucket and merged the branch to master. I had forgotten to push my changes after the commit. Now, after trying switching the current branch to my remote branch and reverting to the commit before the merge, I managed to get all my changes back and back them up elsewhere in my system. What I want to do now is undo the bad merge that I did. Each time I click on the merge and select "Reverse commit", I get the following error message:

我在我的项目中做了很少的更改(我在一个远程分支上而不是主分支上工作),我提交了它们并在 BitBucket 上创建了一个拉取请求并将分支合并到主分支。我忘记在提交后推送我的更改。现在,在尝试将当前分支切换到我的远程分支并恢复到合并之前的提交之后,我设法恢复了所有更改并将它们备份到系统中的其他位置。我现在想做的是撤消我所做的错误合并。每次单击合并并选择“反向提交”时,都会收到以下错误消息:

"error: Commit is a merge but no -m option was given.

fatal: revert failed"

“错误:提交是一个合并,但没有给出 -m 选项。

致命:恢复失败”

The branches look like this now:

分支现在看起来像这样:

enter image description here

在此处输入图片说明

I want to remove the merge and bring it to a state such that it doesn't say master(4 behind)anymore.

我想删除合并并将它带到一个状态,这样它就不再说master(4 behind)了。

回答by Daniel Tonon

This method is based on history deletion:

该方法基于历史删除:

  1. Check out the branch you made the mistake on
  2. Right click on the commit you want to reset the branch to
  3. Click "Reset current branch to this commit"
  4. Select "Hard" mode and click "OK"
  5. Unfortunately you need terminal to do this bit.Type git push origin name_of_branch --forceinto terminal (you may need to enter your git repo username and password for it to accept the command)
  1. 检查你犯错的分支
  2. 右键单击要将分支重置为的提交
  3. 单击“将当前分支重置为此提交”
  4. 选择“硬”模式,然后单击“确定”
  5. 不幸的是,你需要终端来做这件事。键入git push origin name_of_branch --force到终端(您可能需要输入您的混帐回购协议的用户名和密码,它接受命令)

Update:The current version of source tree supports forced pushes. That means you don't need to use the command mentioned in step 5! There is a check box visible when pushing that is labled "force". It is usually disabled.

更新:当前版本的源代码树支持强制推送。这意味着您不需要使用步骤 5 中提到的命令!推动时有一个标记为“力”的复选框可见。它通常被禁用。

How to enabled the force push checkbox:

如何启用强制推送复选框:

  1. "Tools" (in the blue bar at the top of the screen)
  2. "Options"
  3. "Git" tab
  4. "Enable Force Push" (2nd section)
  5. "OK"
  1. “工具”(在屏幕顶部的蓝色栏中)
  2. “选项”
  3. “Git”选项卡
  4. “启用强制推送”(第 2 部分)
  5. “好的”

Check that checkbox when pushing to also fix the mistake on the origin server.

推送时选中该复选框以修复源服务器上的错误。

This is probably the easiest way to do it but since it is based on history deletion, If others are working on the project, make sure to let them all know what you are doing so you don't break anyone's git repository.

这可能是最简单的方法,但由于它基于历史记录删除,如果其他人正在处理该项目,请确保让他们都知道您在做什么,这样您就不会破坏任何人的 git 存储库。

回答by Rameez Hussain

It's very unfortunate that SourceTree doesn't make it easy for you to revert merge commits (at least in Windows SourceTree 1.5.2.0). However, reverting the merge can easily be accomplished from the command line, but do you want to revert the merge by adding another commit that is the reverseof the results of the merge commit, or do you want to just remove the merge commit from history altogether?

非常不幸的是,SourceTree 不能让您轻松恢复合并提交(至少在 Windows SourceTree 1.5.2.0 中)。然而,恢复合并可以很容易地从命令行完成,但是你想通过添加另一个与合并提交结果相反的提交来恢复合并,还是只想从历史记录中删除合并提交共?

If you're not sharing your masterbranch with other people, the simplest thing to do would be to simply do a hard reset to remove the merge commit from history. However, if other people already have a copy of that merge commit, then you'll create extra work for them as they try to re-sync their copies of masterwith the rewritten version of yours.

如果您不master与其他人共享您的分支,最简单的方法就是简单地进行硬重置以从历史记录中删除合并提交。但是,如果其他人已经拥有该合并提交的副本,那么当他们尝试将他们的副本master与您的重写版本重新同步时,您将为他们创建额外的工作 。

So you need to figure out which option you want to use. I will give the steps for both, using the command line:

所以你需要弄清楚你想使用哪个选项。我将使用命令行给出两者的步骤:

Remove Commit from History

从历史记录中删除提交

git checkout master
git push origin master --force

That will overwrite the merge commit on origin/masterwith the current state of your local masterbranch, thus removing the merge commit.

这将origin/master使用本地master分支的当前状态覆盖合并提交,从而删除合并提交。

Revert Commit with another Reverse Commit

使用另一个反向提交还原提交

git checkout master
git merge origin/master
git revert -m 1 HEAD

That will bring your local masterbranch in-sync with origin/master, and then you can add a reverse commit by telling git revertthat the 1stparent is the one that should be considered the "mainline" parent, and it will create the reverse commit relative to the changes brought in from the other parent commit.

这将带来当地master的同步与分支origin/master,然后你可以添加一个反向告诉提交git revert了1母公司是应该考虑的“主线”父之一,它会造成相反的承诺相对变化从另一个父提交带来。

If later on you decide that you want to bring in the changes from the other parent again, then you'll need to add another reversion commit that reverts the reversion commit that you just made (a technique known as "reverting the revert").

如果稍后您决定再次从另一个父级引入更改,那么您需要添加另一个还原提交来还原您刚刚进行的还原提交(一种称为“还原还原”的技术)。

See also:

也可以看看:

  1. Undo a Git merge?.
  2. How do I “un-revert” a reverted Git commit?.
  3. git-revert(1) Manual Page.
  4. How to revert a faulty merge.
  1. 撤消 Git 合并?。
  2. 如何“取消还原”已还原的 Git 提交?.
  3. git-revert(1) 手册页
  4. 如何恢复错误的合并

回答by Fernando Bonet

I found another way without command lines, a bit ugly but does the job.

我找到了另一种没有命令行的方法,有点难看,但可以完成工作。

So, do a hard reset to the commit you want to roll back, go to the project folder (Using Finder on Mac or Explorer on Windows) and make a copy of the whole folder. What you have inside this 'Copy' folder is the point you want to be at the end of this process.

因此,对要回滚的提交进行硬重置,转到项目文件夹(在 Mac 上使用 Finder 或在 Windows 上使用资源管理器)并复制整个文件夹。您在此“复制”文件夹中的内容是您希望在此过程结束时的位置。

Well... go back to source tree and then checkout the Head (the latest commit into your remote), then navigate again to the project folder, be sure you can see hidden folders because you must be able to see a folder called ".git"

好吧...返回源代码树,然后检出 Head(最新提交到您的遥控器),然后再次导航到项目文件夹,确保您可以看到隐藏的文件夹,因为您必须能够看到名为“.混蛋”

project folder

项目文件夹

Delete everything BUT ".git" from your current project, it means your current project has nothing inside but the folder called ".git", then navigate to your 'Copy' folder and copy everything but ".git" folder and paste the content inside your current project (the one with ".git" folder only)

从当前项目中删除除“.git”之外的所有内容,这意味着您当前的项目中只有名为“.git”的文件夹,然后导航到“复制”文件夹并复制除“.git”文件夹之外的所有内容并粘贴内容在您当前的项目中(仅带有“.git”文件夹的项目)

Done. Go to source tree and commit the changes, your project is exactly where you wanted and all changes removed.

完毕。转到源代码树并提交更改,您的项目正是您想要的位置,并且删除了所有更改。

The end.

结束。



Obs1: Delete the "Copy" folder now to clean your pc from dirty files.

Obs1:现在删除“Copy”文件夹以清除电脑上的脏文件。

Obs2: This process don't remove your changes from Git, the commits will be there, what you are doing is deleting your changes and committing it.

Obs2:这个过程不会从 Git 中删除您的更改,提交将在那里,您正在做的是删除您的更改并提交它。