3

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

nulltoken
  • 64,429
  • 20
  • 138
  • 130
Mot
  • 28,248
  • 23
  • 84
  • 121
  • do you mean to add the contents of the file to the index? In that case, that's what `git add` does. Otherwise, I don't know what you mean – rlc May 18 '11 at 12:53
  • 1
    @rlc: I'd understand "exchange" as exchange. Put the version from work tree to index while putting the version from index to work tree. – Jan Hudec May 18 '11 at 13:19
  • @rlc, @Jan: The need to exchange could easily come up if you've been making a few last changes while assembling a commit, especially if the commit isn't committing all work tree changes You might say "oops, that version I staged goes with the commit of the other half of the changes, and the version in the work tree should be staged." – Cascabel May 18 '11 at 13:22

1 Answers1

5

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.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172