给定一个已经使用commit提交,然后使用revert恢复的更改,那么撤消该恢复的最佳方法是什么?

理想情况下,这应该通过一个新的提交来完成,这样就不会重写历史。


如果你还没有按这个改变,git重置-硬头^

否则,还原还原完全没问题。

另一种方法是git结帐HEAD^^——。然后git add -A && git提交。

Git cherry-pick <原始提交sha> 将复制原始提交,本质上是重新应用提交

还原将做同样的事情,与一个更混乱的提交消息: Git revert <commit sha of revert>

这两种方式都允许您在不覆盖历史记录的情况下进行git推送,因为它会在还原后创建一个新的提交。 当输入commit sha时,通常只需要前5或6个字符: Git选择6bfabc

如果你不喜欢“还原还原”这个想法(特别是当这意味着丢失多次提交的历史信息时),你可以随时查看git文档中关于“还原错误合并”的内容。

给定下面的起始情况

 P---o---o---M---x---x---W---x
  \         /
   A---B---C----------------D---E   <-- fixed-up topic branch

(W是归并M的初始还原;D和E修复了你最初损坏的特性分支/提交)

你现在可以简单地重放提交A到E,这样它们都不“属于”被还原的合并:

$ git checkout E
$ git rebase --no-ff P

分支的新副本现在可以再次合并到主分支:

   A'---B'---C'------------D'---E'  <-- recreated topic branch
  /
 P---o---o---M---x---x---W---x
  \         /
   A---B---C----------------D---E

或者你可以用git checkout -b <new-branch> and git cherry-pick <commit> the before to the and git rebase to drop revert commit。像以前一样发送拉请求。

还原提交就像git中的任何其他提交一样。意思是,你可以恢复它,如:

git revert 648d7d808bc1bca6dbf72d93bf3da7c65a9bd746

显然,这只有在更改被推送时才有意义,特别是当您不能强制推送到目标分支时(这对于您的主分支来说是个好主意)。如果更改还没有被推送,只需要选择,还原或简单地删除还原提交。

在我们的团队中,我们有一个规则,在主分支中提交的revert提交上使用revert,主要是为了保持历史记录的干净,这样你就可以看到哪个提交还原了什么:

     7963f4b2a9d    Revert "Revert "OD-9033 parallel reporting configuration"
     "This reverts commit a0e5e86d3b66cf206ae98a9c989f649eeba7965f.
                    ...
     a0e5e86d3b6    Revert "OD-9055 paralel reporting configuration"
     This reverts commit 648d7d808bc1bca6dbf72d93bf3da7c65a9bd746.
                ...
     Merge pull request parallel_reporting_dbs to master* commit 
    '648d7d808bc1bca6dbf72d93bf3da7c65a9bd746'

这样,你就可以追溯历史,弄清楚整个故事,甚至那些不了解遗产的人也可以自己找出答案。然而,如果你选择或改变内容,这些有价值的信息就会丢失(除非你在评论中包含它)。

显然,如果一个提交多次还原和重新还原,就会变得非常混乱。

我是这样做的: 如果分支my_branchname包含在被还原的合并中。我想取消my_branchname:

我首先从my_branchname执行git checkout -b my_new_branchname。 然后我做一个git重置——软$COMMIT_HASH,其中$COMMIT_HASH是my_branchname第一次提交之前的提交哈希(见git日志) 然后我新建一个commit git commit -m "Add back reverted changes" 然后我向上推新的分支git推origin new_branchname 然后我对新分支提出了拉请求。

恢复还原程序就可以了

例如,

如果abcdef是你的提交,ghijkl是你还原abcdef时的提交,那么运行:

git revert ghijkl

这将还原还原

要返回在提交后恢复的非暂存和暂存更改:

git reset HEAD@{1}

恢复所有未执行的删除操作:

git ls-files -d | xargs git checkout --

我有一个问题,有人做了一个恢复到主到我的分支,但我需要能够合并它,但问题是,恢复包括我所有的提交。 让我们看看这种情况,我们从M1创建了我们的特征分支,我们在M3中合并了我们的特征分支,并在RM3中恢复它

M1 -> M2 -> M3 -> M4- > RM3 -> M5
 \.         /
  F1->F2 -

如何使F2能够合并到M5?

git checkout master
git checkout -b brach-before-revert
git reset --hard M4
git checkout master
git checkout -b new-feature-branch
git reset --hard M1 
git merge --squash brach-before-revert

在最初意外删除所有文件的恐慌之后,我使用以下方法来取回我的数据

git reset HEAD@{1}         
git fsck --lost-found      
git show 
git revert <sha that deleted the files>

I saw responses include the command git reset --hard HEAD without any caution. You should be careful with that command because of the option --hard. It resets your index and your remote repo but mostly, it also resets your local repo and all commits that were not pushed to the remote yet will be lost, both from your local repo and index. Never use that flag --hard unless you are sure you also want to reset all your local work from the current commit till the hash you chose. If anyway you did it by mistake, run git reflog to retrieve your ~hash then git reset --hard ~hash to recover your files.

在我的例子中,我需要在恢复后提交更改,然后才能在没有失败的情况下选择原始提交。

git commit -m "Make changes (original commit)."
git revert <original commit hash>
git commit -m "Revert original commit."
git cherry-pick <original commit hash>

如果你错误地做了一个还原:

git revert <commit-id>

你只需要运行:

git cherry-pick <commit-id>

我必须使用这个命令来提交我的更改。

你可以通过以下命令获取你的提交ID:

git log --pretty=format:"%h - %an, %ar : %s"