How to revert a Git Submodule pointer to the commit stored in the containing repository?

前端 未结 7 1135
感动是毒
感动是毒 2021-01-29 22:44

I have a git submodule in my main git repo. As I understand it, the main repo stores a SHA value (somewhere...), pointing to the specific commit of the submodule that it is \"li

相关标签:
7条回答
  • 2021-01-29 23:12

    I wanted to disregard any changes in submodule and in my module also

    The below command helped me:

    git submodule update --init --recursive
    
    0 讨论(0)
  • You want to update your submodule so it is in sync with what the parent repository believes it should be. This is what the update command is for:

    From the submodule manpage:

    Update the registered submodules, i.e. clone missing submodules and
    checkout the commit specified in the index of the containing
    repository. This will make the submodules HEAD be detached unless
    --rebase or --merge is specified or the key submodule.$name.update
    is set to rebase or merge.
    

    Run this and all should be well:

    git submodule update --init
    

    You can add the --recursive flag as well to recurse through all submodules.

    0 讨论(0)
  • 2021-01-29 23:16

    The answer here somehow didn't solve my specific issue with the submodule so in case it happens to you too, try the following...

     git submodule foreach git reset --hard
    

    https://kalyanchakravarthy.net/blog/git-discard-submodule-changes/

    0 讨论(0)
  • 2021-01-29 23:20

    An updated visual walk through - here my submodule is pointing at - 8c3fd8b It would be straightforward to go to this folder and....

    git checkout master
    error: pathspec 'master' did not match any file(s) known to git
    

    In the submodule eg. stylegan2-ada folder needed to run

    git checkout main
    git pull
    

    this resolved my problem (temporarily)

    last step was to commit the changes in parent repo. The tracked changes appeared automatically - and I didn't need to create a file - just stage the file changes by clicking the + in vscode and pushing.

    here very clearly you can see the git commits refreshing.

    -Subproject commit 8c3fd8bac3e5a54a20e860fc163d6e67cc03c623
    +Subproject commit 0045ea50f1b9710ffde3ab2a9a18c0d4c97bfaed
    

    Troubleshooting

    Beware that there's a .gitsubmodule file that points to respective git remote repo.

    0 讨论(0)
  • 2021-01-29 23:23

    Another case I just ran into is if there is an unstaged change in the submodule that you want to discard. git submodule update will not remove that change, nor will git reset --hard on the parent directory. You need to go to the submodule directory and do a git reset --hard. So if I want to fully discard unstaged changes in both my parent and submodule, I do the following:

    In Parent:

    git reset --hard
    
    git submodule update
    

    In Submodule:

    git reset --hard
    
    0 讨论(0)
  • 2021-01-29 23:26

    To change the commit that a submodule points to, you need to checkout that version in the submodule, then go back to the containing repo, add and commit that change.

    Or, if you want the submodule to be on the version the top repo points to, do git submodule update --recursive. Add --init if you've just cloned.

    Also, git submodule without a submodule command will show you the commit you are pointing to. There will be a - or a + in front of the commit if it's not in sync.

    If you look at a tree with a submodule in it, you can see that the submodule is marked as a commit as opposed to the rest that are blobs or trees.

    to see what a particular commit points wrt to submodules you can:

    git ls-tree <some sha1, or branch, etc> Submodule/path
    

    you can then see the commit or anything else if you like by passing that into log, etc (the git-dir option at the git command level allows you to skip having to cd down to the submodule):

    git --git-dir=Submodule/path log -1 $(<the above statement>)
    
    0 讨论(0)
提交回复
热议问题