How do I revert a pushed commit back to being unstaged?
Asked Answered
K

2

13

I tried searching for an answer to this, but haven't found anything that matches quite like this problem. Feel free to link me to an answer if there is one already out there.

What I did was commit and push a large number of changes as one commit. I didn't merge that push into master though. I now want to go back to the state before I staged anything, so I can stage the changes into separate commits.

I've tried creating a new branch (saved-work) with the pushed changes, deleting the original remote branch (user-login), then merging the new branch into the original local branch, but that just takes me back to where I am now, with the user-login branch containing nothing to add/commit.

So how do I get all those changes back so I can review and stage them individually (using git add -p)?

Kwon answered 10/11, 2013 at 22:6 Comment(0)
B
14

If this is a shared repository and if there is a chance that anybody has already pulled your commits, then don't try to modify your history. If somebody else has already pulled your commits then you'll be fighting a losing battle to reconcile any additional commits that have been made to your errant branch.

If this is not the case, then you'll want to do some combination of resetting, rebasing and pushing with -f.

For example, you could reset using git reset --soft <hash> on the branch in question. This will reset the head of the branch to this commit while leaving all of the subsequent commits unstaged. You could then run git add -p to selectively restage your changes bit by bit.

Once you're happy with the branch you can then push with git push -f. This will forcibly overwrite history on the remote - but it cannot remove any history that anybody else might have pulled.

Burley answered 10/11, 2013 at 22:15 Comment(1)
Yep, this is my own repo, not being shared. I did the answer above, using git reset --soft HEAD~1 and then git reset HEAD .. But now I see from your response that git reset --soft HEAD~2 would have done both of those commands in one. Thanks!Kwon
N
7

git reset --soft HEAD~1 to get all HEAD changes in a stage.

$> man git-reset
   ...
   git reset [<mode>] [<commit>]
   ...
       --soft
           Does not touch the index file nor the working tree at all (but
           resets the head to <commit>, just like all modes do). This leaves
           all your changed files "Changes to be committed", as git status
           would put it.
Nonagenarian answered 10/11, 2013 at 22:14 Comment(1)
Thank you! That reverted them back to being staged, so I just did git reset HEAD . to unstage everything.Kwon

© 2022 - 2024 — McMap. All rights reserved.