Saurav Ojha
Saurav Ojha

Reputation: 155

How to revert an old Git commit without impacting the later commits?

I want to revert an older commit in a series of commits on a particular branch in GIT without impacting the later changes. i.e I want only the changes pushed as a part of a particular commit(earlier) to go, keeping the changes done later intact.

Also, How would the situation change if this branch has been merged with another branch later?

I can try this, but not sure if it would help.

git revert <CommitId>

Upvotes: 1

Views: 245

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 22067

git revert <commitXYZ> will create a new commit on top of the current branch, containing the contrary of the changes introduced in <commitXYZ>. If the original (faulty) commit added a line, the revert commit will be the deletion of that line.

Let's say B' is the commit created when you do git revert B :

A --- B --- C --- D --- B'

Here A, B, C and D commits will stay untouched (yes, even B). The changes from B will appear no more at B' state, but commit B is still in the history.

Upvotes: 1

Related Questions