1

I'm trying to wrap my head around git revert.

I'm coming from a Clearcase background where to undo a bad commit, you would either:

  1. check in the old versions of the files on top of the bad committed files (only file level commits on Clearcase)
  2. Delete the element that was the commit
  3. In the version tree, merge the previous version into the current version to create a new version that is identical to the one prior to the latest commit.

Now in my mind, a git revert is an automatic equivalent to the manual work in (3).

My question is: Can we say that a git revert merges the old previous commits on top of the reverted commit?

hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • See http://stackoverflow.com/questions/28166547/what-are-the-differences-between-revert-amend-rollback-and-undo-a-com/28166763#28166763 – jub0bs Mar 02 '15 at 10:28

3 Answers3

1

In short

git revert creates the inverse of the commit you want to undo. Then you commit that new change-set into the repository.

See section Unmodifying a Modified File in 2.4 Git Basics - Undoing Things.

So, no, you can't say:

Can we say that a git revert merges the old previous commits on top of the reverted commit?

because you can revert any commit in the history of the repository.

Example

Assume you have this history:

C1 <-- C2 <-- C3 (HEAD)

That means, if you want to revert commit C2, you don't commit the "old previous commit" which would be C1. Because then you'd end up with

C1 <-- C2 <-- C3 <-- C1 (HEAD)

which you don't. You end up with

C1 <-- C2 <-- C3 <-- C2' (HEAD)

with C2' being the inverse of C2, and C3 still being part of your history.

Illustration

Here is an illustration of the above explanation, taken from here.

enter image description here

Lernkurve
  • 20,203
  • 28
  • 86
  • 118
0

Not exactly. git revert will create a new commit object for every commit you choose to revert, and each commit object will be the exact opposite of each commit you chose to revert respectively.

Meaning, if you git revert C1, then git will create C1R which exactly reverses all changes made by C1.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • This is a great concise clear answer that captures the essence of the question with a minimum of words. (Sorry - the other guy had a diagram). – hawkeye Mar 02 '15 at 11:56
0

Don't forget that those ClearCase option are file by file.

git revert works by commit (which is a bit like an UCM activity), meaning it will revert all the files referenced in that commit.

For reverting only a specific file, you have other options in "Reset or revert a specific file to a specific revision using Git?".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250