DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

git deep dive part 8: Revert the change

In the previous article, I explain how to cherry pick the commit. In this article, I explain how git revert works.

What is revert

So far, every time I change something, I just do git reset. The reason why I can do this without any concern is that I am the only one who is working on the project.

But what if others also working on the same repository? I cannot simply ignore the current commit chain anymore. So how can I revert the change? Let's imagine I want to undo the cherry-pick I did in the previous article which I mark in gray color below.
Alt Text

The proper way to revert the change is to create new commit by parenting the last commit.
Alt Text

What git revert does?

Let's run git revert to see how it works. First make sure I am in master branch and all the commits are there.

gitDeepDive> git log --oneline --graph --all
* 8618d72 (HEAD -> master) cherry-pick 367c2d0
* c36490a update docs
| * 867d90c (dev) update hello.txt
| * 367c2d0 Update news
|/
* 2adbcac (test) Add doc folder and files
* 16f1fa8 commit hello.txt

Run git revert now. I specify HEAD to specify current commit. This is a bit tricky point that I need to specify the commit I want to revert, not the commit I want to revert from.

git revert HEAD

git opens editor to modify the commit comment. I accept the default message. Once revert completed, I run git log to see the current commit chain. I see the new commit 3705a85 was created.

gitDeepDive> git log --oneline --graph --all
* 3705a85 (HEAD -> master) Revert "cherry-pick 367c2d0"
* 8618d72 cherry-pick 367c2d0
* c36490a update docs
| * 867d90c (dev) update hello.txt
| * 367c2d0 Update news
|/
* 2adbcac (test) Add doc folder and files
* 16f1fa8 commit hello.txt

I use git cat-file -p command to see the commit detail. I used git cat-file <type> and git ls-tree before, but I can do the same with git cat-file -p.

The new commit is pointing to the same objects as c36490a, rather than creating new object which makes sense as no contents were modified.

gitDeepDive> git cat-file -p 3705a85
tree f371f103be5837efc8944c6f600bab8fe4f90bc1
parent 8618d7243be90c53dddb867f118a520570f92eb5
author Kenichiro Nakamura <kenakamu@microsoft.com> 1588922393 +0900
committer Kenichiro Nakamura <kenakamu@microsoft.com> 1588922393 +0900

Revert "cherry-pick 367c2d0"

This reverts commit 8618d7243be90c53dddb867f118a520570f92eb5.
gitDeepDive> git cat-file -p f371f103
040000 tree 4090a77139baf325825d406ce817b2bc1d6df168    docs
100644 blob 1fdc81c0f8321569c31fba418e242973be35c354    hello.txt
gitDeepDive> git cat-file -p c36490a
tree f371f103be5837efc8944c6f600bab8fe4f90bc1
parent 2adbcacc0047a991956dedb4b16691ba244674b3
author Kenichiro Nakamura <kenakamu@microsoft.com> 1588865351 +0900
committer Kenichiro Nakamura <kenakamu@microsoft.com> 1588865351 +0900

update docs
gitDeepDive> git cat-file -p f371f10
040000 tree 4090a77139baf325825d406ce817b2bc1d6df168    docs
100644 blob 1fdc81c0f8321569c31fba418e242973be35c354    hello.txt

Alt Text

Summary

Revert is just create new commit pointing to existing object if I revert to previous commit. Of course if I revert from several commits back, I may encounter conflict, and in that case, git may create new object depending on how I resolve the conflict.

I explain stash/apply in the next article.

Top comments (0)