Question

We made some changes to a branch, committed and merged them to master, then pushed to the remote. Subsequently we made more commits to master and pushed those up, related to the merge.

As it turns out, that merge and the subsequent modifications were the wrong way to go about solving our problem, and we'd like to revert to just before the merge. In looking at the SO Git for beginners guide I see the reset command, but for changes that have already been pushed is that good enough, calling reset and then pushing?

It's not our case here, but suppose that after we had merged, and done some fix-up commits/pushed afterward, there were other, unrelated pushes to master, what's a good way of only undoing our changes and not the unrelated ones?

Was it helpful?

Solution

There are two approaches here. The first is to reset your local branch, as if these changes never existed, and then force update the remote branch:

git rest <hash>
git push https://git.... --force

The main problem with this approach is the difficulty it creates for anyone else working with your remote.

The more "elegant" solution, IMHO, is to revert the change - i.e., acknowledge a mistake has been made, and rectify it, preserving history.

You can use git revert <hash> to create an additional commit that undoes the problematic commit, and then just git push it like every other commit. The advantage is that you can always amend the commit message, and explain exactly why the original solution was faulty, and why it had to be undone.

OTHER TIPS

git reset --hard <tag/branch/commit id>

check here for more info

The question was already asked here

Rewriting git history with a private remote repository

If other people have performed git pull on the repository, the most advised approach is to revert the commits search for it, there is many questions like that with many answer and find the one the most appropriate for you.

To avoid these problems in the future, always use an temporary "topic" branch until you are satisfied with the results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top