如何删除 Git 中未跟踪的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8200622/
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
how to remove untracked files in Git?
提问by Sarun Sermsuwan
I'm working on a branch, say "experimental" branch which I branch out from my master branch.Then, I generate a user model in experimental branch, but does not add them to index yet.
我正在一个分支上工作,比如说我从主分支分支出来的“实验”分支。然后,我在实验分支中生成一个用户模型,但还没有将它们添加到索引中。
What do I have to do if I want to discard all the changes of the files recently added in my experimental branch? The untracked files are listed as below:
如果我想丢弃最近添加到我的实验分支中的文件的所有更改,我该怎么办?未跟踪的文件如下所示:
$ git status
On branch new_chick
Untracked files:
(use "git add <file>..." to include in what will be committed)
.project
app/models/user.rb
db/migrate/
test/fixtures/users.yml
test/unit/user_test.rb
I tried to run "git reset --hard" in hope to undo all those changes, but all the files above still show.
我试图运行“git reset --hard”以撤消所有这些更改,但上面的所有文件仍然显示。
Anyone please shed some light on me?
任何人请对我有所了解?
回答by manojlds
To remove untracked files / directories do:
要删除未跟踪的文件/目录,请执行以下操作:
git clean -fdx
-f - force
-f - 力
-d - directories too
-d - 也是目录
-x - remove ignored files too ( don't use this if you don't want to remove ignored files)
-x - 也删除被忽略的文件(如果您不想删除被忽略的文件,请不要使用它)
回答by bit_cracker007
User interactive approach:
用户交互方式:
git clean -i -fd
Remove .classpath [y/N]? N
Remove .gitignore [y/N]? N
Remove .project [y/N]? N
Remove .settings/ [y/N]? N
Remove src/com/amazon/arsdumpgenerator/inspector/ [y/N]? y
Remove src/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
Remove src/com/amazon/arsdumpgenerator/s3/ [y/N]? y
Remove tst/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
Remove tst/com/amazon/arsdumpgenerator/s3/ [y/N]? y
-i for interactive
-f for force
-d for directory
-x for ignored files(add if required)
Note:Add -nor --dry-runto just check what it will do.
-i 用于交互式
-f 用于强制
-d 用于目录
-x 用于忽略文件(如果需要添加)
注意:添加-n或--dry-run以检查它会做什么。
回答by Lily Ballard
Those are untracked files. This means git isn't tracking them. It's only listing them because they're not in the git ignore file. Since they're not being tracked by git, git reset
won't touch them.
这些是未跟踪的文件。这意味着 git 没有跟踪它们。它只是列出它们,因为它们不在 git ignore 文件中。因为他们没有被 git 跟踪,所以git reset
不会碰他们。
If you want to blow away all untracked files, the simplest way is git clean -f
(use git clean -n
instead if you want to see what it would destroy without actually deleting anything). Otherwise, you can just delete the files you don't want by hand.
如果您想清除所有未跟踪的文件,最简单的方法是git clean -f
(git clean -n
如果您想查看它会破坏的内容而不实际删除任何内容,请改用)。否则,您可以手动删除不需要的文件。
回答by Sonu Mishra
For deleting untracked files:
删除未跟踪的文件:
git clean -f
git clean -f
For deleting untracked directories as well, use:
要删除未跟踪的目录,请使用:
git clean -f -d
git clean -f -d
For preventing any cardiac arrest, use
为防止任何心脏骤停,请使用
git clean -n -f -d
git clean -n -f -d
回答by Tomasz Nazarenko
You may also return to the previous state of the local repo in another way:
您还可以通过另一种方式返回本地 repo 的先前状态:
- Add the untracked files to the staging area with
git add
. - return to the previous state of the local repo with
git reset --hard
.
- 使用 将未跟踪的文件添加到暂存区
git add
。 - 使用 返回到本地存储库的先前状态
git reset --hard
。
回答by Jagriti Kumari
The command for your rescue is git clean
.
您的救援命令是git clean
。
回答by skb
Well, I had the similar issue. I had taken latest but there were some changes in the local due to which the merge was not happening to a particular file. The file was untracked and I did not want them so What I did was -
嗯,我有类似的问题。我采取了最新的但由于合并没有发生在特定文件上,所以本地发生了一些变化。该文件未被跟踪,我不想要它们,所以我所做的是-
$ git checkout filepath/filename
$ git checkout 文件路径/文件名
filepath - The location from where I did the git bash. then when I took the latest the changes were available
文件路径 - 我执行 git bash 的位置。然后当我采用最新的更改时
回答by ideasman42
While git clean
works well, I still find it useful to use my own script to clean the git repo, it has some advantages.
虽然git clean
效果很好,但我仍然发现使用我自己的脚本来清理 git repo 很有用,它有一些优点。
This shows a list of files to be cleaned, then interactively prompts to clean or not. This is nearly always what I want since interactively prompting per file gets tedious.
这将显示要清理的文件列表,然后以交互方式提示是否清理。这几乎总是我想要的,因为每个文件的交互提示变得乏味。
It also allows manual filtering of the list which comes in handy when there are file types you don't want to clean (and have reason not to commit).
它还允许手动过滤列表,当存在您不想清理的文件类型(并且有理由不提交)时,这会派上用场。
git_clean.sh
git_clean.sh
#!/bin/bash
readarray -t -d '' FILES < <(
git ls-files -z --other --directory |
grep --null-data --null -v '.bin$\|Cargo.lock$'
)
if [ "$FILES" = "" ]; then
echo "Nothing to clean!"
exit 0
fi
echo "Dirty files:"
printf ' %s\n' "${FILES[@]}"
DO_REMOVE=0
while true; do
echo ""
read -p "Remove ${#FILES[@]} files? [y/n]: " choice
case "$choice" in
y|Y )
DO_REMOVE=1
break ;;
n|N )
echo "Exiting!"
break ;;
* ) echo "Invalid input, expected [Y/y/N/n]"
continue ;;
esac
done
if [ "$DO_REMOVE" -eq 1 ];then
echo "Removing!"
for f in "${FILES[@]}"; do
rm -rfv "$f"
done
fi