Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top