4

How do I change branches from within a submodule? When I run git branch from within the submodule, I see the following output:

> git branch
  * (HEAD detached from 229a7b2)
  master

How would I put myself on a new branch? Like development?

Apollo
  • 8,874
  • 32
  • 104
  • 192

1 Answers1

10

Simply list your branches:

git branch -avv

And then checkout the one you want

git checkout -b myBranch origin/mybranch

Or create a new development branch from the commit you currently are:

git checkout -b development

A submodule is always checked out as a detached HEAD (meansing at a SHA1)

When you change that, and make any new commit (or change the current commit by a branch checkout), don't forget to:

  • push that commit to the submodule remote repository (its own origin)
  • go to the parent repository, and add, commit and push the new submodule SHA1.
    The parent repository stores said submodule SHA1 as a gitlink, a special entry in its index.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • great, thanks! For my specific purposes I never make commits from the submodule, so I guess I'll just need to do `git checkout -b myBranch origin/myBranch` – Apollo Sep 24 '16 at 05:13
  • @Apollo you can make new commits from within a submodule: see http://stackoverflow.com/a/1979194/6309. And you can configure your submodule to follow a branch that way the parent repo can update a submodule to the latest fetched commit of that branch: http://stackoverflow.com/a/9189815/6309 – VonC Sep 24 '16 at 05:14
  • I see, so after I checkout to `myBranch` from within the submodule, I can pull new changes from `myBranch` by doing `git pull origin myBranch` right? – Apollo Sep 24 '16 at 05:19
  • @Apollo Yes, the submodule is a git repo in its own right, with all the usual workflow of a Git repo (fetch/pull/push). You just need to remember to add, commit and push the gitlink in the parent repo (ie record the new state of your submodule in the parent repo) – VonC Sep 24 '16 at 05:22
  • Ok great, thank you. So I performed `git checkout -b myBranch origin/mybranch`, but now I don't see `* (HEAD detached from 229a7b2)` anymore. I only see `myBranch` and `master` when I do `git branch`. And when I did `git checkout master` I see `Switched to branch master. Your branch is behind 'origin/master' by 138 commits, and can be fast-forwarded.` Did I do something wrong? – Apollo Sep 24 '16 at 05:27
  • @Apollo no it depends on where you want to create your branch. Again that submodule is a Git repo, so simply need checkout the right branch. – VonC Sep 24 '16 at 05:37
  • ok. I've been reading up on your other posts and I think I'm starting to understand how this works. Thank you for being so helpful! – Apollo Sep 24 '16 at 05:39