Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing an existing submodule's branch

Tags:

When I initially added my submodule, I specified a particular branch, as seen in the .gitmodule file:

[submodule "externals/grpc/grpc"]     path = externals/grpc/grpc     url = [email protected]:me/grpc.git     branch = release-1.0 

I want to change to the master branch of my submodule, so I changed the branch in .gitmodules from release-1.0 to master, and for good measure, just deleted the submodule from my parent git tree:

cd $submodules rm -rf grpc cd $gitroot git submodule sync git submodule update --init --recursive 

Now, when I go back to my submodule, it's still checked out from a commit on the release-1.0 branch, not the latest master commit.

What steps am I missing to switch my submodule's branch?

like image 472
Brian D Avatar asked Apr 26 '15 20:04

Brian D


People also ask

How do I change my submodule branch?

Go into the directory where the submodule resides and git checkout the correct branch/commit. Then go up one level and git add and git commit the directory. This will check in the submodule with the correct commit. And don't forget to run git submodule update --recursive on the other clients after updating them.

Can a submodule point to a branch?

You can set the submodule to track a particular branch (requires git 1.8. 2+), which is what we are doing with Komodo, or you can reference a particular repository commit (the later requires updating the main repository whenever you want to pull in new changes from the module – i.e. updating the commit hash reference).

Can you make changes to a git submodule?

The submodule is just a separate repository. If you want to make changes to it, you should make the changes in its repository and push them like in a regular Git repository (just execute the git commands in the submodule's directory).

How do I switch my submodule's branch?

What steps am I missing to switch my submodule's branch? Go into the directory where the submodule resides and git checkout the correct branch/commit. Then go up one level and git add and git commit the directory. This will check in the submodule with the correct commit.

How to commit a submodule to a specific branch in Git?

Go into the directory where the submodule resides and git checkout the correct branch/commit. Then go up one level and git add and git commit the directory. This will check in the submodule with the correct commit. And don't forget to run git submodule update --recursive on the other clients after updating them.

How do I remove a submodule from a git repository?

In order to remove a Git submodule from your repository, use the “git submodule deinit” command followed by the “git rm” command and specify the name of the submodule folder. $ git submodule deinit <submodule> $ git rm <submodule>

How do I switch branches in Git checkout?

To switch to an existing branch, you can use git checkout again (without the -b flag) and pass the name of the branch you want to switch to: (my-feature)$ git checkout master Switched to branch 'master' (master)$ There is also a handy shortcut for returning to the previous branch you were on by passing - to git checkout instead of a branch name:


2 Answers

Go into the directory where the submodule resides and git checkout the correct branch/commit. Then go up one level and git add and git commit the directory. This will check in the submodule with the correct commit.

And don't forget to run git submodule update --recursive on the other clients after updating them.

like image 111
milgner Avatar answered Oct 10 '22 01:10

milgner


If you want to switch to a branch you've never tracked before.

After you have changed the branch in .gitmodules, do the following:

git submodule update --init --recursive --remote cd submodule_name git checkout new_branch_name 
like image 42
SergO Avatar answered Oct 10 '22 02:10

SergO