I use git for various projects (personal repositories only), and I want to do some housekeeping.
I have a downloaded git project tree that has a large history of commits. After downloading I made a few more myself. However, I do not need anything apart from the latest commit at the time I downloaded it, and the subsequent commits that I made. All the prior commits take up a lot of space, and I'd like to get rid of them.
What I should have done is delete the .git folder after download and create a new personal repository going forward - but I didn't.
So my question is this: can I clean up the repository so that everything prior to commit X is removed, as if it had never existed, but so that subsequent commits are maintained? If so, how? Also if possible, if there were multiple branches at that time, can I remove other branches also?
(Not sure if this is possible as I think one of git's claims is how hard it is to lose old data by mistake).
CodePudding user response:
I suggest you to squash your local commits by:
git log --oneline
# Write down the hash commit prior to your first commit
git rebase -i <commit-hash>
# Now a text editor will open, so change **pick** into **squash** for the second commit and following, then save and exit editor...
Now, all your new commits will be merged into your latest one.
You are ready to push it.
Here a short tutorial.
CodePudding user response:
This is what I tested;
- Make a backup of your repo first.
- Find the oldest commit.
- Run
git rebase -i <oldest-commit>
, and mark all commits except those you want to keep asdrop
. - Run
git remote remove origin
. - Run
git reflog expire --all --expire=now
. - Run
git gc --aggressive
.