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-12631{{"`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.

Tasks

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 challenge, let's use the repository from https://github.com/labex-labs/git-playground.

  1. Navigate to the directory and configure the identity.
  2. Create a hello.txt file, write "hello,world" in it and add it to the staging area.
  3. Create a fixup commit for the hash of the "Added file1.txt" commit message.
  4. Once you have created the fixup commit, you can automatically merge the fixup commit with the original commit during the next rebase. When opening the interactive editor, you don't need to change the text and save to exit.

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

commit 6f0b8bbfac939af197a44ecd287ef84153817e9d
Author: Hang <huhuhang@users.noreply.github.com>
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