| by Arround The Web | No comments

How to Fix a Broken Commit in Git: Best Practices and Solutions?

Commits are the timeline of the projects having the record of changes implemented. Being human, it is possible that we committed the wrong message, forgot to add the file, put the wrong branch name, or made changes in the wrong branch. These commits are known as broken commits. Luckily, Git offers the feature to fix these commits anytime.

The outlines for this blog are:

How to Fix a Broken Commit in Git: Best Practices and Solutions?

To fix broken commits in Git, check out the following possible scenarios.

Scenario 1: Fix the Wrong Commit Message

The first scenario can be a wrong commit message which can be easily fixed using the “git commit –amend” command. Let’s check the below-given steps to understand the particular scenario.

Step 1: Check Wrong Commit Message

Let’s display the git log history in which we have committed the wrong message:

git log

As you can see, we have the wrong “initial” spelling.

Step 2: Fix Wrong Commit

To fix this wrong commit message, use the “git commit –amend” command and specify the correct message using the “m” flag:

git commit --amend -m "initial commit"

The broken commit message has been fixed.

Step 3: Verification

To verify the commit message, display the log using the “git log” command:

git log

The wrong message has been corrected.

Scenario 2: Add the Missing File to a Commit

The second possible scenario for broken commits can be a missing file you forgot to add to the commit. Likewise, you can use the “git commit –amend” command to fix it. See the practical demonstration in the below-given instructions.

Step 1: Display Git Log

Display the commits history using the Git log command:

git log

As you can see, we have added and committed the new file.

Step 2: Add Missing File

Now, let’s assume you forgot to add one more file to it, simply put the file in the main repository and use the “git commit –amend” command to fix and edit the commit:

git commit --amend -m "missing file added"

The broken commit has been fixed and the “missing-file.txt” omit message has been added.

Scenario 3: Entered Wrong Branch Name

Another popular scenario can be the wrong branch name you set mistakenly. To fix this, you can simply rename that branch with the old and new name. To do this, use the “git branch -m” command, enter the current branch name, and specify the correct branch name. See the execution of the below command:

git branch -m featrue feature

In our case, the branch has been renamed “featrue” to “feature” successfully.

Scenario 4: Committed Changes in the Wrong Branch

Lastly, it’s also been noticed that users have mistakenly committed the changes in the wrong branch. In order to shift these commits to the desired branch, check the following instructions.

Step 1: Create a New Branch

Let’s create the new branch in Git using the following command:

git branch correct

The branch “correct” is created.

Step 2: Reset HEAD

For instance, we have added the changes in the “master” branch. Reset the Git “HEAD” pointer for the master branch:

git reset HEAD~ --hard

Git HEAD has been reset.

Step 3: Move to the New Branch

After resetting the HEAD, move to the desired branch through the given command:

git checkout correct

The branch has been switched to the “correct”.

Step 4: Check Log

Now, display the log history to check the HEAD pointer:

git log

As you can see the committed changes have been shifted to the “correct” branch.

Conclusion

To fix the broken commit in Git, the “git commit –amend” command is carried out. It is helpful that either you committed the wrong message or forgot to add the missing file. Furthermore, we have also seen scenarios where the branch name is wrong, and changes are committed in the wrong branch. This blog has enlightened the possible scenarios to fix the broken commits in Git Bash.

Share Button

Source: linuxhint.com

Leave a Reply