Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only stage line deletions in a changeset?

Tags:

git

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?

like image 762
Frosty840 Avatar asked Oct 29 '25 16:10

Frosty840


1 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!

like image 157
D Malan Avatar answered Oct 31 '25 07:10

D Malan