serhii
serhii

Reputation: 1177

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?

Upvotes: 1

Views: 132

Answers (2)

LeGEC
LeGEC

Reputation: 52111

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

Upvotes: 1

padawin
padawin

Reputation: 4476

You can re-add your files:

git add path/to/the/file

If only part of the file was staged, you can patch add:

git add --patch path/to/the/file

Upvotes: 1

Related Questions