git - How to you revert a staged file with changes, preserving the changes to when it was staged? -


say edited file

echo "hi" > someversionedfile.txt  //then staged file git add . git status  <console reads>  changes committed  new file: someversionedfile.txt  

now make additional changes files

echo "hi again file" >> someversionedfile.txt  //then restage file these changes git add . git status  <console reads>  changed not commited  modified file: someversionedfile.txt 

question: how revert last staged version? possible since wasn't committed?

you can this, there's no single simple command you.

when git add, file has been added repository, there nothing pointing except index. when second git add, new (version of the) file gets added repository, , link between index , first version of file replaced new link. first object still there.

you can use git fsck --dangling list of blob objects in repository not referenced anything, you'll have go through them 1 one git show, git cat-file, etc. determine file want. once find right file, can git show hash > somefile.txt (and maybe git add somefile.txt).

of course, if you've done git prune, git gc, etc. since second git add, may have removed original file repository.

here's quick example:

$ git init foo initialized empty git repository in foo/.git/ $ cd foo $ echo blah > foo.txt $ git add foo.txt $ echo blarg > foo.txt $ git add foo.txt $ git fsck --dangling checking object directories: 100% (256/256), done. dangling blob 907b308167f0880fb2a5c0e1614bb0c7620f9dc3 $ git show 907b308167f0880fb2a5c0e1614bb0c7620f9dc3 blah $ 

Comments