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.
Any ideas how to modify history to accomplish this?
Thanks many,
sly
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.
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.
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.
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:
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:
Now cherry pick the c5 commit (what you had on the clipboard)
git cherry-pick 50da9c3
Now re-root the master branch on to the base branch
git rebase --onto base --root master
Now you can delete the base and c4-c5 branches
git branch -d base
git branch -D c4-c5branch
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.
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