Create a Fixup Commit

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Git, a fixup commit is a special type of commit that is used to fix a previous commit. It is typically used when you want to make a small change to a commit that has already been made, without having to create a new commit. Fixup commits are especially useful when you are working on a large project with many contributors, as they allow you to make small changes without disrupting the work of others.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/commit("`Create Commit`") subgraph Lab Skills git/commit -.-> lab-12717{{"`Create a Fixup Commit`"}} end

Create a Fixup Commit

Suppose you are working on a project with several other developers, and you notice a small error in a commit that was made a few days ago. You want to fix the error, but you don't want to create a new commit and disrupt the work of the other developers. This is where fixup commits come in handy. By creating a fixup commit, you can make the necessary changes without creating a new commit, and the fixup commit will be automatically merged with the original commit during the next rebase.

For example, your task is to write the string "hello,world" to the hello.txt file and add it as a "fixup" commit to the commit with the message "Added file1.txt", so that it can be automatically merged in a subsequent rebase operation.

For this lab, let's use the repository from https://github.com/labex-labs/git-playground.

  1. Clone the repository, navigate to the directory and configure the identity:
git clone https://github.com/labex-labs/git-playground
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
  1. Create a hello.txt file, write "hello,world" in it and add it to the staging area:
echo "hello,world" > hello.txt
git add .
  1. To create a fixup commit, you can use the git commit --fixup <commit> command:
git commit --fixup cf80005
## This is the hash of the commit message "Added file1.txt".

This will create a fixup commit for the specified commit. Note that you must stage your changes before creating the fixup commit. 4. Once you have created the fixup commit, you can use the git rebase --interactive --autosquash command to automatically merge the fixup commit with the original commit during the next rebase. For example:

git rebase --interactive --autosquash HEAD~3

When opening the interactive editor, you don't need to change the text and save to exit. This will perform a rebase on the last 3 commits, and automatically merge any fixup commits with their corresponding original commits.

This is the result of running the git show HEAD~1 command:

commit 6f0b8bbfac939af197a44ecd287ef84153817e9d
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file1.txt

diff --git a/file1.txt b/file1.txt
new file mode 100644
index 0000000..bfccc4a
--- /dev/null
+++ b/file1.txt
@@ -0,0 +1 @@
+This is file1.
diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..2d832d9
--- /dev/null
+++ b/hello.txt
@@ -0,0 +1 @@
+hello,world

Summary

Fixup commits are a useful tool for making small changes to previous commits without disrupting the work of other developers. By creating a fixup commit, you can make the necessary changes and have them automatically merged with the original commit during the next rebase.

Other Git Tutorials you may like