如何清除我在 Git 中的本地工作目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:17:29  来源:igfitidea点击:

How do I clear my local working directory in Git?

git

提问by Hans Sjunnesson

How can I clear my working directory in Git?

如何清除 Git 中的工作目录?

回答by dbr

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 statusoutput:

这在git status输出中提到:

(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 notthe .git/folder!), then do git reset --hardwhich leaves it with only committed files.

要删除未跟踪档案,我通常只是删除工作副本中的所有文件(但不是.git/文件夹!),然后做git reset --hard只有提交的文件留下它。

A better way is to use git clean(warning: using the -xflag 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 -fargument with -nto perform a dry-run or -ifor interactive mode, and it will tell you what will be removed.

将删除未跟踪的文件,包括目录 ( -d) 和被 git ( -x)忽略的文件。将-f参数替换-n为执行试运行或-i交互模式,它会告诉您将删除什么。

Relevant links:

相关链接:

回答by Marko

Use:

用:

git clean -df

It's not well advertised, but git cleanis really handy. Git Readyhas a nice introduction to git clean.

它没有很好地宣传,但git clean真的很方便。Git的准备有一个很好的介绍git clean

回答by dbn

All the answers so far retain local commits. If you're reallyserious, 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 matchthe 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 查找您的旧提交。

回答by nobar

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 empty,然后您可以自由地手动查看和删除任何剩余的非托管内容。

## create a stand-alone, tagged, empty commit
true | git mktree | xargs git commit-tree | xargs git tag empty

## 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 .gitfolder itself.

您的工作副本现在应该没有任何托管内容。剩下的就是非托管文件和.git文件夹本身。

To re-populate your working copy...

要重新填充您的工作副本...

git checkout master ## or whatever branch you will be using


If you're a forward thinking individual, you might start your repository off on the right foot by basing everything on an initial empty commit...

如果你是一个有远见的人,你可以通过将所有内容建立在最初的空提交上来从右脚开始你的存储库......

git init
git commit --allow-empty --allow-empty-message -m ""
git tag empty
...


There are various uses for a tagged empty worktree. My favorite at the moment is to depopulate the root under a set of git worktreesubfolders.

标记的空工作树有多种用途。我目前最喜欢的是删除一组git worktree子文件夹下的根目录。

回答by Tobias

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-at-eol)的工作副本,我无法使用我在此处阅读的任何命令摆脱它们,git checkout <branchname>也无法工作 - 除非给出-f(or --force) 选项。

回答by anshuman

To reset a specific file as git status suggests:

按照 git status 建议重置特定文件:

git checkout <filename>

To reset a folder

重置文件夹

git checkout <foldername>/*