Rewriting Git History - 14 days of Git
Tags: 14daysofgit, git, devops
Published: 2022-09-27 12:55:54

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

* [What is version control?](https://www.techielass.com/what-is-version-control)
* [What is Git?](https://www.techielass.com/what-is-git)
* [Installing Git](https://www.techielass.com/installing-git/)
* [Basic Git commands to get started](https://www.techielass.com/basic-git-commands-to-get-started)
* [Inspect a Git repository](https://www.techielass.com/inspect-a-git-respository)
* [Git File Operations](https://www.techielass.com/git-file-operations)
* [Undoing commits and changes](https://www.techielass.com/undoing-commits-changes)

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](https://www.techielass.com/undoing-commits-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](https://community.ops.io/remoteimages/uploads/articles/hl68kuufpu876hrymv8z.png)

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](https://www.techielass.com/tag/14daysofgit) learning journey is to look at Git branches! Be sure to[ subscribe](https://www.techielass.com/newsletter) and join us for that step in the learning journey!

You can follow along here:[ https://github.com/weeyin83/14daysofgit](https://github.com/weeyin83/14daysofgit)