How to git undo last commit

As a software engineer or web developer, we may require pushing many commits to our Git repository.

However, there are some other cases where we want to commit the files that will not be pushed to our Git repository. Before issuing any commit, we may wish to implement some additional changes sometimes.

As an effect, we require to undo the last commit through our Git repository. We will see how we can undo the last commit in this topic.

Undo Last Git Commit along with a reset

We can easily undo the last git commit by executing the command, i.e., "git reset" along with an option, i.e., "-soft" that will protect all the changes done to our files. We have to describe the commit to undo that is "HEAD~1".

The last commit of the git will be deleted from our Git repository.

In this notation,

"HEAD~1": It means that we wish to reset this HEAD to a single commit before inside the history of the log.

What will be the effect of the above command?

The command, i.e., "git reset" can be viewed as a command, i.e., opposite of the "git add", essentially inserting files into the Git index.

If describing the option of "-soft", Git will not change the files within the index or executing directory at all.

For example, we have included two files within our recent commit, although we wish to implement some changes into this file.

As a result, we will apply "git reset" along with the option of "-soft" to undo the last commit and also implement some additional changes.

Over the branch master

Our branch is master/origin ahead by commit 1 (apply "git push" for publishing our local commits).

Modifications to be committed:

new file: file1

As we can see, still the file is in the index by undoing the last commit (modifications to be committed) although the commit was deleted.

We have done the last commit on our repository successfully.

Hard Reset Git commit

We can consider in the above section that we can easily undo the last commit and preserve the modifications done to any file inside the index. But in some situations, we simply wish to remove the changes and commits made to any file.

It is an objective of an option, i.e., "-hard".

To undo the last commit and remove every modification within the index and directory, execute a command, i.e., "git reset" along with the option, i.e., "-hard" and describe a commit before command HEAD ("HEAD~1").

Every commit and modification will be removed from the index and directory after using the "--hard" command. So we should be careful.

For example, we have committed any new file named "file1" to our Git repository.

Now, assume that we wish to undo the last commit and remove every change.

Now, HEAD is on 90f8bb1 Second commit

Now, let's check our git repository state.

Over the branch master

Our branch is contemporary with master/origin (apply "git push" command for publishing our local commits).

Now, there is nothing for committing, and the working tree is clean.

Mixed Reset Git commit

Keep modification within our working directory, however, NOT within the index. We have to apply the command, i.e., "git reset" along with the option, i.e., "-mixed". After this command, we have to append the "HEAD~1" simply to the last commit.

For example, we have included any file named "file1" within any commit which we require to undo.

Simply, we execute the command, i.e., "git reset" along with the option, i.e., "-mixed" for undoing the last commit.

As a result, the "-mixed" command is a "mix" among the hard and soft reset, so its name.

Over the branch master

Our branch is master/origin ahead by commit 1 (apply "git push" for publishing our local commits).

Untracked files: (apply "git add <file>…" to add inside what will commit)

Nothing included for committing but the untracked files are present (apply "git track" for tracking).

So, we have detected another practice to revert the last commit by preserving modifications done to the files.

Undo Last Commit along with revert

Apply the command, i.e., "git revert" and describe a commit to be reverted, i.e., "HEAD" to the commit of history for reverting the last Git commit.

The command "git revert" is different as compared with the command "git reset" because it can record any new commit along with some modifications defined by degenerating the last commit.

We can describe "HEAD~1" with the "git reset" command because this reset command will set any new HEAD place while degenerating the commit described.

As a result, we will commit the modifications again to some files for reverting and commit to being unperformed. We have committed any new file for our repository of Git but we wish to degenerate this commit.

If executing the command "git revert", Git will open our text editor to commit the modifications automatically.

How to git undo last commit

A new message will show along with a new commit hash when we are done executing a commit message.

If we have to check our history of Git again, we can notice a new commit added to undo the last commit through our repository.






Latest Courses