Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-rebase

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

like image 338
sly Avatar asked Apr 09 '12 23:04

sly


People also ask

How do I rewrite git history?

There are many ways to rewrite history with git. Use git commit --amend to change your latest log message. Use git commit --amend to make modifications to the most recent commit. Use git rebase to combine commits and modify history of a branch.

What is the best way to modify your previous commit?

You can modify the most recent commit in the same branch by running git commit –amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit –amend to modify the most recent commit.

How do I change the first commit message?

Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.


1 Answers

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.

like image 91
Joseph DeCarlo Avatar answered Nov 15 '22 08:11

Joseph DeCarlo