Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 844
David Markowitz Avatar asked Nov 10 '13 22:11

David Markowitz


People also ask

How do you Unstage committed changes?

To unstage commits on Git, use the “git reset” command with the “–soft” option and specify the commit hash. Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily. Using the “–soft” argument, changes are kept in your working directory and index.

How do you restore committed changes?

In review, the steps to git revert a commit and undo unwanted changes are the following: Locate the ID of the commit to revert with the git log or reflog command. Issue the git revert command and provide the commit ID of interest. Supply a meaningful Git commit message to describe why the revert was needed.


1 Answers

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.

like image 125
Richard Cook Avatar answered Oct 25 '22 20:10

Richard Cook