This question already has an answer here:
这个问题已经有了答案:
- How to remove local (untracked) files from the current Git working tree? 31 answers
- 如何从当前的Git工作树中删除本地(未跟踪的)文件?31日答案
How can I clear my working directory in git?
如何在git中清除工作目录?
6 个解决方案
#1
903
To reset a specific file to the last-committed state (to discard uncommitted changes in a specific file):
将特定文件重置为最后提交状态(在特定文件中丢弃未提交的更改):
git checkout thefiletoreset.txt
This is mentioned in the git status
output:
这是在git状态输出中提到的:
(use "git checkout -- <file>..." to discard changes in working directory)
To reset the entire repository to the last committed state:
将整个存储库重置为最后提交状态:
git reset --hard
To remove untracked files, I usually just delete all files in the working copy (but not the .git/
folder!), then do git reset --hard
which leaves it with only committed files.
为了删除未跟踪的文件,我通常只删除工作副本中的所有文件(但不是.git/文件夹!),然后进行git重置——这将使它只保留提交的文件。
A better way is to use git clean
: (Warning: using the -x
flag as below will cause git to delete ignored files.)
更好的方法是使用git clean:(警告:使用下面的-x标志将导致git删除被忽略的文件)。
git clean -d -x -f
will remove untracked files, including directories (-d
) and files ignored by git (-x
). Replace the -f
argument with -n
to perform a dry-run or -i
for interactive mode and it will tell you what will be removed.
将删除未跟踪的文件,包括目录(-d)和git (-x)忽略的文件。将-f参数替换为-n来执行一个干流或-i作为交互模式,它将告诉您将删除哪些内容。
Relevant links:
相关链接:
- git-reset man page
- git-reset手册页
- git-clean man page
- git-clean手册页
- git ready "cleaning up untracked files" (as Marko posted)
- git准备“清理未跟踪文件”(如Marko发布的)
- * question "How do you remove untracked files from your git working copy?"
- *问题“如何从git工作副本中删除未跟踪的文件?”
#2
108
git clean -df
Edit: It's not well advertised but git clean
is really handy. Git Ready has a nice intro to git clean
.
编辑:这不是很好的广告,但是git clean真的很方便。Git准备好了一个很好的介绍。
Update: removed the
x
flag based on the suggestion in the comment below更新:基于下面评论中的建议删除了x标志。
#3
32
All the answers so far retain local commits. If you're really serious, you can discard all local commits and all local edits by doing:
到目前为止,所有的答案都保留了本地提交。如果你是认真的,你可以放弃所有的本地提交和所有本地编辑的做法:
git reset --hard origin/branchname
For example:
例如:
git reset --hard origin/master
This makes your local repository exactly match the state of the origin (other than untracked files).
这使您的本地存储库与源的状态完全匹配(除了未跟踪的文件之外)。
If you accidentally did this after just reading the command, and not what it does :), use git reflog to find your old commits.
如果您只是在读取命令后意外地这样做了,而不是它所做的:),使用git reflog来查找您的旧提交。
#4
9
You could create a commit which contains an empty working copy.
您可以创建一个包含空工作副本的提交。
This is a generally safe, non-destructive approach because it does not involve the use of any brute-force reset mechanisms. First you hide all managed content with git checkout empty
, then you are free to manually review and remove whatever unmanaged content remains.
这是一种通常安全、非破坏性的方法,因为它不涉及使用任何蛮力复位机制。首先,您将所有托管内容隐藏在git checkout中,然后您可以手动检查并删除任何未管理的内容。
## create and initialize a temporary repo with an empty working copy
(
mkdir ./temp-repo && cd ./temp-repo && git init
touch ./temp-file && git add . && git commit -m "almost empty"
git rm ./temp-file && git commit -m "empty"
git tag empty
)
## fetch the history from the temporary repo
git remote add empty ./temp-repo && git fetch --tags empty && git remote rm empty
gvfs-trash ./temp-repo ## delete the temporary repo
## clear the working copy
git checkout empty
Your working copy should now be clear of any managed content. All that remains are unmanaged files and the .git
folder itself.
您的工作副本现在应该清除任何托管内容。剩下的都是非托管文件和.git文件夹本身。
To re-populate your working copy...
重新填充你的工作副本…
git checkout master ## or whatever branch you will be using
#5
7
To switch to another branch, discarding all uncommitted changes (e.g. resulting from Git's strange handling of line endings):
切换到另一个分支,丢弃所有未提交的更改(例如,由于Git对行结束的奇怪处理):
git checkout -f <branchname>
I had a working copy with hundreds of changed files (but empty git diff --ignore-space-at-eol
) which I couldn't get rid off with any of the commands I read here, and git checkout <branchname>
won't work, either - unless given the -f
(or --force
) option.
我有一个工作副本,其中有数百个已更改的文件(但空的git diff——ignore-space- eol),我在这里读到的任何命令都无法摆脱它,而git checkout
#6
1
To reset a specific file as git status suggests:
如git状态所示,重置特定文件:
git checkout <filename>
To reset a folder
重置一个文件夹
git checkout <foldername>/*
#1
903
To reset a specific file to the last-committed state (to discard uncommitted changes in a specific file):
将特定文件重置为最后提交状态(在特定文件中丢弃未提交的更改):
git checkout thefiletoreset.txt
This is mentioned in the git status
output:
这是在git状态输出中提到的:
(use "git checkout -- <file>..." to discard changes in working directory)
To reset the entire repository to the last committed state:
将整个存储库重置为最后提交状态:
git reset --hard
To remove untracked files, I usually just delete all files in the working copy (but not the .git/
folder!), then do git reset --hard
which leaves it with only committed files.
为了删除未跟踪的文件,我通常只删除工作副本中的所有文件(但不是.git/文件夹!),然后进行git重置——这将使它只保留提交的文件。
A better way is to use git clean
: (Warning: using the -x
flag as below will cause git to delete ignored files.)
更好的方法是使用git clean:(警告:使用下面的-x标志将导致git删除被忽略的文件)。
git clean -d -x -f
will remove untracked files, including directories (-d
) and files ignored by git (-x
). Replace the -f
argument with -n
to perform a dry-run or -i
for interactive mode and it will tell you what will be removed.
将删除未跟踪的文件,包括目录(-d)和git (-x)忽略的文件。将-f参数替换为-n来执行一个干流或-i作为交互模式,它将告诉您将删除哪些内容。
Relevant links:
相关链接:
- git-reset man page
- git-reset手册页
- git-clean man page
- git-clean手册页
- git ready "cleaning up untracked files" (as Marko posted)
- git准备“清理未跟踪文件”(如Marko发布的)
- * question "How do you remove untracked files from your git working copy?"
- *问题“如何从git工作副本中删除未跟踪的文件?”
#2
108
git clean -df
Edit: It's not well advertised but git clean
is really handy. Git Ready has a nice intro to git clean
.
编辑:这不是很好的广告,但是git clean真的很方便。Git准备好了一个很好的介绍。
Update: removed the
x
flag based on the suggestion in the comment below更新:基于下面评论中的建议删除了x标志。
#3
32
All the answers so far retain local commits. If you're really serious, you can discard all local commits and all local edits by doing:
到目前为止,所有的答案都保留了本地提交。如果你是认真的,你可以放弃所有的本地提交和所有本地编辑的做法:
git reset --hard origin/branchname
For example:
例如:
git reset --hard origin/master
This makes your local repository exactly match the state of the origin (other than untracked files).
这使您的本地存储库与源的状态完全匹配(除了未跟踪的文件之外)。
If you accidentally did this after just reading the command, and not what it does :), use git reflog to find your old commits.
如果您只是在读取命令后意外地这样做了,而不是它所做的:),使用git reflog来查找您的旧提交。
#4
9
You could create a commit which contains an empty working copy.
您可以创建一个包含空工作副本的提交。
This is a generally safe, non-destructive approach because it does not involve the use of any brute-force reset mechanisms. First you hide all managed content with git checkout empty
, then you are free to manually review and remove whatever unmanaged content remains.
这是一种通常安全、非破坏性的方法,因为它不涉及使用任何蛮力复位机制。首先,您将所有托管内容隐藏在git checkout中,然后您可以手动检查并删除任何未管理的内容。
## create and initialize a temporary repo with an empty working copy
(
mkdir ./temp-repo && cd ./temp-repo && git init
touch ./temp-file && git add . && git commit -m "almost empty"
git rm ./temp-file && git commit -m "empty"
git tag empty
)
## fetch the history from the temporary repo
git remote add empty ./temp-repo && git fetch --tags empty && git remote rm empty
gvfs-trash ./temp-repo ## delete the temporary repo
## clear the working copy
git checkout empty
Your working copy should now be clear of any managed content. All that remains are unmanaged files and the .git
folder itself.
您的工作副本现在应该清除任何托管内容。剩下的都是非托管文件和.git文件夹本身。
To re-populate your working copy...
重新填充你的工作副本…
git checkout master ## or whatever branch you will be using
#5
7
To switch to another branch, discarding all uncommitted changes (e.g. resulting from Git's strange handling of line endings):
切换到另一个分支,丢弃所有未提交的更改(例如,由于Git对行结束的奇怪处理):
git checkout -f <branchname>
I had a working copy with hundreds of changed files (but empty git diff --ignore-space-at-eol
) which I couldn't get rid off with any of the commands I read here, and git checkout <branchname>
won't work, either - unless given the -f
(or --force
) option.
我有一个工作副本,其中有数百个已更改的文件(但空的git diff——ignore-space- eol),我在这里读到的任何命令都无法摆脱它,而git checkout
#6
1
To reset a specific file as git status suggests:
如git状态所示,重置特定文件:
git checkout <filename>
To reset a folder
重置一个文件夹
git checkout <foldername>/*