如何快速备份我想通过 git clean 删除的未跟踪文件?

发布于 2024-10-20 05:12:08 字数 76 浏览 2 评论 0原文

我有很多未跟踪的文件。我很确定我可以删除其中的大多数,但是...您知道,备份可能会有所帮助;)

您在类似情况下正在做什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静水深流 2024-10-27 05:12:08

以下命令将在您的主目录中创建一个 tar 存档,其中包含目录中所有未跟踪(且未被忽略)的文件:

git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup-untracked.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:

git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup-untracked.tar

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 get git ls-files to output the list of files with NUL (a zero byte) as the separator between files, and the -0 parameter to xargs tells it to consider NUL 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 are NUL and /.

  • If you have a huge number of untracked files then xargs will run the tar command more than once, so it's important that I've told tar to append files (r) rather than create a new archive (c), otherwise the later invocations of tar will overwrite the archive created just before.

森林散布 2024-10-27 05:12:08

一般来说,如果它们有用,那么您应该签入它们!如果您现在不需要它们,请将它们签入并删除! 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.

初心 2024-10-27 05:12:08

使用 git-stash,但你必须先跟踪它们。如果它们被明确忽略,则不会添加它们。

git add .
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.

git add .
git stash

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.

如果没有你 2024-10-27 05:12:08

另一种方法是制作 git 存储库文件夹(包括 .git/)的完整本地副本,然后删除“git ls-files”显示的所有内容。所有跟踪的文件。

类似于:

cd copy_of_repository
git ls-files | xargs printf "rm \"%s\"\n" > delete_tracked_files.sh.txt

查看delete_tracked_files.sh.txt,将名称更改为delete_tracked_files.sh并执行。

您可能还想断开副本与远程的连接,只是为了防止意外拉或推。

git remote remove origin

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:

cd copy_of_repository
git ls-files | xargs printf "rm \"%s\"\n" > delete_tracked_files.sh.txt

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.

git remote remove origin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文