I accidentally pulled a remote branch into my local branch, resolved the merged conflicts and committed again some different changes to the feature branch. Can I just revert the pull from the remote branch without losing the commits I have made since or what else can resolve this situation ? The workflow was something like:
git checkout my_feature
git pull origin wrong_branch
git commit -am "Resolving merge conflict"
git push
git commit -m "Some other commit to my_feature"
git commit -m "Another commit to my_feature"
git push
There are multiple ways to go about this; the easiest way would perhaps be to reset to the state before the merge, then cherry-pick the commits you made on top of that.
git reset --hard abc123^ # where abc123 is the hash of the merge
# commit. The ^ refers to the first parent of
# that commit, i.e. the commit before the
# merge happened.
git cherry-pick cde234 # where cde234 is the hash of a commit that
# you made after the merge
An alternative could be using git rebase
with the --onto
flag; see man git-rebase for more information.
Be sure to have a backup just in case.
Note that this means you are changing history. To get these changes to the remote, you will need to force push; man git-push look for --force
or --force-with-lease
. Think twice about this and be aware that if there are others working on the same branch who have pulled these changes in, they will need to take action to correct their history by means of git reset
or git rebase
. Also be aware that if others have pushed changes since you last fetched, you may be inadvertently removing those changes. Using --force-with-lease
in place of --force
will help you avoid this. In many cases, altering history is not worth the hassle.