Kohei Nozaki's blog 

Discard all changes and sync to the HEAD of the repository


Posted on Sunday Mar 09, 2014 at 09:38AM in Technology


Assume this:

kyle-no-MacBook:gitprac5 kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  5  3  9 09:40 hoge.txt
kyle-no-MacBook:gitprac5 kyle$ cat hoge.txt 
hoge
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ 

Wasting working tree

kyle-no-MacBook:gitprac5 kyle$ echo hoge >> hoge.txt
kyle-no-MacBook:gitprac5 kyle$ echo untracked > untracked.txt
kyle-no-MacBook:gitprac5 kyle$ echo tracked > tracked.txt
kyle-no-MacBook:gitprac5 kyle$ git add tracked.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   tracked.txt
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   hoge.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   untracked.txt
kyle-no-MacBook:gitprac5 kyle$ ls -l
total 24
-rw-r--r--+ 1 kyle  staff  10  3  9 09:45 hoge.txt
-rw-r--r--+ 1 kyle  staff   8  3  9 09:45 tracked.txt
-rw-r--r--+ 1 kyle  staff  10  3  9 09:45 untracked.txt
kyle-no-MacBook:gitprac5 kyle$ 

Do it

  • These 2 commands will work:
git reset --hard HEAD
git clean -f
  • Let's do it:
kyle-no-MacBook:gitprac5 kyle$ git reset --hard HEAD
HEAD is now at 31e0ee6 initial
kyle-no-MacBook:gitprac5 kyle$ ls -l
total 16
-rw-r--r--+ 1 kyle  staff   5  3  9 09:46 hoge.txt
-rw-r--r--+ 1 kyle  staff  10  3  9 09:45 untracked.txt
kyle-no-MacBook:gitprac5 kyle$ git clean -f
Removing untracked.txt
kyle-no-MacBook:gitprac5 kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  5  3  9 09:46 hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ 
  • When you want to delete directories, you can use “git clean -fd”.



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed