Git 忽略 .git 文件夹
2022-08-31 01:21:28
你可以使用 git 钩子来实现你想要的。开箱即用,您可以使用预提交钩子来重命名所包含项目的.git目录,例如。到“.git2”,添加后一个项目中的所有文件,除了“.git2”目录,全部提交,推送它,最后使用提交后钩子将模块中的“.git2”文件夹重命名回“.git”。
1) 在根存储库的 .git/hooks/ 下创建预提交文件,内容如下:
#!/bin/sh
mv "vendor/modulename/.git" "vendor/modulename/.git2"
git rm --cached vendor/modulename
git add vendor/modulename/*
git reset vendor/modulename/.git2
2) 在 .git/hooks/ 下创建提交后文件,同时包含以下内容:
#!/bin/sh
mv "vendor/modulename/.git2" "vendor/modulename/.git"
3) 更改存储库中的文件,最后:
git commit -a -m "Commit msg"
git push
看起来 git 会自动忽略根存储库子文件夹中的 .git 文件夹。
(master)[/tmp]
$ mkdir test_root
(master)[/tmp]
$ git init test_root
Initialized empty Git repository in /tmp/test_root/.git/
(master)[/tmp]
$ cd test
test/ test_root/
(master)[/tmp]
$ cd test_root/
(master)[/tmp/test_root] (master)
$ ls
(master)[/tmp/test_root] (master)
$ git init test_child
Initialized empty Git repository in /tmp/test_root/test_child/.git/
(master)[/tmp/test_root] (master)
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
(master)[/tmp/test_root] (master)
$ touch test_root_file
(master)[/tmp/test_root] (master)
$ cd test_child/
(master)[/tmp/test_root/test_child] (master)
$ ls
(master)[/tmp/test_root/test_child] (master)
$ touch test_child_file
(master)[/tmp/test_root/test_child] (master)
$ cd ..
(master)[/tmp/test_root] (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test_child/
test_root_file
nothing added to commit but untracked files present (use "git add" to track)
(master)[/tmp/test_root] (master)
$ git add test_child/test_child_file
(master)[/tmp/test_root] (master)
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test_child/test_child_file
Untracked files:
(use "git add <file>..." to include in what will be committed)
test_root_file
(master)[/tmp/test_root] (master)
$ cd test_child/
(master)[/tmp/test_root/test_child] (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test_child_file
nothing added to commit but untracked files present (use "git add" to track)
(master)[/tmp/test_root/test_child] (master)
$ git --version
git version 1.9.1
$ git add test_root_file
(master)[/tmp/test_root] (master)
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test_child/test_child_file
new file: test_root_file
(master)[/tmp/test_root] (master)
$ git commit -m'1 commit'
[master (root-commit) 4d4b695] 1 commit
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test_child/test_child_file
create mode 100644 test_root_file
(master)[/tmp/test_root] (master)
$ git show
commit 4d4b69589bf4f471c3c784f95f447d2a40ee6d7d
Author: Evgenii Shchemelev
Date: Wed Jan 6 09:20:03 2016 +0200
1 commit
diff --git a/test_child/test_child_file b/test_child/test_child_file
new file mode 100644
index 0000000..e69de29
diff --git a/test_root_file b/test_root_file
new file mode 100644
index 0000000..e69de29