Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rewind master branch to specific commit in master's history

Tags:

git

I have the following recent history in my 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. In hindsight, I should have create a separate branch to do this debugging, but I did it all in master. Now, in commit 7661a06, I have fixed the bug.

I want to move (rewind?) the master branch (or pointer) so that it is located at commit f1c6f05. After I do this, then I can generate a patch from the diff between commits 7661a06 and 62fe9db and apply it to f1c6f05. As a result, a portion of my git log should look like this:

* 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?

like image 372
synaptik Avatar asked Jun 14 '13 14:06

synaptik


People also ask

How do I reset a branch to a specific commit in origin?

Make sure you are on the branch where the commit is. I'm doing this on master. Then use git reset –hard <commit-hash> to set the current branch HEAD to the commit you want.


2 Answers

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. 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:

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:

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. Once that's done, do this to merge that change back into 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.

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. 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...

like image 121
twalberg Avatar answered Sep 21 '22 10:09

twalberg


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

If you are happy with the history of devConsolidate after that:

git checkout master
git merge devConsolidate
like image 21
Peter Lundgren Avatar answered Sep 22 '22 10:09

Peter Lundgren