Wednesday, April 11, 2012

(yet another) modify git history to add a very first commit

I am modifying someone else's (non-git) code. I downloaded the original, ensued my modifications, and jumped the gun and made the modified version my first commit. This was a mistake, I should have added the original downloaded code as my first commit.


Then to add insult to injury, I added the original code as a branch off of my first edit.. In other words it is a big mess now :(


In simple terms, what I have looks like the blue graph below. C5 is the actual original code, C1,C2-C6 are my gradual modifications (C4 is redundant). I'd rather have the green be my history.


enter image description here


Any ideas how to modify history to accomplish this?


Thanks many,


sly


Answer:

I have done this completely at the command-line, but am going to insert screenshots from SourceTree to illustrate what's going on.
First get the first 7 or 8 digits of commit id (50da9c3) from the C5 commit and put in on your clipboard or in textedit or something - you'll need it later
For confirmation, Here is our starting state:
enter image description here
Here are the steps to get where you want:
Create a new non-rooted branch to start building commits off of named base
git symbolic-ref HEAD refs/heads/base
Now remove all files and directories from git
git rm --cached -r .
Now that all the files are 'untracked' remove them from the local working directory
git clean -d --force
Now create a commit with nothing in it (this will be the commit on which everything else will be built).
git commit --allow-empty -m'Initial Commit'
Your tree should look like this now:
enter image description here
Now cherry pick the c5 commit (what you had on the clipboard)
git cherry-pick 50da9c3
enter image description here
Now re-root the master branch on to the base branch
git rebase --onto base --root master
enter image description here
Now you can delete the base and c4-c5 branches
git branch -d base
enter image description here
git branch -D c4-c5branch
enter image description here
Keep in mind the the 'Initial Commit' is an empty commit with no files, so don't be fooled. The C5 commit is actually the first content based commit.

No comments:

Post a Comment