In this blog, we shall look into some of the ways to rectify bad git commits. We shall simulate different situations that will give us a clear idea, what went wrong and how to correct it.

Without any further delay, let’s begin!

 

Situation one: git checkout :

After doing certain changes in the code, we suddenly realized that the previous code was better. Moreover, we haven’t committed the changes we made. Git checkout is another way to rectify git bad commits because we can reset the file where it was just after the last commit.

Therefore, to take the code where it was just after the last commit, execute:

git checkout filename

 

Situation two,  –amend:

while writing the commit message, we mistyped some of the messages and now we want to correct that.

git commit –amend  -m “this is the correct message”

Note: the above command will create a new commit. To check this, execute git log. Each time we commit some changes, git creates  new commit hash. This hash ID is unique for all the commit.

Situation three: Lets say we have forgotten to commit a file.  In other words, that file may be in modified area or in staging area and we want to keep that in the last commit.

Execute the following command to achieve that:

git add newfile

git commit –amend

After executing the command above, it will open an editor, we have to just close it without making any changes. Press esc key, :q. Same as VIM/VI editor.To check, the new file has been added in the latest commit, execute

git log –stat

 

Situation Four:  git cherrypick:

Let’s say we have two branches master and feature. We have done two commits in the master branch and we have created a new branch(feature) from the second commit.

 

Hence, we have a feature branch with two commits. We made two more commits in the feature branch. The image below represents the git structure.

git cherrypick

While working on more than one branch, sometimes we may make commits to the other branches. In this type of situation, we need to transfer those commits to the correct branch, this is where git cherry-pick comes into the picture.

In our case, by mistake, we have made two commits in the feature branch and we want to transfer those to the master branch.

 

Execute the following command to transfer the commits of the feature branch to the master branch.

Currently, we are in the feature branch.

Step1: Execute git log –oneline

copy the hashID to commit 1 of the feature branch.

Step2: Execute git checkout master

go to the master branch.

Step 3: Execute git cherry-pick hashID

paste the hash id to transfer the commit.

Remember, the transfer of commits should be sequential. In other words, commit 1 of the feature branch must be transferred first, then commit 2, and so on. Or else, git will create merge conflict.

 

In case that happens, execute git reset –merge.

 

Situation 5: git reset soft/mixed/hard :

git cherry-pick does not remove the commits from the source branch. In the case above, the commits in the feature branch still exist. Hence, those commits need to be removed.

For removing commits, we use git reset. There are three git resets, which are soft, mixed(this is the default), hard.

Reset soft: it takes us back to the point of the specified commit but it will keep the changes in the staging area.

Syntax: git reset –soft hashID

Reset mixed(default):  unlike reset –soft, mixed reset keeps the changes the modified/working area.

Syntax: git reset hashID

Reset hard: it removes the changes of that commit. There will be nothing in the modified or in the staging area.

Syntax: git reset –hard hashID

Situation 6: getting back the deleted commits after reset hard

Make some commits in a branch

Reset hard one of the commit

Use reflog , it shows the commits in the order to which we referred.

$git reflog

Copy the hashID just before the git reset –hard

Execute git checkout hashID, this will create a new branch without a head.

$git log

$git branch backup

to get the branch list and the current branch name

$git branch

Hence, by executing this command, the headless branch with be named as a backup branch

$git checkout master branch

 

Situation 7: git revert  :

Let’s assume that we have done some changes and other people have pulled those commits.

 

Later on, we realized that those changes are wrong. Therefore, we want to rewrite those commits, so that in the future when other members will pull again, they will get the correct version.

In this scenario, we shall use git revert.

While using git revert, we can only revert the latest change. Or else, git will make a merge conflict.

How to use git revert:

1.Execute git log, to get the latest commit hashID. Copy that hash id.

  1. Execute, git revert hashID, this is open an editor. Just close it by :q

Upon executing the above command, if we check the code, those changes of the last commit are gone.

Conclusion:

In this blog,  we discussed way to rectify git bad commits. Remember, HashID of commit is like a milestone. It works as an reference point from which git can do different kinds of operations like revert, reset etc. Therefore, every operation related to git commit, hashIDs are very much important.

In the upcoming blogs, of git we shall discuss about a git stash command and how is can be helpful of a programmer.