Your Edited Hunk Does Not Apply Edit Again

Week 5

"Splitting upwards commits the easy way"

Taking commits that footling scrap farther

Sometimes, putting everything in a single commit just is not a good idea. Imagine you have pulled in number of updates to your working directory. You may want to divide these up. It is true that you could simply git add simply the files y'all want to include in the commit. However, what happens when you have change iv or five different things in the same file, and you want to divide that commit up into five different commits.

In that location are ii ways you can approach this. The outset is to re-create the file in question out of the working directory, reset the working re-create dorsum to the last committed and copy your changes in line by line. This can be fourth dimension consuming and frustrating and when you are working on many files, it can be totally impractical. What nosotros need is a style to include or exclude sure lines of a file.

To demonstrate this we are going to create a new branch called fantasy and we are going to make several changes to a few files. We are then going to show how the same process can exist achieved by using both the GUI and the command line.

So let us start by creating our branch and making some changes as shown beneath.

john@satsuki:~/coderepo$ git checkout -b fantasy
Switched to a new branch 'fantasy'
john@satsuki:~/coderepo$ echo "This is line i" > newfile1
john@satsuki:~/coderepo$ repeat "This is line ii" >> newfile1
john@satsuki:~/coderepo$ echo "This is line 3" >> newfile1
john@satsuki:~/coderepo$ repeat "This is line 4" >> newfile1
john@satsuki:~/coderepo$ echo "This is a new line" >> newfile2
john@satsuki:~/coderepo$ echo "This is another new line" >> newfile2


Let us at present simply run a git diff to see exactly what the changes are.

john@satsuki:~/coderepo$ git diff
diff --git a/newfile1 b/newfile1
index 44640b2..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -one,2 +i,4 @@
-A new file
-and some more than awesome changes
+This is line 1
+This is line ii
+This is line 3
+This is line iv
diff --git a/newfile2 b/newfile2
index 3545c1d..40efcce 100644
--- a/newfile2
+++ b/newfile2
@@ -i,2 +1,iv @@
Another new file
and a new awesome characteristic
+This is a new line
+This is another new line
john@satsuki:~/coderepo$


At present, nosotros could simply practice git commit -a and exist done with it, just what if nosotros actually wanted to split this information up into four commits? We will introduce a new parameter to our git add tool from before. We are going to use the git add -p or git add --patch. This will allow us to interactively edit the hunks before they are committed. To begin with, let the states run git add -p and encounter what it is we demand to do.

john@satsuki:~/coderepo$ git add together -p
unequal --git a/newfile1 b/newfile1
index 44640b2..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -1,2 +one,four @@
-A new file
-and some more crawly changes
+This is line ane
+This is line two
+This is line 3
+This is line 4
Stage this hunk [y,northward,q,a,d,/,due east,?]?


We are given the options of y,n,q,a,d,/,e,?. At outset glance, this may seem rather daunting. Let usa choose the ? and see what help is presented to us.

Stage this hunk [y,due north,q,a,d,/,e,?]? ?
y - stage this hunk
due north - exercise not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - exercise not phase this hunk nor whatsoever of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - get out this hunk undecided, encounter side by side undecided hunk
J - get out this hunk undecided, see next hunk
k - get out this hunk undecided, see previous undecided hunk
Yard - get out this hunk undecided, see previous hunk
s - split the electric current hunk into smaller hunks
eastward - manually edit the current hunk
? - print help
@@ -1,2 +1,4 @@
-A new file
-and some more awesome changes
+This is line 1
+This is line two
+This is line 3
+This is line iv
Stage this hunk [y,n,q,a,d,/,e,?]?


So information technology appears that Git is offering us the opportunity to either
  • Phase it
  • Exercise not stage it
  • Quit,
  • Stage it and all remaining hunks
  • Do not stage it or any of the remaining ones
  • Search for a regex
  • Edit the hunk

In fact, though the assistance mentioned a separate control, nosotros practice non have this option bachelor to us, due to the nature of our hunk. Instead, if we wish to separate this hunk, we are going to accept to edit information technology manually. To practice this we will choose the e option.

