Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert and unstage

Tags:

git

You can use git revert --no-commit to undo changes from a particular change as we all know. This updates the working copy and the index, but does not commit the reversion. Is there any way to take this a step further and only update the working copy without updating the index? (i.e. the changes will be unstanged). I know you can use git checkout to get a working copy of a particular commit, but this also creates a detached head. I merely want to update the working copy with reverted changes.

like image 476
Explosion Pills Avatar asked Oct 21 '25 16:10

Explosion Pills


1 Answers

You can use a temporary index to leave the original index undisturbed:

true_index=$(git rev-parse --git-dir)/index
export GIT_INDEX_FILE=.mytmpindex    
cp "$true_index" .mytmpindex
git revert -n  <some-commit-id>
rm .mytmpindex
unset GIT_INDEX_FILE

In the case where you have no staged changes that you wish to keep, you can simply do:

git revert -n <some-commit-id>
git reset
like image 116
CB Bailey Avatar answered Oct 23 '25 08:10

CB Bailey