5

I'm working on a repository I forked in which the author took the dirty path and stuffed all his dependencies in the lib/ directory in the initial commit. Normally, if it wasn't the initial commit, I would just do an interactive rebase and delete the entire commit (if that's all he did). However, I have no idea how I would edit/delete the initial commit. Is there a way I can edit the initial commit and remove the lib directory without touching the rest of it?

EDIT

I accidentally stumbled upon the answer here: Can I remove the initial commit from a Git repo? in the selected answer. That's what I get for not searching more thoroughly.

1 Answer 1

4
$ git filter-branch --parent-filter \ 'test $GIT_COMMIT = SECOND && echo "" || cat' \ --tag-name-filter cat -- --all 

where SECOND is the SHA-1 of the commit you want to be the new root.

The above assumes that all branches use the same first and second commits. If not, you'll need to generalize the test in the parent filter.

Note that this is a drastic measure: it will rewrite your entire history and keep you from following the author's repo. Also if you've pushed your repo to remotes, you'll need to clobber those with git push -f.

Sign up to request clarification or add additional context in comments.

4 Comments

WARNING: Ref 'refs/heads/master' is unchanged WARNING: Ref 'refs/remotes/origin/master' is unchanged WARNING: Ref 'refs/remotes/origin/master' is unchanged is what it gave me.
Ack! You want to rewrite the parents of the second commit, not the first. See updated answer, and sorry for the false start.
I figured out a different way to do it (shown in my edit) before you edited this post. I'm going to go with the assumption that this actually works (I totally trust you <3) and mark your answer as accepted anyways.
Wouldn’t that have been shorter with echo <SHA of second commit> 0000000000000000000000000000000000000000 > .git/info/grafts; git filter-branch?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.