如何使用git取消工作区中的更改?

时间:2015-02-24 05:50:50

标签: git

我正试图摆脱工作区中的“新文件”。当我运行'git status'时,我会得到类似的结果:

..
    new file: filepath/filename
    untracked files:....

我想取消暂存此文件。我试着跑:

git reset HEAD filepath/filename

git checkout filepath/filename

但是当我运行git状态时,没有任何内容被更改,或者新文件仍会显示消息“new file ..”(参见上文)。如何删除此文件以便我可以推送其余的代码?

1 个答案:

答案 0 :(得分:0)

git status周围是否有更多输出?

运行方案时,git status建议使用git reset HEAD path/to/file,其工作原理为:

my-mac:my_repo acanby$ echo "test file" > test.txt
my-mac:my_repo acanby$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test.txt

nothing added to commit but untracked files present (use "git add" to track)
my-mac:my_repo acanby$ git add test.txt 
my-mac:my_repo acanby$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   test.txt

my-mac:my_repo acanby$ git reset HEAD test.txt
my-mac:my_repo acanby$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test.txt

nothing added to commit but untracked files present (use "git add" to track)