如何摆脱 Git 子模块未跟踪状态?

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

How to get rid of Git submodules untracked status?

gitgit-submodules

提问by Tomer Lichtash

I can't seem to get rid of untracked content in Git's submodules. Running git statusyields:

我似乎无法摆脱 Git 子模块中未跟踪的内容。运行git status产量:

# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#   (commit or discard the untracked or modified content in submodules)
#
#    modified:   bundle/snipmate (untracked content)
#    modified:   bundle/surround (untracked content)
#    modified:   bundle/trailing-whitespace (untracked content)
#    modified:   bundle/zencoding (untracked content)
#
no changes added to commit (use "git add" and/or "git commit -a")

Adding the --ignore-submodulesparameter hides these messages; but I wonder if there's a way to get rid of this dirt in a more suitable, core-ish, manner.

添加--ignore-submodules参数会隐藏这些消息;但我想知道是否有办法以更合适的核心方式清除这些污垢。

采纳答案by VonC

Since the git status reports untracked content, the actual way to have a clean status would be to go into each one of those submodules and:

由于 git status 报告未跟踪的内容,因此获得干净状态的实际方法是进入这些子模块中的每一个,并且:

  • add and commit the untracked contents,
  • or reference the untracked contents in a .gitignorespecific to each module.
  • or you can add the same ignored content to the submodule's .git/info/exclude, as peci1reports in the comments.
  • or add dirty to the submodule specification, as mentioned in ezraspectre's answer(upvoted).

    git config -f .gitmodules submodule.<path>.ignore untracked
    
  • or add a global.gitignorefile (often ~/.gitignore-global). Like for example .DS_Storeor in my case Carthage/Buildas reported by Marián ?ernyin the comments. See .gitginoreman page:

  • 添加并提交未跟踪的内容,
  • 或引用.gitignore特定于每个模块的未跟踪内容。
  • 或者您可以将相同的忽略内容添加到子模块的.git/info/exclude,如peci1在评论中报告的那样
  • 或将脏添加到子模块规范中,如ezraspectre回答(已投票)中所述。

    git config -f .gitmodules submodule.<path>.ignore untracked
    
  • 或添加一个全局.gitignore文件(通常~/.gitignore-global)。例如,.DS_Store或者在我的情况下,Carthage/Build正如Marián ?erny评论中所报告的那样。请参阅.gitginore手册页

Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user's editor of choice) generally go into a file specified by core.excludesFilein the user's ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOMEis either not set or empty, $HOME/.config/git/ignoreis used instead.

用户希望 Git 在所有情况下忽略的模式(例如,由用户选择的编辑器生成的备份或临时文件)通常会放入用户的 .git 文件中指定的文件core.excludesFile~/.gitconfig。其默认值为$XDG_CONFIG_HOME/git/ignore。如果$XDG_CONFIG_HOME未设置或为空,$HOME/.config/git/ignore则改为使用。

回答by ezraspectre

I found this blog postto work overall. By adding the ignore = dirtyoption to each one of the entries in the .gitmodulesfile.

我发现这篇博文总体上有效。通过将ignore = dirty选项添加到文件中的每个条目.gitmodules

[submodule "zen-coding-gedit3"]
    path = zen-coding-gedit3
    url = git://github.com/leafac/zen-coding-gedit3.git
    ignore = dirty

回答by Ricardo Martins

You can also go to each submodule dir and act as a separated git. For example:

您还可以转到每个子模块目录并充当单独的 git。例如:

cd my/project/submodule
git status

... /gets the list of modified files/

... /获取修改文件列表/

git add .  //to add all of them to commit into submodule
git commit -m "message to your submodule repo"

you can also update your remote submodule repo with

您还可以使用以下命令更新您的远程子模块存储库

git submodule update

after all

毕竟

回答by Browny Lin

It would be due to the detached HEADin your submodule branch. Go into your submodule path (ex: ./bundle/snipmate), then git checkout master.

这将是由于detached HEAD在您的子模块分支中。进入您的子模块路径(例如:)./bundle/snipmate,然后git checkout master.

I hope this will help :)

我希望这个能帮上忙 :)

回答by u1860929

I got stuck on this issue yesterday, in a project which had close to 12 submodules.

昨天我在一个有近 12 个子模块的项目中遇到了这个问题。

git statuswas showing output.

git status正在显示输出。

# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#   (commit or discard the untracked or modified content in submodules)
#
#   modified:   proj1 (untracked content)
#   modified:   proj1 (modified content, untracked content)
#   ...

To resolve the untracked content error, I had to remove the untracked files from all submodules (all were *.pyc, *.pyofiles generated by python) using a .gitignore.

要解决未跟踪内容错误,我不得不删除所有子模块未跟踪文件(均为*.pyc*.pyo使用由蟒蛇生成的文件).gitignore

To resolve the other, I had to run git submodule updatewhich updated each of the submodules.

为了解决另一个问题,我必须运行git submodule update它来更新每个子模块。

回答by HappyCoder

In my situation I clone modules as a starting point for a new module in my ZF2 environment. What this does is put its own .git folder into the directory.

在我的情况下,我克隆模块作为 ZF2 环境中新模块的起点。这样做是将它自己的 .git 文件夹放入目录中。

Solution in this case is to delete the .git folder (you will likely need to show hidden files to view it).

在这种情况下的解决方案是删除 .git 文件夹(您可能需要显示隐藏文件才能查看它)。

回答by DarkCrazy

This worked out just fine for me:

这对我来说很好:

git update-index --skip-worktree

git update-index --skip-worktree

If it doesn't work with the pathname, try the filename. Let me know if this worked for you too.

如果路径名不起作用,请尝试文件名。让我知道这是否也适合您。

Bye!

再见!

回答by Subrat Kumar Palhar

This probably happens when you have another .git [hidden folder] inside the particular folder..

当您在特定文件夹中有另一个 .git [隐藏文件夹] 时,可能会发生这种情况。

modified: ./../.. (modified content, untracked content)

修改:./../..(修改内容,未跟踪内容)

make sure your sub-directory does not contains this .git folder.

确保您的子目录不包含此 .git 文件夹。

If That is the case the issue can be resolved by deleting the .git folder manually from the sub directory.

如果是这种情况,可以通过从子目录中手动删除 .git 文件夹来解决问题。

回答by Glenn Lawrence

I prefer to use SourceTree, so the solution for me was to open the submodule repo in SourceTree which shows me list of all untracked files. I then group selected them all then used "Remove".

我更喜欢使用SourceTree,所以我的解决方案是在 SourceTree 中打开子模块 repo,它显示了所有未跟踪文件的列表。然后我将它们全部分组,然后使用“删除”。

I was able to do this because I knew all the untracked files weren't actually needed.

我能够这样做是因为我知道实际上并不需要所有未跟踪的文件。

回答by daigo

If this is a temporary issue, you can go into the submodule folder and run git reset HEAD --hardbut you will lose all your changes inside of the submodule.

如果这是一个临时问题,您可以进入子模块文件夹并运行,git reset HEAD --hard但您将丢失子模块内的所有更改。