Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: ability to stage a certain file content without touching the working tree

Tags:

git

stage

I want to modify the index of one (text) file without having to change the working tree file state. Is this possible?

like image 378
Mot Avatar asked Oct 15 '25 16:10

Mot


1 Answers

Another take on "changing file in index without altering working dir" is to apply a patch to index only. This is often the way GUI git clients stage only selected lines from a given file.

You start out by (if you want) clearing out the changes from index for that file:

git reset path/to/file

Then extracting the full patch for it

git diff path/to/file > /path/to/tmpfile

Edit the patch file to include only the changes you want to apply, and apply just the edited patch:

git apply --cached /path/to/tmpfile

See:

git help apply
like image 97
ddotsenko Avatar answered Oct 17 '25 05:10

ddotsenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!