Joey Yi Zhao
Joey Yi Zhao

Reputation: 42546

How to reset submodule to repo's master branch?

I have a repo in github which has some submodules. When I run git status in my repo I see the submodules are changed:

    modified:   ci (new commits)
    modified:   metadata (new commits)

How can I reset my local submodule to be same as origin/master submodule reference? I have tried git reset --hard origin/master which only reset local files not submodules.

In addition, I don't have any local changes in these submodules. I think what happens is that I run a git pull in the submodule which makes the commit id referenced by my repo changed.

Upvotes: 5

Views: 6533

Answers (1)

torek
torek

Reputation: 488579

A submodule is a Git repository.

If you want to change which commit is checked out in a Git repository, you enter the Git repository and run git checkout there. So you could:

(cd ci && git checkout origin/master)

for instance to change the commit that is checked out in the Git repository in the ci directory. You can repeat this for the metadata sub-directory that contains the metadata Git repository.

Note that this does not change the commit hash IDs recorded in the superproject (the current Git repository). That superproject lists which hash ID the superproject Git should use when the superproject Git runs:

(cd <submodule> && git checkout <hash-id>)

If you've changed the hash IDs you'd like the superproject to check out—by entering the submodule Git repositories and checking out other commits, for instance—you should run git add in the superproject to record the new hash IDs into the index for the superproject repository:

git add ci metadata

and then run git commit in the superproject repository, to make a new commit that records these new hash IDs.

The git submodule command can do some of this for you:

git submodule update

tells your superproject Git to run:

(cd <submodule> && git checkout <hash-id>)

for each submodule according to the hash ID currently stored in the index in the superproject. On the other hand:

git submodule update --remote

tells your superproject Git to run:

(cd <submodule> && git fetch && git checkout origin/master)

in each submodule. The name origin/master actually depends on configuration settings; that's just the default. There are many additional flags to git submodule update to make it do other things.

(I prefer to just enter the submodule and do the things myself, so that I can see what I am doing.)

Upvotes: 3

Related Questions