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

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

  • Git - How to track a remote branch that you created from a local branch?
  • Git - getting a list of branches that were merged into another branch
  • Providing username with email when using Git clone
  • How to set the root application directory?
  • Git rebase non-interactive for last two commits - write a batch file
  • Git Reset - remotes/origin/master not in sync with local master
  • 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?

  • GitHub clone repositories with SSH doesn't work
  • “No Top file or external nodes data matches found” setting up gitfs with SaltStack
  • Git submodule remote server
  • Mirroring a repository across Github, Sourceforge and Google Code
  • Fix detached head without losing commits made while detached
  • How do I use git worktrees in IntelliJ IDEA 2016.1?
  • 3 Solutions collect form web for “How to you revert a staged file with changes, preserving the changes to when it was staged?”

    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
    $
    

    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).

    You mean you want to revert back to the staged version? Yeah, you can definitely do that. Probably the simplest way is to just commit your staged changes, checkout the file from that commit, and then reset your branch:

    git commit -m "Temporary commit"
    git checkout HEAD -- /path/to/file_you_want_to_revert
    git reset HEAD~
    

    Edit: Oops, sorry I didn’t notice the part where you restaged the file with the new changes. Sorry, but the other answers are correct: this isn’t possible (or at least, not very easy). I’ll leave this answer up, just in case someone else has a similar problem to you but hasn’t overwritten the cache yet.

    Git Baby is a git and github fan, let's start git clone.