Rewrite your git history

Post image

I build a lot of experimental repos to support my tutorials, and I often find myself reusing code for other purpose. The problem is, these experimental repos can often come with a terribly messy git history… And so, before I try building on an old demo project, I like to clean up the git history

Use Git log to see your commit history

In a terminal, run the git log command to see the history of commits

1
git log --oneline

A typical output look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
7916089 (HEAD -> main, origin/main) Add NetlifyCMS and Netlify Identity
1772e12 Stage partials from theme before adding NetlifyCMS and Netlify Identity
6bf16ea update README.md for dev env
abd0c30 update structure from posts to blog to match theme structure
289d5a8 Add first draft post and author
26af822 Add default config / pages from theme
57817ee Add README.md
79c5be4 Add bookworm-light theme
7692bf2 Add gitignore for hugo
a2778df init commit

Start the interactive git rebase process

Start the git rebase process with the oldest commit you want to modify (in our case 26af822 Add default config / pages from theme):

1
git rebase -i 26af822~1

An editor will be fired up with all the commits in your current branch (ignoring merge commits), which come after the given commit. You can reorder the commits in this list to your heart’s content, and you can remove them. The list looks more or less like this:

1
2
3
pick 26af822 Add default config / pages from theme
pick 289d5a8 Add first draft post and author
...

git-rebase will not look at the oneline descriptions (purely for your pleasure) but do not delete or edit the names (“26af822” and “289d5a8” in this example).

Enter INSERT mode by typing i

By replacing the command “pick” with the command “edit”, you can tell git-rebase to stop after applying that commit, so that you can edit the files and/or the commit message, amend the commit, and continue rebasing. If you want to fold two or more commits into one, replace the command “pick” with “squash” for the second and subsequent commit. If the commits had different authors, it will attribute the squashed commit to the author of the first commit.

  • to edit a commit, replace pick by edit

  • to delete a commit, replace pick by drop

  • to insert new commits, add a new line with the word break

Exit the INSERT mode by hitting the ESC button on your keyboard

Save and exit the vim editor by typing: :wq

if you exit the editor with an error code, the rebase will be aborted. To exit with an error code on vim, do

:cq

now you are at the right commit. Make changes and add/stage those changes Then run

1
git commit --amend

or if you just want to insert new commits:

1
git commit

After that, return back to the previous HEAD commit using:

1
git rebase --continue

WARNING: Note that this will change the SHA-1 of that commit as well as all children – in other words, this rewrites the history from that point forward.

Update remote repository

Now we are going to force push changes to the repo (as we know what we are doing and are the only user of this repo)

1
git push --force

You May Also Like