1

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?

torek
499k71 gold badges762 silver badges888 bronze badges
asked Nov 16, 2021 at 7:45

1 Answer 1

4

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.

answered Nov 16, 2021 at 9:26
Sign up to request clarification or add additional context in comments.

3 Comments

i want just to remove the changes that was added with the second commit. Do you know a solution?
i ask you, because there could be many changed files, and in that situation to detect what file should be removed and what should be left?
As I say in the beginning of my answer: there is no magic solution therefor. You have two (sets of) changes that build on one another, and if you remove the first one, git will have trouble applying the second set of changes. No matter how you try to tackle this problem you still have those two sets of changes that interact together, and you want to keep the second one. You'll have to go through a conflict...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.