git:更好的方式为git恢复没有额外的恢复提交 [英] git: better way for git revert without additional reverted commit

查看:92
本文介绍了git:更好的方式为git恢复没有额外的恢复提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在远程+本地分支中提交了一个提交,并且我想将该提交从历史记录中提取出来,并将其中的一部分放入自己的分支中。

基本上,现在我有:

  D --- E --- F --- G master 

code>

我想:

  E --- G话题
/
D主人

这应该是无论是在我的本地还是在(只有一个,称为原产地)远程仓库中。



哪种方法最简单?



另外,还有其他人已经克隆了该回购,并且已经检出了主分支。如果我想在远程回购中做这样的改变,那么'git pull'是否也能为他们获得同样的状态?

如果你已经发布,那么你是对的,你不想重写 master 的历史。你想要的是发布一个提交到主控,让它回到状态它在 D ,同时保留当前的历史记录,以便其他用户可以合并或重定义他们的工作如果你计划在未来某个时候将主题合并到中, master 那么你可能还想做的是在 master topic之间建立一个新的公共基础,以便在后续合并 topic 时,不会丢失在 master 中返回的提交。 。最简单的方法是在'undo'提交之上进行'重做'提交,将 master 重置为其原始状态并基于新的<$ c

 #checkout master分支(当前位于G)$ c $> topic  
git checkout master

#将索引重置为我们想要master看起来像
的样子git reset D

#将分支指针移回到哪里它应该是,让索引
#看起来像D
git reset --soft HEAD @ {1}

#为这个头部提交一个提交(D') master分支
git commit -m暂时还原E,F和G

#根据master创建新的主题分支。
#我们将它放在master的顶部,并且'撤消'
#提交,以确保后续的master->主题
#或topic-> master的合并撤消合并。
git checkout -b主题

#恢复撤消提交,进行重做提交(G')。
git revert HEAD

作为替代方案,您可以提交E',F'和G'分别重做每个部分,但是因为E,F和G已经出现在您发布的历史记录中,所以如果您只是引用撤消提交并说该提交正在撤消,那么可能会更容易理解。这就是 git revert ,无论如何。



基本上你知道的是这个。

  D  -  E  -  F  -  G  -  D'< - 主
\
\
G'< - topic

重要的是你没有改写历史并且主题基于主,因此合并不会意外地应用任何撤消提交。您现在可以安全地将 master 主题推送到远程存储库。


I have a commit in a remote+local branch and I want to throw that commit out of the history and put some of them into an own branch.

Basically, right now I have:

           D---E---F---G master

And I want:

             E---G topic
            /
           D master

That should be both in my local and in the (there is only one, called origin) remote repository.

Which is the cleanest way to get that?

Also, there are also other people who have cloned that repo and who have checked out the master branch. If I would do such a change in the remote repo, would 'git pull' work for them to get also to the same state?

解决方案

If you've published then you are right that you don't want to re-write the history of master. What you want is to publish a commit to master that brings it back to the state that it was at D while retaining its current history so that other users can merge or rebase their work easily.

If you are planning at some point in the future to merge topic into master then what you probably also want to do is make a new common base between master and topic, so that when you do subsequently merge topic, you don't lose the commits that were reverted in master. The easiest way to do this is by making a 'redo' commit on top of the 'undo' commit that resets master back to its original state and basing the new topic branch on top of that.

# checkout master branch (currently at G)
git checkout master

# Reset the index to how we want master to look like
git reset D

# Move the branch pointer back to where it should be, leaving the index
# looking like D
git reset --soft HEAD@{1}

# Make a commit (D') for the head of the master branch
git commit -m "Temporarily revert E, F and G"

# Create the new topic branch based on master.
# We're going to make it on top of master and the 'undo'
# commit to ensure that subsequent merges of master->topic
# or topic->master don't merge in the undo.
git checkout -b topic

# Revert the undo commit, making a redo commit (G').
git revert HEAD

As an alternative you could have made commits E', F' and G' redoing each part separately but as E, F and G are already in your published history it's probably more understandable if you just reference the 'undo' commit and say that that commit is being undone. This is what git revert does, anyway.

Essentially what you know have is this.

D -- E -- F -- G -- D'      <-- master
                     \
                      \
                        G'  <-- topic

The important things are that you have not rewritten history and topic is based on master so merges won't accidentally apply any 'undo' commits. You can now safely push both master and topic to your remote repository.

这篇关于git:更好的方式为git恢复没有额外的恢复提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