Remove changed files in git working folder

Git has three states.

  • working directory
  • staging area (index), files marked to be included in the next commit
  • .git directory, for files already in the git tree

Sometimes you may want to restore changed or staged files back as they were in last commit.

To remove changed files use:

# this will discard changes to existing files
# and switch back to master branch
# works even for staged changes
git checkout -f master

# this will remove untracked files
# add -d to remove untracked folders
# add -x to remove .gitignored files
git clean -f

Example from scratch

Clone a sample repo hello-world/php:

git clone hello-world-php

Create and edit some files:

cd hello-world-php

# create new file
touch new_file

# modify existing file
echo "Add new line to existing file" >>

# stage file
git add

To see current repository status:

git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


Untracked files:
  (use "git add <file>..." to include in what will be committed)


Now run the git checkout -f master and git clean -f commands to get back to last commit.