How to Undo Commits in Git Locally & Remotely?

Coding Docs Git

Git is the world’s most popular version control system (VCS) to keep track of text-based file changes. Thanks to git commit command, we can save our local changes with a log message which makes available to send it to the remote repository or return back to that state anytime we want. 

But sometimes like every human being we can make mistakes. For example, we could include wrong files to our commit, we could make a misspelling in our commit message, we might shame from our latest commit and want to get rid of it on our local or remote repository.

In this article, you will learn how to undo things in git when you face these kinds of situations.

Undoing Cases

The easiest undoing git commit cases are the ones you are not pushed to your remote repository yet. You are free to do anything in your local git repository as much as you want if your current work is not used by your collaborators.

But when it comes to editing a commit or git history that is pushed to the remote repository and it is also publicly in use, you need to be very careful. Because which means you are dangering others’ works by destroying already in use git history.

That’s why we are going to divide these cases into two main cases as undoing locally and remotely.

Undoing Locally

Case: Staging Unwanted File

Let’s say you are about to make a commit in your local repository and by default behavior you use:

git add .

or

git add --all

commands in order to move fast. But this means add all file changes to the staging area in that repository. Sometimes, we can forget that some changes are not related to that commit and we shouldn’t include those files.
It’s not too late. Just use the git reset command with the file you don’t want to stage:

git reset unwanted_file

By using this command, we can move our changed file from the staging area to the working directory 

Case: Misspelled Commit Message

Let’s say you just make your commit to your local repository. But you also realized that the latest commit of yours has a misspelled word in your commit message. It’s not too late again. Just use git commit with amend flag.

git commit --amend

This opens your default terminal editor and you can edit your latest commit message as you desire as you can see below:

If you don’t want to deal with the editor, it has also a shorthand as:

git commit --amend -m "my updated commit message"

By using git commit with amend flag command, we overwrite our current commit which is a kind of a destructive operation. Because if we had already pushed this commit, our collaborators that were already making changes based on the same commit but by using the previous version. Thus there will be a git conflict that needed to be resolved.

Case: Adding Additional Change to Latest Commit

Let’s say you make your commit that is not pushed yet and also you realize that additional changes are needed for this commit. 

As a solution, first, you need to add your changed files to the staging area by git add command.

git add forgotten-file.js

Then you can use git commit command again but this time with amend and no-edit flags which automatically add your latest staged changes to your latest commit.

git commit --amend --no-edit

By this command, we just updated our current commit content. Again it will be a safe operation in your local repository.

Undoing Remotely

Case: Undo Already Pushed Commit

Let’s say you make few changes on your local repository that is not necessary and still you pushed it in a commit to your remote repository.

Now things need to be changed because also other remote repository users may pull the latest changes and use it like the git history is not be changed.

The safest and best approach is accepting your mistake and create an additional commit to take back your changes. By doing this git history will be stable but your mistake will be there forever.

First, let me show you how to look your git commits with git log command.

git log --oneline

You can realize at the beginning of each row there is a string which is git commit id also called as git commit hash and after that the branch information that comes with the git commit message. 

In order to exit from this git log screen just hit the “q” key.

You may already know that in git, HEAD indicates that your latest commit in your current branch. So we can use git revert command on our latest commit as:

git revert HEAD

This will open your default text editor with a default git commit message which you can edit it.

After you save your commit message (in vi editor, just type “:wq” and hit enter), you will see the git revert command output as:

Now you can check your commit history with git log command:

As you can see, now we have an additional git revert commit which undoes our changes in our “my feature implementation” commit by not interfering with git history. This is a safe operation that should be preferred in most cases.

Case: Undoing Multiple Commits

Let’s say you are working in a branch that nobody cares yet. You are just implementing your experimental features. Then the time has come, you need to send a pull request with your feature branch. 

But you have a lot of unnecessary commits which can be unnecessary about your feature or making to crowded your feature implementation.

Thus first look at that branch commits with git log command:

Then we decided that we just want to keep the first and last commit and get rid of others. There is an amazing git command for that which is interactive git rebase command. We just need to give a commit id in order to give rebase a starting point. In our case, it is the initial commit.

Thus the git command will be:

git rebase -i 1875e2a

This will open your default terminal editor (in our case, it is vi), and show you all the commits after the initial commit message as: 

You just need to select commits you want to keep by not changing the “pick” keyword. But for the others that you don’t want, just type “d” or “drop” instead of “pick”.

Then carefully check your changes and save & close the editor. The output will be:

If you want to check your commits again, it will be:

As you can see, we get rid of the commits between first commit and the latest commit. But don’t forget that it is a destructive operation which should be only used on a branch that nobody is using.

Conclusion

I hope by reading this article you learned how to undo things in Git locally and remotely. 

If you want to learn more about Programming, don’t forget to subscribe to our newsletter and if you have any questions, please don’t hesitate to contact us.

See you in new articles.

Leave a Reply

Your email address will not be published. Required fields are marked *