discard changes in git

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

The git is a distributed version control system. In git, each user has a local copy of the repository. While working on a local copy, there may be chances that a user does not want to updated local changes in the remote git repository.  The reason may be some temporary changes, those are not needed by anyone.  This git tutorial will guide steps to discard the local changes in a file, whit the easy to understand git example.

Why need to discard uncommitted changes in git?

While working, some files may change, but develope do 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 stagging 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 while commit unwanted changes will not go in the local repository.

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

[[email protected]_6_64-160 gitundochanges]# git clone [email protected]:/myrepository.git
Initialized empty Git repository in /home/gitdevelopment/gitundochanges/myrepository/.git/
[email protected]'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.
[[email protected]_6_64-160 gitundochanges]# ls -lrt
total 4
drwxr-xr-x 3 root root 4096 Dec 23 03:04 myrepository
[[email protected]_6_64-160 gitundochanges]# cd myrepository/
[[email protected]_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
[[email protected]_6_64-160 myrepository]#

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


[[email protected]_6_64-160 myrepository]# git status
# On branch master
nothing to commit (working directory clean)
[[email protected]_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")
[[email protected]_6_64-160 myrepository]#

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

[[email protected]_6_64-160 myrepository]# git checkout firstFile.c
[[email protected]_6_64-160 myrepository]# git status
# On branch master
nothing to commit (working directory clean)
[[email protected]_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.

Leave a Comment