gitclean 删除git中的文件忽略,db/production.sqlite3丢失

git clean deletes files in gitignore, db/production.sqlite3 is lost

我试图从我删除的分支中删除一个未跟踪的文件,但我不小心删除了未跟踪和跟踪的文件...显然包括开发和生产数据库!!!

我该怎么办? git clone 并从上次提交重新开始?

Thomass-MacBook-Pro:livebytransit tomb$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    app/assets/javascripts/.txt
    app/assets/javascripts/shared/

nothing added to commit but untracked files present (use "git add" to track)
Thomass-MacBook-Pro:livebytransit tomb$ git clean -n
Would remove app/assets/javascripts/.txt
Thomass-MacBook-Pro:livebytransit tomb$ git clean -fx
Removing app/assets/javascripts/.txt
Removing db/development.sqlite3
Removing db/production.sqlite3
Removing log/development.log
Removing log/production.log

首先,git clean只能删除未跟踪的文件。它不能删除被跟踪的文件(即使你刚刚对文件做了 git addgit clean 也无法删除它)。根据定义,任何未跟踪的文件都不能被 git 恢复,因为 git 只是不知道它们。如果您之前跟踪过它们,您可以 check out a past version of the file.

其次,您 运行 的 2 个命令并不相同:-x 标志指示 git 删除所有未跟踪的文件(甚至那些被 various .gitignore 忽略的文件) .这就是导致您出现问题的原因。

虽然您的第一个命令 (git clean -n) 向您显示了文件,但它会忽略 gitignore 规则指定的文件,而第二个命令 (git clean -fx) 也包括那些要删除的文件由于 -x 标志。因此,所有未跟踪的文件 都将被删除 - 无论是否被 gitignore 忽略。

将来,如果您想要所需的行为,您应该只执行:git clean -f

目前,如果需要,您应该从 backup/some 其他 developer/via 数据库迁移中获取 db/*.sqlite 文件。 *.log 文件应该有望由您的开发设置自动生成,如果没有,只需使用该名称创建空文件。

引自documentation

$ man git-clean

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

-x

Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products.

-n, --dry-run

Don’t actually remove anything, just show what would be done.

-f, --force

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i