There's git add -p
to stage changes and git checkout -p
to discard changes interactively. How can I unstage changes from index by hunks?
(I thought that git unstage -p
or git reset HEAD -p
might work.)
In the Commit window, select the file you want to partially commit, then select the text you want to commit in the right pane, then right-click on the selection and choose 'Stage selected lines' from the context menu.
To unstage commits on Git, use the “git reset” command with the “–soft” option and specify the commit hash. Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily. Using the “–soft” argument, changes are kept in your working directory and index.
When you enter Git's patch mode, the chunks of code ('hunks') you're offered to stage/skip can sometimes be bigger than you'd want. Maybe a hunk you're offered contains multiple lines with changes that belong in more than one commit. Luckily, the s option is there to split the hunk down further.
Staging PatchesIt's also possible for Git to stage certain parts of files and not the rest.
If I am not mistaken, what you want is to unstage hunks interactively? I thought git reset -p
does exactly that. Its prompt message is even exactly like Unstage this hunk?
Also from the manual:
This means that git reset -p is the opposite of git add -p, i.e. you can use it to selectively reset hunks. See the “Interactive Mode” section of git-add(1) to learn how to operate the --patch mode.
I found this answer very helpful when learning staging, so I thought I'd modify it for unstaging, as I haven't found a thorough answer on stackoverflow to this "How to git unstage one line or part of a file?" question.
As @manojlds says, you can use git reset --patch <filename>
(or -p
instead of --patch
for short), and git will begin to break down your file into what it thinks are sensible "hunks" (portions of the file).
Git will then prompt you with a variant of this question:
Unstage this hunk [y,n,q,a,d,g,/,j,J,k,K,s,e,?]?
Here is a description of each option:
NOTES ABOUT e MANUAL EDITING: Be extra careful when using the edit (e
) mode above as it is not intuitive. I'll include and then revise the in-line git documentation:
# To remove '+' lines, make them ' ' lines (context). # To remove '-' lines, delete them. # Lines starting with # will be removed.
Revised for clarity:
+
are lines currently staged to be added which will now be unstaged. To keep +
lines from being unstaged (i.e. to leave them as staged changes), replace the initial +
with a space character.-
are lines currently staged to be deleted which will now be unstaged. To keep -
lines from being unstaged (i.e. to leave them as staged changes), delete each line entirely.Afterwards, you can use:
git diff --staged
to check that you unstaged/staged the correct changesgit add -p
to stage mistakenly removed hunksgit commit -v
to view your commit while you edit the commit message.Reference for future: Git Tools - Interactive Staging
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With