Steve Emmerson
Steve Emmerson

Reputation: 7832

In Git, how do I create a commit that's before the oldest commit?

I have a Git repository whose oldest commit is version 6.8.2 (it is tagged "v6.8.2"). I have version 6.8.1 of the package that I would like to commit to the repository as the parent commit of the oldest commit. Is this possible? If so, how?

Upvotes: 4

Views: 212

Answers (1)

Abizern
Abizern

Reputation: 150675

You could rebase your current branch on top of the oldest commit that you want (v6.8.1). It will rewrite your history, though, but it is possible.

Edited to add more

Assuming your code is on the branch master

Clear out the existing code and create a new branch. Your old code is not lost - it still exists on the master branch.

git symbolic-ref HEAD refs/heads/newbase
rm .git/index
git clean -dxf

Now you are on the branch newbase which is an empty directory. Add the files that you want to be the new base to here.

<add the files to the directory>
git add .
git commit -a -m "New base commit v6.8.1"

New you have two branches master which has your original code and newbase which has only your new base.

Now checkout the master branch and rebase it upon the newbase branch

git checkout master
git rebase newbase

And then All you need to do is fix the conflicts, and you have a new master branch that starts at the earlier state of the code.

This is all a bit of a mess though. If you just want to add the code to the repository, just do the first bit, which creates a new branch. Tag it so that you know what it is, there is no need to rebase the master branch on to it because if you get to the base of master, and want to go further back in time - you can just see what is in the other branch.

Upvotes: 4

Related Questions