Home > Software engineering >  Git revert cover multiple commits
Git revert cover multiple commits

Time:11-17

I added 3 commits in my branch:

  • '3' -> c3425f (added: <h3>1</h3>)
  • '2' -> c34252 (added: <h2>2</h2>)
  • 'first commit' -> c3425d (added: <h1>1</h1>)

At the end i got this structure:

   <h1>1</h1>
   <h2>2</h2>
   <h3>3</h3>

Now I want to exclude the c34252 commit. For this I used git revert c34252.

Doing this my IDE show me this:

    <h1>1</h1>
<<<<<<< HEAD
    <h2>2</h2>
    <h3>3</h3>
=======
>>>>>>> 

So, it suggest me that the second commit contains <h2>2</h2><h3>3</h3>, but should contain only <h2>2</h2>. So, why git suggest me that the second commit contains both changes? What git command should be used to exclude only the second commit?

CodePudding user response:

TLDR

There is no magic command for this. You need to explain git what you want exactly.

more in depth

When you look at your commit "2":

git show c34252

you will see that the commit contains a diff like this:

diff --git a/file b/file
index ace652e..6d8ba51 100644
--- a/file
    b/file
@@ -1  1,2 @@
 <h1>1<h1>
 <h2>2<h2>

So when you run

git revert c34252

git tries to apply the inverse of this. It tries to find

<h1>1<h1>
<h2>2<h2>
(end of file)

and then to remove the h2 line. However instead it finds

<h1>1<h1>
<h2>2<h2>
<h3>3<h3>
(end of file)

so it is not sure about what you want to obtain: remove just the h2 line, or from h2 untill (end of file). You need to explain git what you want, there is no way around this AFAIK. This kind of conflicts often arise when you edit the same line or lines that immediately follow each other.

  •  Tags:  
  • git
  • Related