Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I remove the initial commit from a Git repo? [duplicate]

Tags:

git

It seems that my initial commit is eating up 90% of the space, since it accidentally got made with quite a lot of media files in it. Is there a way to remove just the first commit from local and remote repos, or should I just leave this one be?

like image 359
kari.patila Avatar asked Feb 22 '09 20:02

kari.patila


People also ask

How do I remove a previous commit in git?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

How do I remove a clone from a repository?

There's not trick in terms of how to delete a Git repo locally from your computer. You just need to delete all of the content from the folder in which the Git repo was either cloned or initialized. That's it.


2 Answers

It sounds like you've already shared the repository with a number of other users. If this is the case, then you should probably just live with it.

If you're in control of all of the clones then you can re-write the history on top of a modified root commit with the unintended files removed. Note that you shouldn't do this if other developers have already based work off this branch.

If you're into rewriting history then you can try the following. Note that because git keeps around logs of where your HEAD commit has been recently (reflogs), the large objects won't immediately disappear from your repository, or other repositories which already have them, even if you attempt to git gc or git gc --prune. It will, however, ensure that any new clones won't end up fetching the large objects as part of the history of the master branch.

Assuming that your working directory is 'clean':

# Go back the initial commit
git checkout <SHA1_of_old_root>

# Clean up the index to remove unwanted files, e.g. using git rm <files>
# ...

# Amend the initial commit with the new tree. Note the sha1 of the new commit
git commit --amend

# Go back to the master branch
git checkout master

# Re-apply all the commits onto the new root
git rebase --onto <SHA1_of_new_root> <SHA1_of_old_root>
like image 61
CB Bailey Avatar answered Oct 11 '22 13:10

CB Bailey


Alternatively you can use git filter-branch to remove offending files (see EXAMPLES section), but it is also rewriting history. So anybody who based his/her code on top of your old history would get bad suprise...

like image 29
Jakub Narębski Avatar answered Oct 11 '22 13:10

Jakub Narębski