Edit a commit message

Sometimes while writing the commit message, we make a typing error. The following commands can be used to fix the issue. Note that the below command creates a new commit so avoid using –amend for modifying commits which have been already pushed to a central repository.

git commit --amend            // start the editor to edit message
git commit --amend -m"New message" // edit the commit message directly

Normal commit

amend commit

If you forget to add a file while git add, then just add it and amend the previous commit.

git add forgotten_file_name
git commit --amend

Clean local commits before pushing

–amend is very useful to edit a commit message but it will not work if the commit you want to do is not the last one. In that case, rebase is used.

git rebase --interactive 

// if you didn't specify any tracking information for this branch,
// add upstream and remote branch information:
git rebase --interactive origin branch

This will give the following menu:

You will see a list of options that you can use to be taken to a view where you can edit the message. However, as can be seen from the above listing, interactive rebases offer a lot more than simple commit message editing: you can completely remove commits by deleting them from the list, as well as edit, reorder, and squash them. Squashing allows you to merge several commits into one before pushing them to the remote.

Common Git Problems and Their Fixes

Git is a free and open-source version control system. These days git is being extensively used and therefore this article focuses on some of the common Git tricks that everybody at some point of time requires while working with git or Github.

Sometimes the user makes some mistakes while working on Git which results in the loss of information or wrong information being added. To overcome this problem, Git provides some methods/tricks to roll back or modify the changes that were made wrong or made by mistake. Some of these problems along with their fixes are listed below:

Table of Content

  • 1. Edit a commit message
  • 2. Undo the local commits
  • 3. Reverting pushed commits
  • 4. Avoid repeated merge conflicts
  • 5. Find a commit that broke something after a merge

Similar Reads

1. Edit a commit message

Sometimes while writing the commit message, we make a typing error. The following commands can be used to fix the issue. Note that the below command creates a new commit so avoid using –amend for modifying commits which have been already pushed to a central repository....

2. Undo the local commits

Sometimes we realize that there is some error /mistake but by that time a few of the changes are committed locally....

3. Reverting pushed commits

Sometimes faulty commits do make it into the central repository even after amend and rebase. Therefore you can use the below commands...

4. Avoid repeated merge conflicts

Fixing Merge conflicts repeatedly is really bothersome....

5. Find a commit that broke something after a merge

Sometimes there is a need to find a commit that changed the project wrongly. This bad commit is difficult to find and hence consumes a lot of time. Git has introduced a method to find this commit which broke something after the merge:...

Contact Us