本文共 2946 字,大约阅读时间需要 9 分钟。
本文翻译自:
This question already has an answer here: 这个问题在这里已有答案:
How can I clear my working directory in Git? 如何在Git中清除我的工作目录?
参考:
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查找旧的提交。
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 committrue | git mktree | xargs git commit-tree | xargs git tag empty## clear the working copygit 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
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 initgit 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 worktree
subfolders. 我最喜欢的是在一组git worktree
子文件夹下减少root。
To reset a specific file as git status suggests: 要将特定文件重置为git status建议:
git checkout
To reset a folder 要重置文件夹
git checkout/*
To switch to another branch, discarding all uncommitted changes (eg resulting from Git's strange handling of line endings): 要切换到另一个分支,丢弃所有未提交的更改(例如,由Git奇怪处理行结尾):
git checkout -f
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>
won' --force
工作 - 除非给出-f
(或--force
)选项。
Use: 使用:
git clean -df
It's not well advertised, but git clean
is really handy. 它没有得到很好的宣传,但git clean
非常方便。 Git Ready has a . Git Ready 有一个 。
转载地址:http://pjcnb.baihongyu.com/