How to discard changes in Git РGit undo local changes?


Git is a distributed version control system. To make changes, each user must obtain a local copy of a central repository using a git clone command. During the development life cycle, there may be chances that a user does not want to push local changes in the remote git repository in the next commit.

One reason may be there are some temporary changes that no one needs. This git tutorial will guide the steps to discard the local changes in a file with easy-to-understand examples.

Why do we need to discard uncommitted changes in Git?

While working, some files may change, but the developer does not want to update those files in the central repository. But why need to discard changes? Just make a pull request and edit the file. But that is not the case; when you do pull, it may ask for staging your changes. Once you stage, changes will go in the following git push.

When we do not want to stage unwanted changes, we need to discard changes so that in the next commit, the unwanted changes will not go into the local repository.

Clone the repository to bring the files to the local machine from the remote. 

[root@CentOS_6_64-160 gitundochanges]# git clone root@192.168.1.159:/myrepository.git
Initialized empty Git repository in /home/gitdevelopment/gitundochanges/myrepository/.git/
root@192.168.1.159's password:
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), 454 bytes, done.
[root@CentOS_6_64-160 gitundochanges]# ls -lrt
total 4
drwxr-xr-x 3 root root 4096 Dec 23 03:04 myrepository
[root@CentOS_6_64-160 gitundochanges]# cd myrepository/
[root@CentOS_6_64-160 myrepository]# ls -lrt
total 0
-rw-r--r-- 1 root root 0 Dec 23 03:04 secondFile.c
-rw-r--r-- 1 root root 0 Dec 23 03:04 firstFile.c
[root@CentOS_6_64-160 myrepository]#

Now we have two files. The following will check the status.

[root@CentOS_6_64-160 myrepository]# git status
# On branch master
nothing to commit (working directory clean)
[root@CentOS_6_64-160 myrepository]#

Git status shows there are no changes in any file. And it should be, as nothing changed yet. Now we will add some unwanted code in firstFile.c. Again will run the git status command.

git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: firstFile.c
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@CentOS_6_64-160 myrepository]#

Now we have changes to commit or stage. If the file has unwanted changes, the following will discard the changes.

[root@CentOS_6_64-160 myrepository]# git checkout firstFile.c
[root@CentOS_6_64-160 myrepository]# git status
# On branch master
nothing to commit (working directory clean)
[root@CentOS_6_64-160 myrepository]# vim firstFile.c

Git status, again, shows nothing changes. This means changes have been removed. You can verify by opening the file in VIM or any other editor.