简体   繁体   English

Git将master分支回滚到master的历史记录中的特定提交

[英]Git rewind master branch to specific commit in master's history

I have the following recent history in my git repo. 我的git repo中有以下近期历史记录。

* 7661a06 (HEAD, origin/devConsolidate, devConsolidate) Fix seg fault; OCBA intermediary compares by value() now also
| * 0bbe038 (origin/master, master) Flawed work no seedShift
|/  
* 62fe9db Turn on OCBA_DEBUG; nan/inf values in OCBA prior to crash
* 71298c8 Turn on OCBA (non-intermediary); seg fault occurs
* 3693904 No OpenMP, no OCBA; no memory leaks on valgrind
* 9d5686c Disable OpenMP threads
* 80bbc3b Debug (-O0) build now throws exception also
* e148013 Post convert simulation4_NEJMdisutilities [] to at()
* 66cfba9 Post convert OCBA [] to at()
* 32db3be Pre convert OCBA [] to at()
* 4a9f25b Prep for debugging
* 907e88b Found error (vector out of bounds); need to fix from here
* ca6c639 Implement elapsed, iteration, and max time in OCBA
* db68c15 Fix SEG FAULT; OCBA now working (with intermediary)
* f1c6f05 GA now uses OCBA; GA/OCBA params from config file; produces SEG FAULT
| * 3b16dcf (origin/genCRNgraph, genCRNgraph) Generate QALYs test and control independent-sampling
|/  
*   001eff2 Merge branch 'OCBAdev'

All the commits subsequent to (descendants of) f1c6f05 were done to debug a problem. f1c6f05 (后代)之后的所有提交都是为了调试问题。 In hindsight, I should have create a separate branch to do this debugging, but I did it all in master . 事后来说,我应该创建一个单独的分支来完成这个调试,但我在master完成了所有这些。 Now, in commit 7661a06 , I have fixed the bug. 现在,在提交7661a06 ,我修复了该错误。

I want to move (rewind?) the master branch (or pointer) so that it is located at commit f1c6f05 . 我想移动(倒回?) master分支(或指针),使其位于提交f1c6f05 After I do this, then I can generate a patch from the diff between commits 7661a06 and 62fe9db and apply it to f1c6f05 . 在我这样做之后,我可以从提交7661a0662fe9db之间的差异生成补丁并将其应用于f1c6f05 As a result, a portion of my git log should look like this: 因此,我的git日志的一部分应如下所示:

* 7661a06 (origin/devConsolidate, devConsolidate) Fix seg fault; OCBA intermediary compares by value() now also
| * 0bbe038 Flawed work no seedShift
|/  
* 62fe9db Turn on OCBA_DEBUG; nan/inf values in OCBA prior to crash
           .
           .        
           .
* db68c15 Fix SEG FAULT; OCBA now working (with intermediary)
| * <HASH> (HEAD, origin/master, master) Fix problem and rewind master
|/
* f1c6f05 GA now uses OCBA; GA/OCBA params from config file; produces SEG FAULT
| * 3b16dcf (origin/genCRNgraph, genCRNgraph) Generate QALYs test and control independent-sampling
|/  
*   001eff2 Merge branch 'OCBAdev'

What commands would I use to achieve all of this? 我会用什么命令来实现这一切? Specifically, how would I: (1) move master, (2) generate the patch, and (3) apply the patch? 具体来说,我将如何:(1)移动主,(2)生成补丁,(3)应用补丁?

Since you have already pushed master to origin ( origin/master is at 0bbe038 ), this will cause problems for anyone who has already fetched or pulled from origin since your push. 由于您已经将master推送到originorigin/master位于0bbe038 ),这将导致自推送以来已经从origin取出或拉出的任何人出现问题。 So, you may need to communicate and coordinate with others what is going on so they can adjust as necessary. 因此,您可能需要与其他人沟通和协调正在进行的操作,以便他们可以根据需要进行调整。

With that in mind, though, here's how you would do it: 考虑到这一点,这里是你如何做到这一点:

1) Move master back to f1c6f05 : 1)将主机移回f1c6f05

git checkout master
git reset --hard f1c6f05
git push -f origin master

2+3) Rather than generating and applying a patch, assuming 7661a06 is the only relevant commit for your fix, you can simply do this: 2 + 3)假设7661a06是您的修复的唯一相关提交,而不是生成和应用补丁,您可以简单地执行此操作:

git checkout -b bug-fix-branch master
git cherry-pick 7661a06

It's possible you may get some minor conflicts, depending on exactly what all the "debug" commits actually did, and whether they touched anything nearby what the 7661a06 commit changed, but these should be pretty easy to fix. 您可能会遇到一些小的冲突,具体取决于所有“调试”提交实际上做了什么,以及它们是否触及了7661a06提交更改的附近任何内容,但这些应该很容易修复。 Once that's done, do this to merge that change back into master: 完成后,执行此操作将该更改合并回master:

git checkout master
git merge bug-fix-branch
git branch -d bug-fix-branch    # optional, maybe not desired
git push origin master

At this point, everyone else will need to fetch/pull the new master branch, and restart whatever work they have pending. 此时,其他人都需要获取/拉取新的master分支,并重新启动它们待处理的任何工作。

If the disruption to others is unacceptable, you can alternatively do this: 如果对其他人的干扰是不可接受的,您可以选择这样做:

git checkout master
git revert f1c6f05..62fe9db
git push master

This will create new commits on master that undo the effects of all those "debug" commits, but won't actually remove them from history. 这将在master上创建新的提交,撤消所有这些“调试”提交的效果,但实际上不会从历史记录中删除它们。 So the ending content of your working directory should end up looking the same, but the history will look very different. 因此,工作目录的结束内容应该看起来一样,但历史记录看起来会有很大不同。 In this case, there should be no disruption to others, so depending on your environment/workflow, this is probably the better option, but it does document for posterity the mistakes made, and may cause confusion/problems down the road if you ever need to use git bisect to track down new bugs or something... 在这种情况下,不应该对其他人造成干扰,因此根据您的环境/工作流程,这可能是更好的选择,但它确实为后代记录了所犯的错误,如果您需要,可能会引起混乱/问题。使用git bisect来追踪新的bug或其他东西......

git checkout master
git reset --hard f1c6f05
git rebase --onto master 62fe9db devConsolidate

If you are happy with the history of devConsolidate after that: 如果您对devConsolidate的历史感到满意:

git checkout master
git merge devConsolidate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM