git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v5 2/2] submodule add: If --branch is given, record it in .gitmodules

From
W. Trevor King <wking@tremily.us>
Date
Nov 28, 2012, 19:30 UTC
Message-ID
<572f6aee10162597c4544522330262e877e74992.1354130656.git.wking@tremily.us>
In-Reply-To
<cover.1354130656.git.wking@tremily.us>
From: "W. Trevor King" <wking@tremily.us>

This allows you to easily record a submodule.<name>.branch option in .gitmodules when you add a new submodule. With this patch,

  $ git submodule add -b <branch> <repository> [<path>]
  $ git config -f .gitmodules submodule.<path>.branch <branch>
reduces to
  $ git submodule add -b <branch> <repository> [<path>]
This means that future calls to
  $ git submodule update --remote ...

will get updates from the same branch that you used to initialize the submodule, which is usually what you want.

Signed-off-by: W. Trevor King <wking@tremily.us>
---
 Documentation/git-submodule.txt | 2 ++
 git-submodule.sh                | 4 ++++
 t/t7400-submodule-basic.sh      | 1 +
 3 files changed, 7 insertions(+)
diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt
index 39aa02d..43e5b4b 100644
--- a/Documentation/git-submodule.txt
+++ b/Documentation/git-submodule.txt
@@ -208,6 +208,8 @@ OPTIONS
 -b::
 --branch::
 	Branch of repository to add as submodule.
+	The name of the branch is recorded as `submodule.<path>.branch` in
+	`.gitmodules` for `update --remote`.
 
 -f::
 --force::
diff --git a/git-submodule.sh b/git-submodule.sh
index b63d869..1f76893 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -368,6 +368,10 @@ Use -f if you really want to add it." >&2
 
 	git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
 	git config -f .gitmodules submodule."$sm_path".url "$repo" &&
+	if test -n "$branch"
+	then
+		git config -f .gitmodules submodule."$sm_path".branch "$branch"
+	fi &&
 	git add --force .gitmodules ||
 	die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
 }
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 5397037..90e2915 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -133,6 +133,7 @@ test_expect_success 'submodule add --branch' '
 	(
 		cd addtest &&
 		git submodule add -b initial "$submodurl" submod-branch &&
+		test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
 		git submodule init
 	) &&
 
-- 
1.8.0.2.gad10246.dirty
Previous: W. Trevor King
Message 5 of 5 in “Re: [PATCH v4 0/4] git-submodule add: Add --local-branch option (summary)”
  1. W. Trevor KingNov 28, 2012
  2. W. Trevor KingNov 28, 2012
  3. 0/2 submodule update: add --remote for submodule's upstream changesW. Trevor King, Nov 28, 2012
  4. 1/2 submodule update: add --remote for submodule's upstream changesW. Trevor King, Nov 28, 2012
  5. 2/2 submodule add: If --branch is given, record it in .gitmodulesW. Trevor King, Nov 28, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.