@@ -369,6 +369,11 @@ shorthand:
369369The full name is occasionally useful if, for example, there ever
370370exists a tag and a branch with the same name.
371371
372+ (Newly created refs are actually stored in the .git/refs directory,
373+ under the path given by their name. However, for efficiency reasons
374+ they may also be packed together in a single file; see
375+ gitlink:git-pack-refs[1]).
376+
372377As another useful shortcut, the "HEAD" of a repository can be referred
373378to just using the name of that repository. So, for example, "origin"
374379is usually a shortcut for the HEAD branch in the repository "origin".
@@ -2189,9 +2194,9 @@ test|release)
21892194 git checkout $1 && git pull . origin
21902195 ;;
21912196origin)
2192- before=$(cat . git/ refs/remotes/origin/master)
2197+ before=$(git rev-parse refs/remotes/origin/master)
21932198 git fetch origin
2194- after=$(cat . git/ refs/remotes/origin/master)
2199+ after=$(git rev-parse refs/remotes/origin/master)
21952200 if [ $before != $after ]
21962201 then
21972202 git log $before..$after | git shortlog
@@ -2216,11 +2221,10 @@ usage()
22162221 exit 1
22172222}
22182223
2219- if [ ! -f .git/refs/heads/"$1" ]
2220- then
2224+ git show-ref -q --verify -- refs/heads/"$1" || {
22212225 echo "Can't see branch <$1>" 1>&2
22222226 usage
2223- fi
2227+ }
22242228
22252229case "$2" in
22262230test|release)
@@ -2251,7 +2255,7 @@ then
22512255 git log test..release
22522256fi
22532257
2254- for branch in `ls .git/refs/heads `
2258+ for branch in `git show-ref --heads | sed 's|^.*/||' `
22552259do
22562260 if [ $branch = test -o $branch = release ]
22572261 then
@@ -2946,7 +2950,7 @@ nLE/L9aUXdWeTFPron96DLA=
29462950See the gitlink:git-tag[1] command to learn how to create and verify tag
29472951objects. (Note that gitlink:git-tag[1] can also be used to create
29482952"lightweight tags", which are not tag objects at all, but just simple
2949- references in .git/ refs/tags/).
2953+ references whose names begin with " refs/tags/" ).
29502954
29512955[[pack-files]]
29522956How git stores objects efficiently: pack files
@@ -3155,6 +3159,208 @@ a tree which you are in the process of working on.
31553159If you blow the index away entirely, you generally haven't lost any
31563160information as long as you have the name of the tree that it described.
31573161
3162+ [[submodules]]
3163+ Submodules
3164+ ==========
3165+
3166+ This tutorial explains how to create and publish a repository with submodules
3167+ using the gitlink:git-submodule[1] command.
3168+
3169+ Submodules maintain their own identity; the submodule support just stores the
3170+ submodule repository location and commit ID, so other developers who clone the
3171+ superproject can easily clone all the submodules at the same revision.
3172+
3173+ To see how submodule support works, create (for example) four example
3174+ repositories that can be used later as a submodule:
3175+
3176+ -------------------------------------------------
3177+ $ mkdir ~/git
3178+ $ cd ~/git
3179+ $ for i in a b c d
3180+ do
3181+ mkdir $i
3182+ cd $i
3183+ git init
3184+ echo "module $i" > $i.txt
3185+ git add $i.txt
3186+ git commit -m "Initial commit, submodule $i"
3187+ cd ..
3188+ done
3189+ -------------------------------------------------
3190+
3191+ Now create the superproject and add all the submodules:
3192+
3193+ -------------------------------------------------
3194+ $ mkdir super
3195+ $ cd super
3196+ $ git init
3197+ $ for i in a b c d
3198+ do
3199+ git submodule add ~/git/$i
3200+ done
3201+ -------------------------------------------------
3202+
3203+ NOTE: Do not use local URLs here if you plan to publish your superproject!
3204+
3205+ See what files `git submodule` created:
3206+
3207+ -------------------------------------------------
3208+ $ ls -a
3209+ . .. .git .gitmodules a b c d
3210+ -------------------------------------------------
3211+
3212+ The `git submodule add` command does a couple of things:
3213+
3214+ - It clones the submodule under the current directory and by default checks out
3215+ the master branch.
3216+ - It adds the submodule's clone path to the `.gitmodules` file and adds this
3217+ file to the index, ready to be committed.
3218+ - It adds the submodule's current commit ID to the index, ready to be
3219+ committed.
3220+
3221+ Commit the superproject:
3222+
3223+ -------------------------------------------------
3224+ $ git commit -m "Add submodules a, b, c and d."
3225+ -------------------------------------------------
3226+
3227+ Now clone the superproject:
3228+
3229+ -------------------------------------------------
3230+ $ cd ..
3231+ $ git clone super cloned
3232+ $ cd cloned
3233+ -------------------------------------------------
3234+
3235+ The submodule directories are there, but they're empty:
3236+
3237+ -------------------------------------------------
3238+ $ ls -a a
3239+ . ..
3240+ $ git submodule status
3241+ -d266b9873ad50488163457f025db7cdd9683d88b a
3242+ -e81d457da15309b4fef4249aba9b50187999670d b
3243+ -c1536a972b9affea0f16e0680ba87332dc059146 c
3244+ -d96249ff5d57de5de093e6baff9e0aafa5276a74 d
3245+ -------------------------------------------------
3246+
3247+ NOTE: The commit object names shown above would be different for you, but they
3248+ should match the HEAD commit object names of your repositories. You can check
3249+ it by running `git ls-remote ../a`.
3250+
3251+ Pulling down the submodules is a two-step process. First run `git submodule
3252+ init` to add the submodule repository URLs to `.git/config`:
3253+
3254+ -------------------------------------------------
3255+ $ git submodule init
3256+ -------------------------------------------------
3257+
3258+ Now use `git submodule update` to clone the repositories and check out the
3259+ commits specified in the superproject:
3260+
3261+ -------------------------------------------------
3262+ $ git submodule update
3263+ $ cd a
3264+ $ ls -a
3265+ . .. .git a.txt
3266+ -------------------------------------------------
3267+
3268+ One major difference between `git submodule update` and `git submodule add` is
3269+ that `git submodule update` checks out a specific commit, rather than the tip
3270+ of a branch. It's like checking out a tag: the head is detached, so you're not
3271+ working on a branch.
3272+
3273+ -------------------------------------------------
3274+ $ git branch
3275+ * (no branch)
3276+ master
3277+ -------------------------------------------------
3278+
3279+ If you want to make a change within a submodule and you have a detached head,
3280+ then you should create or checkout a branch, make your changes, publish the
3281+ change within the submodule, and then update the superproject to reference the
3282+ new commit:
3283+
3284+ -------------------------------------------------
3285+ $ git checkout master
3286+ -------------------------------------------------
3287+
3288+ or
3289+
3290+ -------------------------------------------------
3291+ $ git checkout -b fix-up
3292+ -------------------------------------------------
3293+
3294+ then
3295+
3296+ -------------------------------------------------
3297+ $ echo "adding a line again" >> a.txt
3298+ $ git commit -a -m "Updated the submodule from within the superproject."
3299+ $ git push
3300+ $ cd ..
3301+ $ git diff
3302+ diff --git a/a b/a
3303+ index d266b98..261dfac 160000
3304+ --- a/a
3305+ +++ b/a
3306+ @@ -1 +1 @@
3307+ -Subproject commit d266b9873ad50488163457f025db7cdd9683d88b
3308+ +Subproject commit 261dfac35cb99d380eb966e102c1197139f7fa24
3309+ $ git add a
3310+ $ git commit -m "Updated submodule a."
3311+ $ git push
3312+ -------------------------------------------------
3313+
3314+ You have to run `git submodule update` after `git pull` if you want to update
3315+ submodules, too.
3316+
3317+ Pitfalls with submodules
3318+ ------------------------
3319+
3320+ Always publish the submodule change before publishing the change to the
3321+ superproject that references it. If you forget to publish the submodule change,
3322+ others won't be able to clone the repository:
3323+
3324+ -------------------------------------------------
3325+ $ cd ~/git/super/a
3326+ $ echo i added another line to this file >> a.txt
3327+ $ git commit -a -m "doing it wrong this time"
3328+ $ cd ..
3329+ $ git add a
3330+ $ git commit -m "Updated submodule a again."
3331+ $ git push
3332+ $ cd ~/git/cloned
3333+ $ git pull
3334+ $ git submodule update
3335+ error: pathspec '261dfac35cb99d380eb966e102c1197139f7fa24' did not match any file(s) known to git.
3336+ Did you forget to 'git add'?
3337+ Unable to checkout '261dfac35cb99d380eb966e102c1197139f7fa24' in submodule path 'a'
3338+ -------------------------------------------------
3339+
3340+ You also should not rewind branches in a submodule beyond commits that were
3341+ ever recorded in any superproject.
3342+
3343+ It's not safe to run `git submodule update` if you've made and committed
3344+ changes within a submodule without checking out a branch first. They will be
3345+ silently overwritten:
3346+
3347+ -------------------------------------------------
3348+ $ cat a.txt
3349+ module a
3350+ $ echo line added from private2 >> a.txt
3351+ $ git commit -a -m "line added inside private2"
3352+ $ cd ..
3353+ $ git submodule update
3354+ Submodule path 'a': checked out 'd266b9873ad50488163457f025db7cdd9683d88b'
3355+ $ cd a
3356+ $ cat a.txt
3357+ module a
3358+ -------------------------------------------------
3359+
3360+ NOTE: The changes are still visible in the submodule's reflog.
3361+
3362+ This is not the case if you did not commit your changes.
3363+
31583364[[low-level-operations]]
31593365Low-level git operations
31603366========================
0 commit comments