Git

How to Undo Local Changes in Git

Git user performs different types of tasks by using multiple branches in the local repository. Sometimes the user needs to undo the local changes after or before commit for the project purposes. This operation can be done easily in git. Git saves the snap of the repository at different points and stores the history of the task. The user can move backward or forward at the particular committed or uncommitted point using git history. The local changes of the repository can undo before publishing to the remote server by discarding all changes or leaving the staged changes.

Prerequisites:

Install GitHub Desktop.

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a GitHub account

You will require to create a GitHub account to check the output of the commands used here in the remote server.

Create a local and remote repository

You have to create a local repository and publish the repository in the remote server to test the commands used in this tutorial. Go to the local repository folder to check the commands used in this tutorial.

Undo local changes by using git checkout:

The `git checkout` can be used for doing different types of tasks in the git repository. In this part, this command has used to undo the local changes of a particular file. Before executing the following commands, the send-email.php file has been modified that was added before in the repository. Run the following commands to check the status of the current repository and undo the changes of the send-email.php file by using the `git checkout command.

$ git status

$ git checkout send-email.php

$ git status

The following output will appear after executing the above commands. The first `git status` command’s output shows that the send-email.php file has been modified. Now you can add the modified file to the repository or undo the changes of the file. Here, the `git checkout` command has used the file name to undo the file changes and make the working tree clean. The last `git status` command shows that the working tree is clean now, and the file has been restored after executing the `git checkout command.

Undo local changes by using restore:

The `git restore ` is the easiest way to undo the local changes of the file in the repository. Like the last part, the send-email2.php file has been updated. Run the following commands to check the status of the git and undo the changes of the file by using the `git restore` command.

$ git status

$ git restore send-email2.php

$ git status

The following output will appear after executing the above commands. Here, the `git restore` command has used the file name to undo the file changes and make the working tree clean.

The existing files of the repository were modified and restored in the last part of this tutorial. But if the modified file is added to the repository and the user wants to restore the file in the previous state later, he/she has to run the `git restore ` command with the –stage option. Run the following command to check the current status of the repository, add the modified file and undo the adding task of the modified file.

$ git status

$ git add send-email.php

$ git status

$ git restore --staged send-email.php

$ git status

The following output will appear after executing the above commands. The first status output is showing that a file is modified. After adding the file, the second status output shows that the file can be restored in the previous stage using the `git restore` command. The third status output is showing that the updated file has been removed from the repository.

Undo local changes by using git reset:

Another useful command of git to undo local changes is `git reset.` The way to undo the changes after adding a new file in the repository is shown in this tutorial part. Run the following commands to add a new file named index.html in the repository and undo this task by using the `git reset command.

$ git status

$ git add index.html

$ git status

$ git reset HEAD

$ git status

The following output will appear after executing the above commands. Here, the `git reset HEAD` command has applied to undo the change made by the `git add ` command. The other outputs are the same as the previous part of this tutorial.

Undo local changes by using git revert:

The way to undo a committed task is shown in this tutorial. `git revert` command works like the `git reset` command but removes the added file from the repository after committing. Run the following command to check the current status of the repository. Run the following command to add the index.html file into the repository. Commit the task with the commit message and undo this change using the `git revert` command.

$ git add index.html

$ git commit -m "Index file added"

$ git revert HEAD

The following output will appear after executing the above commands. The output is showing that the committed task has reverted and the added file has been removed.

If you open the local repository from the GitHub Desktop, then the following information will appear.

Conclusion:

Four different ways have shown in this tutorial to undo the local changes of the git repository.  `git checkout,` `git restore` and `git reset` commands have used to undo the changes before committing the task, and the `git revert` command has used to undo the changes after committing the task.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.