Undoing a git pull and it's effects on the history.

Here is my sad story

Today I made the mistake of pulling from a different Repository into mine. I am working on Repository A and I pulled the contents of Repository B into A. I did it because I wanted some files from that Repository.

After that I pushed my repository to remote.

My problem is not my code because both repositories are different. But the problem is the history.

Now my remote Repository which can be seen by the public has all the history of commits from Repository B mixed with my commits in the log.

How can I remove those commits from the history LOG?

Is there a way to kinda reset my repository to it’s state like yesterday ? Would that cause the commits from Repository B in my history to disappear ?

I tried to reset the Merge, Use Re-base and Cherry-pick but the history wasn’t removed in either case.

Thanks for your time

  • I want to revert my master branch to state 8 commits ago
  • How can I re-merge commits that were reverted in github?
  • SVN Revert Trunk, remove a revision as if it never existed?
  • Tortoise SVN Making a past revision the new Head
  • Is there a difference between git reset --hard HEAD and git checkout .?
  • Revert past git commits but keep changes introduced since
  • After reverting a file to a previous revision git diff shows no differences?
  • Revert changes to a file in a commit
  • 3 Solutions collect form web for “Undoing a git pull and it's effects on the history.”

    If you know where all of your branches were yesterday, you can go through and reset each of them to their original positions. For each relevant branch:

    git checkout <branch name>
    git reset --hard <old hash>
    git push -f <repository A> <branch name>
    

    You can then delete any new branches you don’t want. For each such branch:

    git branch -D <branch name>
    git push <repository A> :<branch name>
    

    This is all extremely sketchy if other people might have pulled from Repository A in the meantime though. The solution may be partly social as well as technical — other people who are using the repository may need to be told what’s going on so they can fix up their local clones.

    git reflog is where you want to look. So long as you haven’t garbage collected in a while, you should be able to do a git reset --hard HEAD@{x} with x being the specific point in history that you want to reset everything in your local branch back to.

    As soon as you do that, I’d do a git push --force to put your public repo back to where you want it…

    This has the unfortunate problems that if someone else has already pulled from your remote, their history will be messed up.

    Thank you everyone. I’ve solved my problem. I managed to solve it with reference to different articles and questions.

    I will explain the answer here along with things I’ve learned through my struggle.

    To begin with, in order to selectively remove commits from history that are scattered around the best way Can be found in the answer to this question:

    Specifically the answer by Charles Bailey. The closest to this answer was Stuart.

    Note: Reverting a commit doesn’t remove it from history, Reset does.

    As for my particular situation I thought I have to follow the previous method for every commit that I don’t want but actually the answer was simpler.

    In my situation I had to reset git to the commit just before I pulled Repository B and it has to be done for all branches. Once this is done all the undesired commits from B disappeared from my history.

    Another Interesting thing to mention is that using git Cherry-Pick or Revert on many commits isn’t as straight forward as it sounds from online articles, least for me.

    Thank you everyone.

    Git Baby is a git and github fan, let's start git clone.