I got a huge code base, with many changes, I want to branch it, and push it to a remote new branch, but to push only change sets from the last month, make the first change of the month look like it's the creation of the repository, meaning I don't care about previous changes. I'm sure it's possible, just don't know what's it called and how to do it.
First, let's suppose that you've found the commit from about a month ago that you want to be the new first commit, perhaps with git show HEAD@{1.month.ago} or by just looking through git log. Let's say that that commit is f414f31. Let's also suppose that the name of the branch you're working on is called dev.
You say that you want it to appear that a later commit was the first one in the repository, so there's no real point in keeping this branch in the same repository - there will be no history in common at all. So, let's create a new empty repository in order to start from a clean slate:
mkdir squashed-repository
cd squashed-repository
git init
Now let's add a remote that refers to your old repository, and fetch all the branches from there as remote-tracking branches in your new repository.
git remote add previous /home/whoever/original-repository/
git fetch previous
Now update your working tree and index from the commit at the start of the month:
git checkout f414f31 .
(Note the . at the end of that line.)
Now create a commit from the state of the index:
git commit -m 'A new start.'
Now you use git rebase to replay all of the commits from after f414f31 up to and including previous/dev on top of your new master, which currently only has one commit.
git rebase --onto master b1cbc10e84bb2e2 previous/master
You may have to fix some conflicts during that rebase. By default, git rebase will linearize the history when it reapplies the changes introduced by those commits - you can tell it to try to preserve them with -p, but that may not work.
Now the master branch in this new repository should be as you want it, so you can remove the remote we created:
git remote rm previous
I should say that I'd generally try to avoid this type of rewriting of history, especially if you've shared the original repository with anyone. In any case, the earlier history can be really useful - is it really so bad to keep that in your repository?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With