Changing root git history

Updating git history from the initial commit

ยท

5 min read

โš ๏ธ WARNING

The following post contains material that may be harmful to some audiences

Just kidding, if you don't have force push access to your protected branches, or even better, you know how git works, there will be nothing wrong with this post ๐Ÿ˜….

The story ๐Ÿค“

I think it will be cool that share the story. So you won't be thinking, well, another git junk command that I won't use, at least not in this life.

I created a new repo in the organization for a POC-features, I made a fresh react-typescript vite project, and because it was the first time I pushed using terminal (I mostly use webstorm commit pannel). It used git config based on my system username and hostname that was not desirable.

I'm only human afterall, Don't put your blame on me

I wanted to do some cool stuff that was in my head, so I didn't notice the git log, and I didn't pay attention to the default git response. I also config git project by project and not globally expect the gitignore ๐Ÿ˜. (set global git ignore)

โžœ  poc git:(main) โœ— git commit -m "chore: init ts react vite project"
[main (root-commit) f164c9b] chore: init ts react vite project
  Committer: USERNAME <username@username.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 13 files changed, 227 insertions(+)
 create mode 100644 .gitignore
***
 create mode 100644 vite.config.ts

NOTE: If you want to see stats for a specific commit, you can use
git diff --shortstat <SHA-256>
You can see how git tries to be helpful, let's explain it just a little bit.

  • git(๐Ÿ‘พ): Okay bro, you want to commit, let's see what you have configured yet
  • me(๐Ÿฅธ): I'm somehow in a hurry, could you do it faster, please
  • git(๐Ÿ‘พ): Chill man, "we will all die someday" ๐Ÿ˜…. okay, it seems that you have not configured anything neither locally nor globally, I'm going to use your username and hostname, but remember this might not be your desire. It would be great if you can see what I just wrote in the console, for double-check.
  • me(๐Ÿฅธ): $ clear
  • git(๐Ÿ‘พ): ๐Ÿ˜

It's early in the morning, so I'm going to finish this up fast and sleep for just another one hour. I changed the configs and pushed another two commits but this time with webstorm. In the first commit, it asked me to set my name and email either locally or globally (I choose locally, like always). I pushed them into that remote main branch. Finally wanted to check the history in the Github commits page for the main branch, and saw this.

github.com_ishan-open_poc-riddler_commits_main (1).png

  • me(๐Ÿฅธ): What the H has just happened ๐Ÿ˜ข, git seems to be bad, it messes things up ๐Ÿคฌ.
  • git(๐Ÿ‘พ): ๐Ÿ˜
  • me(๐Ÿฅธ): Okay, I will change the commit with webstorm GUI and force push the changes to the main
  • me(๐Ÿฅธ): Webstorm doesn't allow me to change the initial commit, that's awesome ๐Ÿ˜ฐ.
  • git(๐Ÿ‘พ): ๐Ÿคฃ
  • git(๐Ÿ‘พ): Okay man let's fix it together, but this time with the terminal.

    No I'm not using a 23M $ warp terminal.

We need to change the history and change the author of the initial commit in that. Hopefully, git lets you rebase interactively and edit specific commits. for example

$ git rebase -i HEAD~2

Let you edit the latest two commits in the history, so simple haha. There were 3 commits and I want the latest one, let me start with the following:

$ git rebase -i HEAD~3
fatal: invalid upstream 'HEAD~3'

OMG, it is impossible to change the initial commit, guys relax,

"git 1.7.12+ (August 2012) has enhanced the option --root for git rebase" source

That sounds cool, isn't it?

$ git rebase -i --root

Then it opens the new vim editor, let's change the initial commit from pick (default behavior) to edit which let us use the commit, but it stops for amending. it may seem irrelevant to amend but if you recall the first git warning it asked us to amend the init commit with the --reset-author flag.

In vim, if you type i in go to insert mode to exit vim and save your changes, first press (esc) to exit insert mode and the press : (shift+;), and then type wq and press enter. it writes your changes and quit the vim. in case you don't feel confident with vim you can set your favorite editor as a git editor

- pick 330bdcf chore: init ts react vite project
+ edit 330bdcf chore: init ts react vite project
pick 8a6c17f feat: Add client server config for socket
pick 1589c55 feat: Add basic score-board tracker

After that, you can change the author via the following command

$  git commit --amend --author="your-username <your-email>"

It will open the vim page again, so you can simply save and exit like before (:wq)

Then you need to say rebase, I'm done you might finish rebasing process.

$ git rebase --continue

It is all done there. You just need to force push the following changes to the upstream.

$ git push --force

NOTE ๐Ÿดโ€โ˜ ๏ธ: changing git history is a bad idea most of the time, and changing git history that other people are working on will cause weird things to happen, but this repo (in your case, it might be your branch) was only used by you, so changing is not an evil action, till you are sure about the previous statement.

And there we go, it is all done.

github.com_ishan-open_poc-riddler_commits_main (2).png

ย