Here we are left in our chosen text editor, to either add together, modify or remove lines from the hunk. In our example, we are going to delete a few lines. We only want to get out the commencement line of additions. Just because we delete the others does not mean they are deleted from the working re-create. Retrieve, nosotros are not editing the actual files here. Merely the hunks that are going to exist staged.

# Manual hunk edit mode -- see lesser for a quick guide
@@ -ane,two +1,4 @@
-A new file
-and some more crawly changes
+This is line one
+This is line 2
+This is line iii
+This is line 4
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # volition be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will exist given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.


After the deletes, the editors file should look like this.

# Manual hunk edit way -- see bottom for a quick guide
@@ -1,2 +ane,four @@
-A new file
-and some more awesome changes
+This is line 1
# ---
# To remove '-' lines, brand them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does non apply cleanly, you will exist given
# an opportunity to edit once again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.


In one case we quit our editor, we are so asked about the next hunk. In our case, nosotros are going to utilise all of the changes to our 2d file during this commit. To do this nosotros are going to use the a choice.

unequal --git a/newfile2 b/newfile2
index 3545c1d..40efcce 100644
--- a/newfile2
+++ b/newfile2
@@ -1,2 +1,iv @@
Another new file
and a new awesome feature
+This is a new line
+This is another new line
Phase this hunk [y,n,q,a,d,/,e,?]? a

john@satsuki:~/coderepo$


At starting time glance, we practise not appear to have been left with any indication that anything has taken place. In guild to perform a cheque nosotros shall run our obligatory git diff, both between the working re-create and the alphabetize, and betwixt the index and the last commit.

john@satsuki:~/coderepo$ git diff
diff --git a/newfile1 b/newfile1
alphabetize f702b65..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -one +1,four @@
This is line i
+This is line 2
+This is line 3
+This is line four
john@satsuki:~/coderepo$


Then above, we can see that the divergence betwixt the working copy and the alphabetize, or staging area, is the terminal 3 lines that we are not ready to commit notwithstanding. Below we can come across the difference between the staging area and the terminal commit to the repository. This includes the three lines that we included during our interactive commit preparation.

john@satsuki:~/coderepo$ git unequal --buried
diff --git a/newfile1 b/newfile1
index 44640b2..f702b65 100644
--- a/newfile1
+++ b/newfile1
@@ -1,two +i @@
-A new file
-and some more than crawly changes
+This is line 1
diff --git a/newfile2 b/newfile2
index 3545c1d..40efcce 100644
--- a/newfile2
+++ b/newfile2
@@ -1,2 +i,4 @@
Another new file
and a new crawly feature
+This is a new line
+This is some other new line
john@satsuki:~/coderepo$


Nosotros can at present commit in the normal way, and continue to edit the working re-create to stage the sections nosotros require. The following output is shortened for brevity.

john@satsuki:~/coderepo$ git commit -m 'Added first line'
[fantasy 03bd20c] Added start line
2 files inverse, 3 insertions(+), 2 deletions(-)
john@satsuki:~/coderepo$ git add -p
diff --git a/newfile1 b/newfile1
alphabetize f702b65..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -1 +i,4 @@
This is line 1
+This is line two
+This is line 3
+This is line 4
Stage this hunk [y,n,q,a,d,/,eastward,?]? east

john@satsuki:~/coderepo$ git commit -m 'Added second line'
[fantasy 302e3fa] Added second line
1 files changed, 1 insertions(+), 0 deletions(-)
john@satsuki:~/coderepo$ git add -p
...
...


Nosotros take gone through the process of editing each hunk for each commit and have performed the commits.

john@satsuki:~/coderepo$ git log
commit a59e73b1dc571318a1154aa4c2fc591ab6f1f395
Writer: John Haskins <john.haskins@tamagoyakiinc.koala>
Engagement: Wednesday Apr 13 23:56:39 2011 +0100

Added quaternary line

commit 3ca3d627a54418be4c2e9d9196db6ce62e2b93ff
Writer: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Midweek Apr 13 23:56:13 2011 +0100

Added third line

commit 302e3fa5f880a2a503235667b4c96d4dcdaa11be
Author: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Wed Apr 13 23:55:57 2011 +0100

Added second line

commit 03bd20cb8a78a28f003ab402492cf7055f21bb2e
Author: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Wed Apr thirteen 23:55:32 2011 +0100

