git add --interactive "Your edited hunk does not apply"
git add line by line
git add patch edit
git add -p flag
git add all
git interactive rebase
I'm trying to use
git add --interactive to selectively add some changes to my index, but I continually receive the "Your edited hunk does not apply. Edit again..." message. I get this message even if I choose the e option, and immediately save/close my editor. In other words, without editing the hunk at all, the patch doesn't apply.
Here's the exact example I'm using (I'm trying to put together a small demo):
first change second change off branch third change off branch second change third change fourth change
Change supporting feature 1 first change second change off branch third change off branch second change third change fourth change bug fix 1 change supporting feature 1
I'm trying to show how to use
git add --interactive to only add the "bug fix 1" line to the index. Running interactive add on the file, I choose the patch mode. It presents me with
diff --git a/newfile b/newfile index 6d501a3..8b81ae9 100644 --- a/newfile +++ b/newfile @@ -1,6 +1,9 @@ +Change supporting feature 1 first change second change off branch third change off branch second change third change fourth change +bug fix 1 +change supporting feature 1
I respond with split, followed by "no" to apply the first hunk. The second hunk, I try to edit. I originally tried deleting the bottom line - that didn't work. Leaving the hunk alone completely doesn't work either, and I can't figure out why.
For this particular example, you need to tweak the line numbers in the hunk. Change the line:
@@ -1,6 +2,8 @@
so that it instead reads:
@@ -2,7 +2,8 @@
Git add --interactive, or how to make amazing commits (Example), Git add --interactive, or how to make amazing commits. #: Select the file number #</li> -#: Unselect the file number #</li> *: Select all files</li> #-#: Select an interval of files</li> #-: Select the files from the # to the last</li> git add -i (or --interactive) will give you an interactive interface where you can edit the index, to prepare what you want to have in the next commit. You can add and remove changes to whole files, add untracked files and remove files from being tracked, but also select subsection of changes to put in the index, by selecting chunks of changes
git add --patch and --interactive, git add –interactive. status gives you a status output, showing you how many added and removed lines there are in each file (split up between staged and unstaged changes) update allows you to stage complete files into the index. revert allows you to unstage changes from the index, again operating on whole files. git add -p which is the same as doing: git add --interactive p (for patch) * (choose all the files) From now, you will be asked for each diff of each file if you want to add it, with many possible answers :
Of course I'm late to this but nevertheless wanted to mention for the record that this issue was discussed last year on the git mailing list and it looks like not much has changed since.
This particular issue stems from splitting and attempting to edit the same hunk. The analysis, as originally posted by Jeff King, of the underlying problem essentially is:
Hm. OK, I see. The "does this diff apply" check feeds both parts of the split patch to git-apply. But of course the second part will never correctly apply, because its context overlaps with the first part, but doesn't take it into account.
Doing the check with just the edited patch would work. But that doesn't take into account that your edited patch will potentially fail to apply in the long run, depending on whether or not you accept the other half of the split patch. And we can't know that yet, because the user may not have told us (they could have skipped the first half, and then come back to it later after the edit step).
Jeff concludes his post with a very pragmatic workaround that always succeeds, and is thus highly recommended:
So in general, I think splitting and editing the same hunk is inherently dangerous and is going to lead to these sorts of problems. And because editing provides a superset of the functionality, I think you should just edit and either allow the first part of the hunk to be applied or not depending on your preference.
By only choosing to edit a hunk not previously split, you will not have to deal with the line numbers.
A Thorough Introduction to Git's Interactive Patch Mode, So far, so good; you're in a position to git add and git commit files this with staged as opposed to unstaged changes, redirect git diff --cached . git add --patch and --interactive. By Markus Wein • In Git • 1223 Words One of the things that is pretty much unique to Git is the index (also known as the cache or staging area). The index is the place where you prepare your next commit.
When you want to not delete a line that staged for deletion, as in
first line -second line third line
where you want to keep the second line, make sure you replace the
- with a space, rather than deleting the whole line (as you would to get rid of an added line). Git will use the line for context.
Git and the interactive patch add - DEV Community , txt or git add --patch file.txt . Both will result in an interactive console that shows the hunks, or grouped lines being changed, that you can either Now git commit and write a note to future Lorna in the commit message; When you're doing the interactive add, you see the same output as you do from git diff but with options to add this section, or not, or split it. I find this is a really good way of making sure you read the diff, and then only add related changes to the commit.
I recently figured out from reading this thread how to work the manual editing.
The trick I used was that if I have a diff like this.
+ Line to add + Line to add + Line I dont want to include + Line I dont want to include
The trick is to remove the two line I do not want completely making the resulting diff looking like this.
+ Line to add + Line to add
While this is most likley obvious to most people it was not for me until today and I thought I should just share my experience. Please tell me if there is any danger to this method.
Commit only part of a file in Git, You can use git add --interactive or git add -p <file> , and then git commit (not git commit -a ); see Interactive mode in git-add manpage, git-add-interactive. Leverages the blessed library to provide an alternative to git add -i, giving the ability to dynamically add/remove files of the next commit. 🎒 Getting started
Git, git add -i (or --interactive ) will give you an interactive interface where you can edit the index, to prepare what you want to have in the next commit. You can add The git add command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add will fail with a list of ignored files. Ignored files reached by directory recursion or filename globbing performed by Git (quote your globs before the shell) will be silently ignored.
git ready » interactive adding, Sometimes simple adding with git add . or git commit -am just isn't enough. You may want to split changes up over several commits, or you're One way to do what you want would be roll back the commit while keeping the changes, and then committing the changes in batches. Undo last commit and keep changes: git reset --soft HEAD~1 This is in contrast to git reset --hard HEAD~1 which would remove the changes as well
Cleaner history with interactive commit building in Git, Git's Interactive mode for selecting files and diff hunks to add to the you review diffs of what you have staged: like a file-by-file git diff --cached You'll tell Git you're merging the two relevant branches, but that it shouldn't try to commit the result on its own, (edited to add: nor fast-forward if it thinks the merge is trivial): git merge --no-commit --no-ff branch-to-merge Then you'll ask git for the file as it appeared in the two branches:
- A good thing to make sure of here is that you aren't adding
-'s at the beginning of lines that don't exist in the file to begin with; it's a diff and it can't delete lines that aren't already there. So if a line in the diff begins with
+and you change it to
-git goes WTF? because now the line marked for removal doesn't exist to begin with (instead that line was marked for addition, and when a line marked for addition is marked for removal git can't remove a line that isn't already in the file).
- Check also line endings (LF, CRLF) in my case it didn't apply for one LF instead of CRLF!
- After some digging, I found that those lines are showing the "from-file range" and "to-file range". I don't really understand the logic behind changing the 1 to a 2. I tried it, and it works, but I don't understand why the "from-file range" changes. The original file is the same, whether I'm applying the whole patch, or just the edited hunk. Can you clarify further, or point me to a descent reference on the unified diff format. I've been unsuccessful in finding one.
- @Josh: stackoverflow.com/questions/2529441/… can help, even if I don't get entirely the Unified format hunks. @William +1
- @Josh: In looking into it it looks like it may be a bug. After you edit the hunk, git attempts to verify the patch by checking that all hunks will apply (this may be excessive). Unfortunately, in this case, that means the previous hunk (which you are not applying) is being checked, and there is some overlap which causes git apply --check to fail. I don't know an elegant solution; git may be doing the right thing by being overly cautious here.
- More info on changing the line numbers please? Why do we have to do that? And how? What does each number mean?
- Looks like modern versions of git handle this properly, though.
- Thanks for the link, but I had already seen this. I was not adding/leaving an additional line. The problem was in the line numbers, but I still don't understand the fix.
- I was not replacing a deleted '-' with a blank space - thus screwing up the indentation. Thx!!
- Just make sure you don't misunderstand the blank space part. I thought all unchanged lines should be just a line with a black - instead of merely the indent character ... Had an hour of "edited hunk does not apply" until I understood why :-/
- @oligofren I am not sure I understand you: what did you had to do to make your edited hunk apply?
- @VonC: I first thought I should change
- foointo ` ` (just a blank space, not "a blank space and the entire line"). Took me a while to understand it should have been ` foo`.