How to discard changes in git – git undo local changes?

The git is a distributed version control system. To make changes, each user has to obtain a local copy of a central repository by 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 maybe there are some temporary changes, those are not needed by anyone.  This git tutorial will guide steps to discard the local changes in a file, with easy-to-understand examples.

Why 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 do a pull request and update the file. But not the case, when you do pull it may ask for staging your changes. Once you stage, changes will go in the next 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@
Initialized empty Git repository in /home/gitdevelopment/gitundochanges/myrepository/.git/
root@'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 yes, 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 showing nothing changes. Means changes have been removed. You can also verify by opening the file in VIM or any other editor.