Reputation: 3265
On a new project, with a team new to git, we started out committing directly on master. We accidentally made a few bad commits on master that were pushed to a central repo and pulled down by the whole team.
Realising our mistake, we then created a feature branch off the commit before the bad commits. Our team is now happily working on the feature branch.
We now want our master branch to contain the exact code from the feature branch, without re-introducing the bad commits.
I see the merge theirs
option has been removed from the later versions of git. So what is the best way to discard the bad commits and make our master look exactly like our feature branch?
Upvotes: 1
Views: 1054
Reputation: 40760
There are (at least) two ways to get what you want, which may result in different history. Which you pick depends on how comfortable you are with git data structures and plumbing.
In the first, you make a merge commit with two parents: the current master and the tip of your feature branch. The content of this commit is exactly the same as the content in the tip of the parent branch. The easiest way to get this is to run the merge (git merge --no-commit
) then replace the contents of the index with the contents from the feature branch (git reset FEATURE_BRANCH
, I think) before committing the result.
In the second, you add a commit to the master that undoes all of your dodgy commits before manually merging in the feature branch. git revert
can help with this, or you can use git checkout LAST_GOOD_COMMIT
to get the code back to the state it was in at the last good commit, and commit that as the tip of master. Lastly, merge (or rebase) as usual.
I often find it helps to draw out the current structure of the tree and the structure I want, then work out which git operations I need in order to get from one to the other. Remember to work on a copy, and that the reflog will store all your history in any case :).
Upvotes: 2
Reputation: 605
if you want to go back to HEAD ( the previous ) you can do:
git reset --hard HEAD@{1}
Note: Working directory changes are lost.
You can check this as well [git commands]: http://git-scm.com/book/en/Git-Basics-Undoing-Things
Once you have moved back to a stable version without bad commits you can merge your feature branch to main branch
git checkout master_branch
You will switch to master with above command then
git pull origin master
To pull master branch
git merge feature_branch
This will update the master with feature branch changes. Now both branches will be same.
Upvotes: 6