Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

Say I edited a file

echo "hi" > someVersionedFile.txt

//Then I staged the file
git add .
git status

<console reads>
 Changes to be committed
 new file: someVersionedFile.txt 

Now I make additional changes to the files

echo "hi again file" >> someVersionedFile.txt

//Then I restage the file with these changes
git add .
git status

<console reads>
 Changed but not commited
 modified file: someVersionedFile.txt

Question: how do I revert the last staged version? Is this possible to do since it wasn't committed?

like image 342
stackoverflow Avatar asked Jul 15 '13 16:07

stackoverflow


People also ask

How do you reverse staged changes?

Staged files are those which go into your next commit. If you accidentally added files to the staged area, you can undo this by typing git restore --staged <file> , so in this case, it would be git restore --staged lib.

How do I revert a staged file in git?

Explanation: After you staged unwanted file(s), to undo, you can do git reset . Head is head of your file in the local and the last parameter is the name of your file.

What command is used to undo changes in the working directory and staging?

The git add command is used to add changes to the staging index. Git reset is primarily used to undo the staging index changes.


2 Answers

You can probably do this, but there's no single simple command to do it for you.

When you do git add, the file has already been added to the repository, but there is currently nothing pointing to it except the index. When you do a second git add, the new (version of the) file gets added to the repository, and the link between the index and the first version of the file is replaced with the new link. But the first object is still there.

You can use git fsck --dangling to get a list of blob objects in your repository that are currently not referenced by anything, but then you'll have to go through them one by one with git show, git cat-file, etc. to determine which is the file you want. Once you find the right file, you can git show HASH > somefile.txt (and maybe git add somefile.txt).

Of course, if you've done any git prune, git gc, etc. since the second git add, that may have actually removed the original file from your repository.

Here's a 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
$
like image 161
twalberg Avatar answered Oct 31 '22 07:10

twalberg


You can't do this. Git can only revert files to a previous commit, not previous staged version. This is because the file won't be entered into git's repository until you make the commit. (You can read more about best practices in committing over at Coding Horror)

Perhaps you can apply judicious use of the undo button? (If you've edited the code in an editor, obviously echoing to the file doesn't have an undo).

like image 41
SomeKittens Avatar answered Oct 31 '22 07:10

SomeKittens