H_end-rik
H_end-rik

Reputation: 591

Git, remove commits from parent Repo

I cloned a starting repo for a project. Spend the last few weeks idly working on it and making commits. Now I realized that there are tons of old commits in my repo that don't have anything to do with my project. As well as my repo having a few dozen contributors that never contributed to the project.

I would like to get rid of the old commits that are unrelated to the project and keep my own ones.

Using

git rebase -i

I was able to squash all old commits into two commits. But I am at a loos as to how I could get rip of those two in my history. Because rebasing seems to require me to use the second oldest commit as the one I squash into.

So having three commits:

  1. my initial commit
  2. middle commit
  3. old initial commit

I would like to squash them all into the first one but have no idea how to as I seem to only be able to quash into older commits.

Here would be the repo.

Upvotes: 1

Views: 84

Answers (1)

Barend
Barend

Reputation: 17419

If I read your question correctly, your problem is that you don't have a root commit to squash the original commits into. You can work around this by rebasing onto an orphan branch, thus injecting a new root commit before everything else:

(master) $ git checkout --orphan newmaster
(newmaster) $ git reset --hard
(newmaster) $ git commit --allow-empty -m 'Initial commit'
(newmaster) $ git checkout master
(master) $ git rebase newmaster
(master) $ git rebase -i "<<hash of root commit>>"
(master) $ git push --force

This is a very blunt instrument. It reminds me of the old adage that “Just because you can, doesn't mean you should.”.

Note: every commit hash in your master is going to get rewritten, so make sure you have no open branches anywhere. If your repository contains any tags you want to keep, you'll have to force update all of them. I recommend you try it on a throwaway clone first, to make sure it gets you what you're looking for, at a price you're willing to pay.

Upvotes: 1

Related Questions