I have a branch that I was working on.
My co-workers accidentally merged their branches into mine and now all the commits are all over the place.
I can't cherry pick coz there are hundreds of commits.
How do I keep only my changes in the branch?
Basically, I'm looking for a way to just keep my commits on the branch and discard any other commits done by other users.
In addition to the new branch solution hinted at by Nivrutti Pawar and Shakil, I'd suggest you could also restore the branch to its pre-merge state.
Explicit steps below.
Phase 1 : locate the merge commit (if you didn't do anything since the bad merge, go directly to phase 2 and use HEAD^ for the <bad-commit>)
# in case you're not presently on your branch
git checkout your-branch
# now we need to spot the merge commit where things went awry,
# it's probably not too far in your history, so spot its commit hash in the log
git log --oneline
In the output list you should be able to determine what is the commit hash of the bad merge, I'll just use <bad-commit> for the purpose of the example.
Phase 2 : go back in history
# Rewind history to the point just BEFORE the bad merge, so mind the caret
git reset --hard <bad-commit>^
At this point your branch is in the state it was just before the merge, but you'll need to force-push it to your remote to reflect this new history, so be sure to inform your coworkers beforehand, and then
git push --force origin HEAD
(And since you're not alone on the project, maybe take a look at --force-with-lease)
I want to add one more thing with Nivrutti pawar in this case we can use git-refloglink and check all actions of us in individual local machine and can able to go just before the mess action and start a new branch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With