Petter
Petter

Reputation: 38624

Git: continue repository history from previous commit

Let's say the master branch of my repository looks like this:

* commit 75e259944814a102d6362eaf42fd4ff09d839865
|
|       Bad commit
|
* commit 0307a5c4e623c584898c44d243a50bb02c91aa9e
|
|       Good commit 1.
|
* commit 98ef0e69e3e5cd51ff5e24c1df616703272c5a79
|
|       Good commit 1.
|

The bad commit has been pushed to the central repository and distributed. I want to continue the history from the last good commit. When I am done, I would like the history to look like

* commit 0626f793ff7ba847e7663fe88de6c6a2d597dc73
| 
|       Continuing development.
| 
| * commit 75e259944814a102d6362eaf42fd4ff09d839865
| | 
| |     Bad commit
| |
| |     
| |
* | commit 0307a5c4e623c584898c44d243a50bb02c91aa9e
|/ 
|       Good commit 2.
|
|       
|
* commit 98ef0e69e3e5cd51ff5e24c1df616703272c5a79
|
|       Good commit 1.
|

That is, the development continues from the last "good" commit. Can I safely achieve this even though the "bad" commit has been distributed among the developers?

Upvotes: 0

Views: 347

Answers (1)

Gary Fixler
Gary Fixler

Reputation: 6018

If you reset your current branch back to the last good commit with git reset --hard and then make new commits and push again, you'll be changing the history out from under any coworkers who've started working on top of the bad commit. With a small, local team who know what they're doing in git, this may not be too big a deal. You can warn everyone up front, make the change and push it, then tell them all to adjust locally. It can be a bit of a pain if people have made feature branches and merges after the bad commit, though. They'll end up having to reset things around, remake merges, etc.

You could start a new branch from back at the last good commit, but that won't remove the bad commit from the history of the current branch, so it's probably not helpful.

What you probably want to do in situations like this is simply git revert that bad commit, which makes a new commit that is the reverse of the bad commit. This won't change anyone's history. It'll just add a new commit that fixes the issue. Have a look at git help revert for details. It's fairly simple.

Upvotes: 4

Related Questions