How to change git subrepo to track a differentt branch?

1.1k views Asked by At

I have a subrepo in an ext/[subrep] directory that I want to track a different branch.

I have followed the instructions for switching subrepo branches at https://github.com/ingydotnet/git-subrepo/wiki/FAQ however, it does not seem to be working.

First, since the code in the subrepo and its parent are not production ready code, I want this subrepo to be on an independent branch.

For better reference tracking, I created a new branch, then merged all of the existing work from the previously tracked branch into the new branch.

If I follow the instructions explicitly, that is without specifying the exact directory where the subrepo is located, I get a new directory instead simply changing the branch reference in the .gitrepo file.

After having merged my existing work with the new branch, if I then specify the remote URL, the branch I want to track, and the existing directory, the clone simply tells me that the subrepo is up-to-date without changing anything - even with the --force flag specified.

What I have had to do is to

  1. git subrepo clean ext/[subrepodir]
  2. git rm -r -f ext/[subrepodir]
  3. physically delete ext/[subrepodir] (cloning the subrepo here without first deleting the directory gives the directory not empty error even with the --force flag.
  4. git commit
  5. git subrepo clone [remoteURL] ext/[subrepodir] --force -b new_branch

The result of the above is that the subrepo is properly tracking the new branch. However, from the FAQ it seems like I should not have to go through all of this just to switch the branch that the subrepo is tracking.

Is this a bug?

2

There are 2 answers

3
kcoul On BEST ANSWER

It looks like this is possible using the syntax:

git subrepo <subrepo_name> pull -b <branch_name>

I needed to first invoke (in at least some situations):

git subrepo clean <subrepo_name> 

https://github.com/ingydotnet/git-subrepo/issues/336#issuecomment-663901849

2
MadHatter On

I never used submodule before, and come across an artical which may resolve your problem, pls read it, it is well written. About switching the default branch in a submodule, run the following command:

# "DbConnector" is the submodule name, and "stable" is the expected branch of the submodule
git config -f .gitmodules submodule.DbConnector.branch stable

Reference artical: https://git-scm.com/book/en/v2/Git-Tools-Submodules