如何快速备份我想通过 git clean 删除的未跟踪文件?
我有很多未跟踪的文件。我很确定我可以删除其中的大多数,但是...您知道,备份可能会有所帮助;)
您在类似情况下正在做什么?
I have a lot of untracked files. I am pretty sure that most of them I can delete, however... you know, backup could be helpful ;)
What you are doing in similar situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下命令将在您的主目录中创建一个 tar 存档,其中包含目录中所有未跟踪(且未被忽略)的文件:
如果您要使用此技术,请仔细检查 git ls-files --others --exclude-standard 本身会生成您期望的文件列表!
关于此解决方案的一些注释可能如下:
我使用
-z
来获取git ls-files
来输出带有的文件列表NUL
(零字节)作为文件之间的分隔符,xargs
的-0
参数告诉它考虑NUL
是从标准输入读取的参数之间的分隔符。这是处理文件名可能包含换行符的可能性的标准技巧,因为 Linux 上文件名中唯一不允许的两个字节是NUL
和/
.如果您有大量未跟踪的文件,那么
xargs
将多次运行tar
命令,因此我告诉tar< /code> 追加文件 (
r
) 而不是创建新存档 (c
),否则稍后调用tar
将覆盖该存档之前创建的。The following command will create a tar archive in your home directory of all of the untracked (and not ignored) files in your directory:
If you're going to use this technique, check carefully that
git ls-files --others --exclude-standard
on its own produces the list of files you expect!A few notes on this solution might be in order:
I've used
-z
to getgit ls-files
to output the list of files withNUL
(a zero byte) as the separator between files, and the-0
parameter toxargs
tells it to considerNUL
to be the separator between the parameters it reads from standard input. This is a standard trick to deal with the possibility that a filename might contain a newline, since the only two bytes that aren't allowed in filenames on Linux areNUL
and/
.If you have a huge number of untracked files then
xargs
will run thetar
command more than once, so it's important that I've toldtar
to append files (r
) rather than create a new archive (c
), otherwise the later invocations oftar
will overwrite the archive created just before.一般来说,如果它们有用,那么您应该签入它们!如果您现在不需要它们,请将它们签入并删除! IE,将它们跟踪为已删除的文件。这样,如果您决定确实需要它们,您可以随时恢复它们。
请记住,使用版本控制系统的基本规则是“跟踪一切”。小更改、损坏的更改(在分支中)等。您永远不知道何时可能再次需要这些文件或更改,因此请使用 VC 系统来确保您不会丢失它们。
Generally if they are ever going to be useful, then you should be checking them in! If you don't need them right now, then check them in and remove them! IE, track them as deleted files. That way you can always recover them later if you decide you do need them.
Remember, a fundamental rule of using a version control system is "track everything". Small changes, broken changes (in a branch), etc. You never know when you might need those files or changes again, so use your VC system to make sure you can't lose them.
使用 git-stash,但你必须先跟踪它们。如果它们被明确忽略,则不会添加它们。
现在,所有未跟踪的文件都被隐藏并从工作目录中消失。如果这是不正确的,请使用 git stash pop,添加并提交您想要保留的内容,然后重复。
Use git-stash, but you have to track them first. If they’re explicitly ignored, they won’t get added.
Now all the untracked files are stashed away and gone from the working directory. If this was incorrect, use
git stash pop
, add and commit the ones you want to keep, and repeat.另一种方法是制作 git 存储库文件夹(包括 .git/)的完整本地副本,然后删除“git ls-files”显示的所有内容。所有跟踪的文件。
类似于:
查看delete_tracked_files.sh.txt,将名称更改为delete_tracked_files.sh并执行。
您可能还想断开副本与远程的连接,只是为了防止意外拉或推。
Another approach would be to make a complete local copy of your git repository folder (including .git/) and there delete all what 'git ls-files' shows resp. all tracked files.
Something like:
Review delete_tracked_files.sh.txt, change name to delete_tracked_files.sh and execute it.
You may also want to disconnect the copy from the remote, just to prevent unintentional pulls or pushes.