从目录中删除所有 git 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4822321/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:56:57  来源:igfitidea点击:

Remove all git files from a directory?

git

提问by user291701

I have a folder under version control. I want to make a copy of it to send around, but don't want to include all the .git directories and the files underneath it.

我有一个受版本控制的文件夹。我想制作它的副本以发送出去,但不想包含所有 .git 目录和它下面的文件。

Is there a way to remove all the git files instead of manually deleting all of them?

有没有办法删除所有 git 文件而不是手动删除所有文件?

回答by Chris Kooken

The .git folder is only stored in the root directory of the repo, not all the sub-directories like subversion. You should be able to just delete that one folder, unless you are using Submodules...then they will have one too.

.git 文件夹只存储在 repo 的根目录中,而不是像 subversion 这样的所有子目录。您应该能够删除那个文件夹,除非您使用的是子模块……那么它们也会有一个。

回答by Eric Leschinski

How to remove all .gitdirectories under a folder in Linux.

如何.git在Linux中删除文件夹下的所有目录。

Run this find command, it will list all .gitdirectories under the current folder:

运行此 find 命令,它将列出.git当前文件夹下的所有目录:

find . -type d -name ".git" \
&& find . -name ".gitignore" \
&& find . -name ".gitmodules"

Prints:

印刷:

./.git
./.gitmodules
./foobar/.git
./footbar2/.git
./footbar2/.gitignore

There should only be like 3 or 4 .gitdirectories because git only has one .git folder for every project. You can rm -rf yourpatheach of the above by hand.

应该只有 3 或 4 个.git目录,因为 git 对于每个项目只有一个 .git 文件夹。您可以rm -rf yourpath手动完成上述每项操作。

If you feel like removing them all in one command and living dangerously:

如果您想在一个命令中将它们全部删除并危险地生活:

//Retrieve all the files named ".git" and pump them into 'rm -rf'
//WARNING if you don't understand why/how this command works, DO NOT run it!

( find . -type d -name ".git" \
  && find . -name ".gitignore" \
  && find . -name ".gitmodules" ) | xargs rm -rf

//WARNING, if you accidentally pipe a `.` or `/` or other wildcard
//into xargs rm -rf, then the next question you will have is: "why is
//the bash ls command not found?  Requiring an OS reinstall.

回答by jweyrich

You can use git-archive, for example:

您可以使用git-archive,例如:

git archive master | bzip2 > project.tar.bz2

Where masteris the desired branch.

master所需的分支在哪里。

回答by zePiet

cdto the repository then

cd然后到存储库

find . -name ".git*" -exec rm -R {} \;

Make sure not to accidentally pipe a single dot, slash, asterisk, or other regex wildcard into find, or else rmwill happily delete it.

确保不要意外地将单个点、斜线、星号或其他正则表达式通配符通过管道传输到 find 中,否则rm会很乐意将其删除。

回答by OldTinfoil

In case someone else stumbles onto this topic - here's a more "one size fits all" solution.

万一其他人偶然发现这个话题 - 这里有一个更“一刀切”的解决方案。

If you are using .gitor .svn, you can use the --exclude-vcsoption for tar. This will ignore many different files/folders required by different version control systems.

如果您使用.git.svn,则可以使用--exclude-vcstar 选项。这将忽略不同版本控制系统所需的许多不同文件/文件夹。

If you want to read more about it, check out: http://www.gnu.org/software/tar/manual/html_section/exclude.html

如果您想了解更多信息,请查看:http: //www.gnu.org/software/tar/manual/html_section/exclude.html

回答by Adrian Petrescu

Unlike other source control systems like SVN or CVS, git stores all of its metadata in a singledirectory, rather than in every subdirectory of the project. So just delete .gitfrom the root (or use a script like git-export) and you should be fine.

与 SVN 或 CVS 等其他源代码控制系统不同,git 将其所有元数据存储在单个目录中,而不是项目的每个子目录中。因此,只需.git从根目录中删除(或使用git-export 之类的脚本),您应该没问题。

回答by Daniel Teichman

ls | xargs find 2>/dev/null | egrep /\.git$ | xargs rm -rf

ls | xargs 查找 2>/dev/null | egrep /\.git$ | xargs rm -rf

This command (and it is just one command) will recursively remove .git directories (and files) that are in a directory without deleting the top-level git repo, which is handy if you want to commit all of your files without managing any submodules.

这个命令(它只是一个命令)将递归删除目录中的 .git 目录(和文件)而不删除顶级 git repo,如果你想提交所有文件而不管理任何子模块,这很方便.

find 2>/dev/null | egrep /\.git$ | xargs rm -rf

查找 2>/dev/null | egrep /\.git$ | xargs rm -rf

This command will do the same thing, but will also delete the .git folder from the top-level directory.

此命令将执行相同的操作,但还会从顶级目录中删除 .git 文件夹。