infoclogged
infoclogged

Reputation: 3997

Remove commits from an accidental merge

I have a situation, where I accidentally merged a submodule in the super project. I ran the below command from the super project i.e the location of .gitmodules

git pull submodule_dir/submodule_file

Ofcourse, now every commit messages from the submodule is integrated in the super project. I can manually remove the files, because they are just in a folder, but how do I remove all the commits ( they are around 9 in total ) and can also be identified. The problem is that rebase would not work, because the commits are spread across. A rebase for example to HEAD^9 would also remove some of the commits that actually belongs to the superproject.

git reset, git rebase wont work. because otherwise, I will loose other commits.

git reset --hard origin/master is the closest best bet, but it would destroy my local changes that I already commited.

What could be the solution? I have never used cherry pick,, but I am not sure, if I should do it. I wanted to ask before experimenting and make things worse. Something like this and to remove all the culprit commits.

commit latest: this consists of all my local changes and they are precious.
commit 
commit
culprit commit
commit
commit 
culprit commit
commit
commit
culprit commit
commit
commit
commit
commit

Upvotes: 0

Views: 469

Answers (2)

Caligone
Caligone

Reputation: 180

git reset --soft HEAD~9 will preserve your changes but will remove the commits

Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

Source

Upvotes: 2

aji ajith
aji ajith

Reputation: 71

You can use below command from your super branch.

    git reset --hard HEAD~9

It will reset last 9 commits and run below command

    git log

You can see your commits are removed.

Upvotes: 0

Related Questions