Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap the content of a file in the working directory with the content of a previously staged version of this file?

Tags:

git

git-index

Is it possible to exchange the index state of a certain file with its working tree content?

like image 819
Mot Avatar asked Dec 12 '25 18:12

Mot


1 Answers

You can get content from index to anywhere you wish using git showobject-name, where object-name is the SHA1 or :filename (that means the version from index) or revision:filename (that means the version from given revision). So either:

git show :filename > filename.tmp
git add filename
mv filename.tmp filename

or

OBJECT=$(git rev-parse :filename)
git add filename
git show $OBJECT > filename

The former saves the data to disk before modifying the index while the later simply asks the index for object name it had, changes it and than gets the object from object store. At that time, nothing refers to the object any more, but it will not be removed until you run git gc.

like image 175
Jan Hudec Avatar answered Dec 14 '25 14:12

Jan Hudec



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!