Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
git add -e

git add -e is like git add -p, except instead of adding things at the hunk level, you edit the entire patch at once. Or, in other words, whereas git add -p will show you each hunk for every file and ask what you want to do for each of them, git add -e will show you the entire patch and allow you to edit it at will. I used this trick to recently split apart one massive commit into 28 smaller, digestible ones.

Say you’ve replaced a line containing “baz” with one containing “bar”. When you git add -e, you’ll be presented with a diff like so:

- baz
+ bar

From here you can decide, actually, you don’t want to delete baz, you just want to add bar. And you want to add it above baz.

From this spot you can just change the minus to a space, making that line context for the diff. Then you can move the line that adds bar above baz, with this result:

+ bar

After saving and closing the editor you’ll be able to look at the result of your work in the index with git diff --cached:

+ bar

And the line you made into context remains in your working directory, which you can see with git diff:


But what if you end up modifying the diff in a way that makes it a patch that doesn’t apply? Git’s got you covered there.

Let’s say that when you moved bar above baz, after removing the minus from baz, you added an extra line on accident, making the patch invalid:


+ bar

When you save and close the editor Git will tell you of the problem:

error: patch failed: <some_filename>:1
error: file.txt: patch does not apply
fatal: Could not apply '.git/ADD_EDIT.patch'

In which case you’ll be able to attempt the add -e again, as Git will not have made any changes to the working directory or index at this point.

In some cases Git will attempt to apply the patch and give you the option of retrying the add, re-opening the editor with the modified .git/ADD_EDIT.patch if you choose to retry. If you don’t choose to retry, Git will delete .git/ADD_EDIT.patch.

In addition to editing the patch wholesale via git add -e, you can also choose during git add -p to edit a particular hunk manually by choosing ‘e’ to edit it instead of simply adding it via ‘a’. You can also add a file glob to the end of add -e as you would any other command to limit the size of the patch you’re about to edit.

  • chanhxc

    great explanation. thanks :)

  • Marcus

    Brilliant post, thank you.
    The problem with staging files with git add -e is that you have to keep account of lines inserted and deleted and update hunk headers accordingly. Is there any way to make git update those values automatically? I saw that git apply –recount does that, however I couldn’t make it work like git add -e does?

    • Can you give an example of the problem you’re running into? I’ve made considerable modifications to diffs via add -e and have never had to modify the hunk headers.

      Sorry for the late reply.

Share This