How to tell git to ignore git sub modules?

浪尽此生 提交于 2020-01-14 04:26:25

问题


My "real" .git folder is in /var/www. I want to push the whole folder to github. Unfortunately, a few git sub modules are excluded.

For example the folder w/extensions/Lingo is excluded. But w/extensions/Lingo/.git isn't "my" git sub module. Git was only used to download w/extensions/Lingo and will be used to update that folder. No own development planed.

How can I tell my "real" git /var/www/.git to ignore any sub modules such as w/extensions/Lingo/.git?

In other words, what I want to tell git is "just treat any file in /var/www/* as normal file".


回答1:


www/extensions/Lingo should not be ignored: what you will push is the gitlink (special entry, mode 16000) which represents (in the working tree of the main repo) the SHA1 currently checked out in www/extensions/Lingo.

You can check that by doing a git log (without trailing slash):

cd /var/www
git log -- extensions/Lingo



回答2:


If you insist including the content of a submodule in your repo, you can do a:

git submodule deinit Lingo 
git rm -r --cached extensions/Lingo
rm -R extensions/Lingo/.git # (if it is still there)
git add extensions/Lingo

(As I detail in "How do I remove a Git submodule?")

The --cached make sure you don't remove the content of that folder in your working tree.




回答3:


I do not recommend doing this, but it sounds like you're dead-set... Note that this will cause files from your "submodule" to be committed in two places, and keeping them in sync will be an additional challenge.

My opinion is that this is at least as complicated as submodules, and since it's a weird thing to do other developers who want to contribute will likely be confused.

You could move the .git directory out of your submodule directory so that

  1. your main Git repository does not view the "submodule" directory as a submodule, and
  2. you can still fetch from upstream.

Here is a short example:

  1. Clone something (your "inner repository") into a subdirectory of your main repository:

    ~ $ cd repo
    ~/repo/ $ git clone git://some-other-repo.git/
    
  2. Create a directory to hold the .git directory from your inner repository somewhere outside of your main repository, and move your inner .git directory there:

    ~/repo/ $ cd ..
    ~ $ mkdir -p fake-submodules/some-other-repo/
    ~ $ mv ~/repo/some-other-repo/.git fake-submodules/some-other-repo/
    
  3. Now, from your main repository you can add all of the files and commit as usual:

    ~ $ cd repo
    ~/repo/ $ git add some-other-repo
    ~/repo/ $ git commit -m "Add some other repo"
    
  4. And you can fetch updates from your upstream repository using Git's --work-tree option:

    ~/repo/ $ cd ../fake-submodules/some-other-repo
    ~/fake-submodules/some-other-repo/ $ git --work-tree ../../repo/some-other-repo status
    ~/fake-submodules/some-other-repo/ $ git --work-tree ../../repo/some-other-repo fetch
    ~/fake-submodules/some-other-repo/ $ git --work-tree ../../repo/some-other-repo merge origin/master
    

Note that you would have to perform all Git operations for your inner repository from its fake-submodules directory. You will probably want to do something like git config core.worktree ../../repo/some-other-repo to avoid having to type the --work-tree option in each time.

Again, please reconsider doing this. Git's submodules may not be perfect, but at least they're widely used and understood. As VonC says, there are other more appropriate options as well, like subtrees.



来源:https://stackoverflow.com/questions/23362967/how-to-tell-git-to-ignore-git-sub-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!