Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve: "Sorry, cannot split this hunk"? [duplicate]

Tags:

git

I've run:

git add -p

and I then want to split my hunk by typing s:

 import org.hamcrest.CoreMatchers;
-import org.junit.Ignore;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? s
Sorry, cannot split this hunk

I would like to only add import org.junit.Before; to my commit and not to remove import org.junit.Ignore;. How can I do it?

like image 954
Ortomala Lokni Avatar asked Jul 04 '19 18:07

Ortomala Lokni


1 Answers

You can manually edit the hunk by typing e and then edit the hunk so that it only contains whatever you need (note the space before all imports except the one you want to add):

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1,5 +1,5 @@
 import org.hamcrest.CoreMatchers;
 import org.junit.Ignore;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;

This way you will get:

  • +import org.junit.Before; will be in the staging area, ready to be committed (cf. git diff --cached)
  • -import org.junit.Ignore; will be there in the working directory, ready to be added to the staging area or changed (cf. git diff)

After committing, your HEAD will show (i.e. git show HEAD):

commit f2d890ec808c4ef35a4e77b7929bcfbc233101b4 (HEAD -> master)
Author: xxx
Date:   Thu Jul 4 19:49:25 2019 +0100

    Second

diff --git a/file.txt b/file.txt
index d1d0b87..462d075 100644
--- a/file.txt
+++ b/file.txt
@@ -1,5 +1,6 @@
 import org.hamcrest.CoreMatchers;
 import org.junit.Ignore;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
like image 70
user2340612 Avatar answered Sep 21 '22 18:09

user2340612