Added first line
...
...


Side by side nosotros are going to meet how to perform exactly the aforementioned procedure using git gui. For a starting time, we are going to reset our branch Head and our index back to their state before nosotros fabricated the last four commits, however we are going to exit the working re-create files in the state they were after these final 4 commits. This is called a mixed reset, every bit it modifies the staging expanse and the HEAD, simply does not touch the working files.

john@satsuki:~/coderepo$ git log --oneline
a59e73b Added fourth line
3ca3d62 Added third line
302e3fa Added 2nd line
03bd20c Added first line
d50ffb2 Merged in zaney
ed2301b Removed third file
...
...
john@satsuki:~/coderepo$ git reset --mixed d50ffb2
Unstaged changes afterward reset:
K newfile1
M newfile2
john@satsuki:~/coderepo$


Now nosotros will run a diff, but to be sure.

john@satsuki:~/coderepo$ git diff
diff --git a/newfile1 b/newfile1
alphabetize 44640b2..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -one,ii +i,4 @@
-A new file
-and some more than crawly changes
+This is line 1
+This is line ii
+This is line iii
+This is line 4
unequal --git a/newfile2 b/newfile2
index 3545c1d..40efcce 100644
--- a/newfile2
+++ b/newfile2
@@ -i,2 +ane,iv @@
Another new file
and a new awesome feature
+This is a new line
+This is some other new line
john@satsuki:~/coderepo$


If we now run our git gui command, nosotros volition run into the changes that are present, once we click on ane of the files in the left hand portion of the screen. Let us commencement with newfile2 every bit we want every change from that file present in this commit. Figure 1 shows what git gui looks like at this stage.

Figure ane
Changes to newfile2


If we right click on one of the green lines, remembering that dark-green is short for an addition, we get a carte du jour which we take not seen earlier. Among other things, this menu has the ability to stage a specific line or hunk for commit. In our instance we are going to hit the Stage Hunk For Commit and and so motility on to the side by side file, newfile1.

Figure 2
Changes to newfile1


Now we are looking at newfile1, we can use the Stage Line For Commit to add specific lines into the staging area. The file is shown in Figure 2. Equally our file is so small, we can see problems if we just cull lines at random and stage them. This is because it is hard for Git to find context around which to associate the change. The context is the area immediately surrounding the change nosotros are making. In order to reduce the gamble of the error occurring, we are only going to start at the acme, and select the first three lines for committing, by correct clicking on each on in plow and choosing the Stage Line For Commit option from the menu.

We have the changes that nosotros expect set to exist committed. If we desire to check ane last fourth dimension that they are right, nosotros can use the Staged Changes pane on the left to choose the file and inspect the diff. In one case we are happy we can apply the Commit area of the window to type our commit bulletin and emblazon our changes forever.

Figure 3
Changes to newfile1 after staging


Figure 3, shows what our file looks similar subsequently the kickoff commit. Interestingly, as our file is so small, you will probably detect that git gui throws a corrupt patch error if you attempt to just commit the adjacent line. As we mentioned earlier, it is always a skillful thought to know how to utilise the command line tools for precisely this reason. Frequently yous are dealing with special cases, that the GUI simply can non handle. In these cases, you may find you need to switch to the command line interface, to go the job washed.

Allow united states of america now movement dorsum to our master branch and remove the fantasy branch.

john@satsuki:~/coderepo$ git checkout main
Switched to branch 'master'
john@satsuki:~/coderepo$ git branch -D fantasy
Deleted branch fantasy (was 29ceede).
john@satsuki:~/coderepo$


Find that because we used the capital -D parameter, we were not asked if nosotros were certain nosotros wanted to delete the fantasy co-operative.

And then in the Later Hours department this week, nosotros have establish two ways to do the same complex task, one graphical and one control based. Staging commits in this way may seem rather odd, merely it volition help you to keep your commits exceedingly verbal. Whilst this may non matter on a personal project, for Tamagoyaki Inc, grouping the correct lines together in a commit will exist an extremely useful procedure to have available.

Previous Day

Next Day

chavezoulaings.blogspot.com

Source: https://cbx33.github.io/gitt/afterhours5-1.html

0 Response to "Your Edited Hunk Does Not Apply Edit Again"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel