Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How To Modify Existing, Unpushed Commit Messages in Git?
Next article icon

How To Modify Existing, Unpushed Commit Messages in Git?

Last Updated : 13 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When using Git, it's easy to make mistakes or want to change commit messages after you've made them. If you haven't pushed your changes to a remote repository yet, you can edit the commit message without messing up the commit history. In this article, we'll look at different ways to change commit messages that haven't been pushed yet.

Approach 1: Using git commit --amend

The --amend option allows you to modify the most recent commit message. When you run git commit --amend, Git will create a new commit that replaces the previous one, using the same changes, but with a new commit message.

Step 1: Git creates a new commit with the same changes as the previous one.

Step 2: The new commit gets a new commit hash.

Step 3: The previous commit is no longer referenced by the branch.

Step 4: The branch pointer is updated to point to the new commit.

# Create a new commit with a mistake in the message
git commit -m "Initial commit with a typo"

# Modify the commit message using --amend
git commit --amend -m "Initial commit without typo"

Output

Capture
Using git commit --amend

Approach 2. Using git rebase -i

The rebase -i option allows you to interactively modify commit messages. When you run git rebase -i, Git opens an interactive shell where you can modify the commit messages, reorder commits, or even delete commits.

Step 1: Git creates a temporary branch that points to the commit you want to modify.

Step 2: Git opens an interactive shell with a list of commits, where you can modify the commit messages.

Step 3: You can reorder commits, delete commits, or modify commit messages.

Step 4: When you save and close the file, Git applies the changes to the temporary branch.

Step 5: The temporary branch is then merged into your original branch.

# Create multiple commits with mistakes in the messages
git commit -m "Commit 1 with typo"
git commit -m "Commit 2 with typo"
git commit -m "Commit 3 with typo"

# Modify the commit messages using rebase -i
git rebase -i HEAD~3

This will open an shell where you can modify the commit messages. Replace pick with edit for the commits you want to modify, and then save and close the file.

edit 1234567 Commit 1 with typo
edit 2345678 Commit 2 with typo
edit 3456789 Commit 3 with typo

Then, for each commit, run:

git commit --amend -m "new commit message"
git rebase --continue

Output

Capture
Using git rebase -i

Approach 3. Using git filter-branch

The filter-branch option allows you to rewrite commit messages using a custom command. When you run git filter-branch, Git applies the command to each commit message, rewriting the commit history.

Step 1: Git creates a temporary branch that points to the commit you want to modify.

Step 2: Git applies the custom command to each commit message, rewriting the commit history.

Step 3: The temporary branch is then merged into your original branch.

# Create multiple commits with mistakes in the messages
git commit -m "Commit 1 with typo"
git commit -m "Commit 2 with typo"
git commit -m "Commit 3 with typo"

# Modify the commit messages using filter-branch
git filter-branch --msg-filter 'sed "s/typo/corrected/"' HEAD~3

This will rewrite the commit messages, replacing "typo" with "corrected".

Output

Capture
Using git filter-branch

Next Article
How To Modify Existing, Unpushed Commit Messages in Git?

E

emailmeay8uic
Improve
Article Tags :
  • Web Technologies
  • Git

Similar Reads

    How to Write Good Commit Messages in GitHub
    A good commit message is a concise, clear, and meaningful description of the changes made to the code. It provides context and helps collaborators understand the purpose behind the modifications. Writing effective commit messages is crucial for maintaining an organized project history, improving col
    7 min read
    How To Amend Commit Message In Git?
    Sometimes, after making a commit in Git, you may realize that the commit message needs to be changed. Whether it's a typo, missing information, or a need for better clarity, Git provides a way to amend commit messages. This article will guide you through the process of amending commit messages in Gi
    3 min read
    How to Undo Last Commit in Git?
    Sometimes, you might need to undo the last commit, whether it's due to a mistake, an incorrect commit message, or the necessity to rework the changes. This article will guide you through different methods to uncommit the last commit in Git, ensuring you can manage your repository effectively. Need t
    3 min read
    How to Merge Commits in Git?
    Merging commits in Git is a crucial part of version control, especially when working on complex projects with multiple contributors. Combining commits can help streamline the commit history, making it cleaner and easier to understand. In this article, we’ll explore different methods to merge commits
    3 min read
    How to Change Commit Message in Git?
    Changing a commit message in Git can be done in a few different ways, depending on whether the commit is the most recent one or an earlier commit. Here's an article on how to change a commit message in Git, covering scenarios both before and after the commit has been pushed to a remote repository.Th
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences