简体   繁体   English

如何从目录中删除除未版本控制的 git 文件之外的所有文件?

[英]How to remove all but the unversioned git files from a directory?

I wanted to put a lot of files in a repository.我想把很多文件放在一个存储库中。 However, their size exceeds 10GB.但是,它们的大小超过 10GB。 And they are a lot of files.它们是很多文件。 The total storage is around 13gb.总存储空间约为 13GB。

I am thinking of ways to go about this.我正在考虑解决这个问题的方法。 Now I am thinking of creating a separate repository where I could push the rest of the files.现在我正在考虑创建一个单独的存储库,我可以在其中推送其余文件。 But since there are many files, it would be too tedious to do that manually.但是因为有很多文件,手动操作太繁琐了。

Is there some way to see which files have not been added to git (maybe they are called unversioned files)?有什么方法可以查看哪些文件没有添加到 git 中(也许它们被称为未版本控制的文件)? and then remove all but the unadded files.然后删除所有未添加的文件。 And then init a new repo on the same folder and push the rest to the other repository.然后在同一个文件夹中初始化一个新的 repo 并将其余的推送到另一个存储库。

Please let me know if this is possible.请让我知道这是否可行。 I am open to using shell or a python wrapper around git if that exists.如果存在,我愿意在 git 周围使用 shell 或 python 包装器。

The files are unrelated to each other so it's not a big deal that they would be saved in two separate repositories.这些文件彼此无关,因此将它们保存在两个单独的存储库中并不是什么大问题。

To find files that haven't been committed:要查找尚未提交的文件:

git status --porcelain --untracked-files --ignored

This will show you all file paths with changes, including those in .gitignore , and descending into directories.这将向您显示所有更改的文件路径,包括.gitignore文件路径,以及下降到目录中的文件路径。 If you have no pending changes in your repo, this should only be untracked/ignored files.如果您的 repo 中没有挂起的更改,这应该只是未跟踪/忽略的文件。 There will be 3 chars at the beginning of the lines like ??行的开头会有 3 个字符,例如?? or !!或者!! representing the status that you can strip off with | cut -c 4-代表你可以剥离的状态| cut -c 4- | cut -c 4- unless you want to parse them to distinguish different statuses. | cut -c 4-除非你想解析它们以区分不同的状态。

From there - it would be easier to move these files to a different directory (rather than moving all the other files).从那里 - 将这些文件移动到不同的目录会更容易(而不是移动所有其他文件)。

List all tracked files :列出所有跟踪的文件

git ls-tree --full-tree -r --name-only HEAD

Remove all tracked files:删除所有跟踪的文件:

git ls-tree --full-tree -r --name-only HEAD |
  xargs git rm

My approach would be to purely use Git:我的方法是纯粹使用 Git:

git status # Make sure you don’t have any change in already tracking files
cat .gitignore # If you ignore some files that you want to actually index, modify this file
git add . # Adding all untracked files
git commit # Take note of this commit hash, abcde123
cd ../another_new_repo
git init 
git fetch ../old_repo
git cherry-pick abcde123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM