-1

I had a feature branch and a PR on github. This branch had many commits. I eventually merged the PR into master (did not squash all my commits into 1, so theres still ~50 commits that got put into master's history).

I'd like to undo that merge completely (along with all the commits that came from my feature branch along with the merge). I know I can do git reset --hard <sha before merge> but would that also remove all the commits that came into master from my feature branch as a result of the PR merge?

8
  • You can git rebase -i <sha before merge> and drop the commits you don't want. Commented Feb 1, 2023 at 17:09
  • And going forward, I would advise to squash the branches you're merging. Commented Feb 1, 2023 at 17:11
  • yep... lesson learned. Commented Feb 1, 2023 at 17:12
  • 2
    I would advice against squashing down everything to one commit when merging. It looses valuable history, especially for future use when you are trying to debug some problem with git bisect. If you are concerned with perhaps having previously added some debug code earlier that later is removed but you do not want to have added permanently to the history that is good thinking, but the remedy is to ensure proper version hygiene by using temporary commits, not to flatten everything into one commit. Commented Feb 1, 2023 at 18:08
  • 1
    @TTT yea that makes sense, I can close it and reference the other answer Commented Feb 2, 2023 at 18:47

2 Answers 2

0

looks like github has a 'revert' button on the merged PR - it will create a new PR to undo all the changes. I will go with that option. thanks all

Sign up to request clarification or add additional context in comments.

Comments

-3

You can use git rebase -i HEAD~<number of commits> then squash all the commits into one. But the catch here, you have to force push the master branch.

It's always the best practice to squash all commits and rebasing with latest master of the branch you are merging before merging that into master. Cheers!

7 Comments

but i merged into master, which also has other commits. with this approach i'd have to manually go through each commit (mine and not mine) and decide to keep/remove it?
Do you have that feature branch?
yea i have the feature branch
No, that isn't "always the best practice". It's purely a matter of convention and opinion. I happen to think it's a very silly thing to do actually. History is good.
And my perspective is that you shouldn't tell a beginner that squashing away history is "best practice". It isn't.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.