How to only stage line deletions in a changeset?

Viewed 137

I've been coding in a particular file for too long without committing, and I've ended up with a situation where I want to commit feature (A) that removes a line of code. But, in my file, I also have work on feature (B) that adds a new line of code in the place of the code removed by feature (A).

In order to commit feature (A), I want to commit the change that deletes the original line. I don't want to commit the change that adds the new line from feature (B).

How do I just commit the deletion of the line, and leave the addition as an unstaged change?

2 Answers

You can use git add --edit to edit the diff. Run git add --edit, search for the relevant line and delete the line that contains the code you added for B. It should start with +. You should see the line that you want to remove for A and it should start with - (keep it).

Editing the diff shouldn't modify your working directory, but I'd make a backup of the file just in case!

If you're looking for a GitKraken answer: When you click a file and see the changes in hunk view, you can add each changed line sparately by clicking the "+" sign that appears right between the line numbers:

GitKraken: stage single line in hunk view

Klicking this in front of the deleted line will stage it and leave all other changes in the shown file as unstaged.

Related