3

Is it possible to only checkout a single branch as a submodule WITHOUT the overhead of the whole repository in my main repositories working directory? (And really only download the files of that branch)

I think of having a submodule for a javascript library with a branch called "dist" containing only the minified compiled ready-to-use js file. The "master" would instead contain the source files and a bunch of other stuff only needed for development of the lib.

Danny Raufeisen
  • 953
  • 8
  • 21
  • Possible duplicate of [Git submodules: Specify a branch/tag](https://stackoverflow.com/questions/1777854/git-submodules-specify-a-branch-tag) – phd Feb 18 '18 at 23:23
  • I have edited my answer, but the key is: you will checkout the branch you want, however, you still need tob fetch the all repo. – VonC Feb 19 '18 at 08:44
  • Although, there is a single branch option: see my edit. – VonC Feb 19 '18 at 08:52

2 Answers2

2

Note that a submodule only represents a fixed SHA1.

That being said, on git submodule update --remote, you can make a submodule change its SHA1 by the latest of a given branch by following my answer "git submodule tracking latest"

For an existing submodule:

git config -f .gitmodules submodule.<path>.branch <branch>

A repo can add itself as a submodule, with a given branch.
I used to do so for the gh-pages of a GitHub pages repo.

Since a submodule is a sub-repo, you will download the repo, and only checkout the branch you need.
Although, there is a --single-branch option to git clone which does limit the clone to a single branch.
You can try and add again your submodule with the -b (git submodule add -b abranch) option to check that is did clone the remote submodule repo with only that one branch in it.
See "Is a full clone the only way to submodule add a branch?".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Adding Submodule Branch(Submodule1Branch) inside MainProjectBranch In Same Repository Process :-

Create Branch of Submodule(treated as Repository):-

  • Create 1 Branch as Submodule1Branch from Master.

  • Clone Submodule1Branch in your local machine and delete everything of Master in it and push with respective Project of Submodule1Branch only by given commands:-

Add this Submodule1Branch as a Submodule of your MainProjectBranch in same repo:-

  • Create a branch from “Master” and give name it “ MainProjectBranch”.
  • Clone this branch in you local.
  • Now Perform Submodule on this branch by given commands:-
  • git submodule add -b Submodule1Branch https://Your@bitbucket.com/yourmasterporject.git Submodules/
  •  git status
  • cat .gitmodules
  •  git add .
  • git commit -m "Added Submodule1Branch As Submodule of MainProjectBranch."
  • git push

Result :- You may go one bitbucket or git server and check MainProjectBranch there you will find Submodule1Branch as submodule of MainProjectBranch.

Note:- You can perform same above steps for separate repositories as well .(Ex:- submodule repo will be different and main project repo will be different). Happy coding!! :).

Abhimanyu Daspan
  • 1,095
  • 16
  • 20