DEV Community

Cover image for Rewriting Git History - 14 days of Git
Sarah Lean 🏴󠁧󠁒
Sarah Lean 🏴󠁧󠁒

Posted on • Originally published at techielass.com

Rewriting Git History - 14 days of Git

I am now over halfway on my 14 days of Git learning journey, so far, I have explored:

Today I am going to look at rewriting Git history with a repository.

Rewriting history

If you have been following my journey learning Git, you will remember I look at how to undo commits and changes. I started to think about what if we wanted to change commits and rewrite the history a little?

There are a few options to do this. What I really want to look at is the --amend option that is available within the git commit command.

Change most recent Git commit message

There are times when you make a commit but realise you've written the wrong thing within the commit message. What do you do?This is one of the use cases for the --amend option.

If you issue git commit --amend -m "update the last commit message" it will amend the latest commit's message.

Git commit --amend

It's a great way of updating the last commit message if you've made a typo or missed out information you want to share with colleagues on the changes you've made.

Add extra changes to a commit

There are times when you complete a commit and then realise you want to add in one more change or you've forgotten something. And it would make more sense to add it into that commit rather than open another one.

This is another use case for the git commit --amend command.

Your workflow might be:

  1. Make changes to file 1 and file 2
  2. Add and commit those changes
  3. Realise you've forgotten to add a small change into file 1
  4. Make the additional change
  5. Use the command git commit --amend --no-edit

The additional option on the command,_ --no-edit_ takes that last change and puts it into the previous commit, without changing the message. For anyone else looking at this commit, it will look like it was done in one commit.

I would caution using this option and only using it on your own commits, don't confuse others by amending other people's commits.

Other ways to rewrite history

There are a couple of other commands that I'll be looking at later in my 14 days of Git learning journey that can help to rewrite history. Those commands are git rebase and git squash which will happen on Day 11 and Day 12 respectively.

14 days of Git

I've enjoyed diving into the additional options that are available with the git commit command and seeing how they can be used to help correct mistakes or rewrite history within my Git history. I look forward to exploring other commands that will also play a part in this later.

The next step in my 14 days of Git learning journey is to look at Git branches! Be sure to subscribe and join us for that step in the learning journey!

You can follow along here: https://github.com/weeyin83/14daysofgit

Top comments (0)