Search code examples
gitresetstage

Recover stage state after reset


After git reset --mixed my previously staged new files became unstaged.
Is it possible to recover stage state that was before git reset execution?


Solution

  • Unfortunately, git does not automatically store the staging history, there is no direct way to restore the index to one of its previous state.


    In the future, here are some actions you can take :

    • commit more often
    • a variation : commit then rollback :

      # add stuff :
      git add ... / git add -p ...
      
      git commit -m "work in progress"
      git reset --soft HEAD^
      

      this will add the created commit in the current branch's reflog,
      using git reset --soft will preserve the staging area

    • use git stash && git stash apply
      git stash creates two commits :

      1. one for the content of the staging area,
      2. one for the content of all modified files

    Here is the output of the command :

    $ git stash && git stash apply
    Saved working directory and index state WIP on master: 7475d1d {message}
    On branch master
    Your branch is up to date with 'origin/master'.
    
    Changes to be committed:
    [...]
    

    here is what the stash commits look like :

        $ git log --oneline --graph stash@{0}
        *   79d9cd7 (refs/stash) WIP on master: 7475d1d {message} # <- full stash
        |\  
        | * 43f3da0 index on master: 7475d1d {message}            # <- staging area's content
        |/  
        *   7475d1d (HEAD -> master, origin/master) {message}     # <- current active commit
        |\
        ...