You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/01-introduction/sections/help.asc
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ Diese Befehle sind nützlich, weil Sie sich die Hilfe jederzeit anzeigen lassen
21
21
Wenn die Hilfeseiten und dieses Buch nicht ausreichen und Sie persönliche Hilfe brauchen, können Sie den `#git` oder `#github` Kanal auf dem Freenode IRC Server probieren, der unter https://freenode.net zu finden ist.
22
22
Diese Kanäle sind in der Regel sehr gut besucht. Normalerweise findet sich unter den vielen Anwendern, die oft sehr viel Erfahrung mit Git haben, irgendjemand, der Ihnen weiterhelfen kann.(((IRC)))
23
23
24
-
Wenn Sie nicht die vollständige Manpage-Hilfe benötigen, sondern nur eine kurze Beschreibung der verfügbaren Optionen für einen Git-Befehl, können Sie auch in den kompakteren „Hilfeseiten“ mit den Optionen `-h` oder `--help` nachschauen, wie in:
24
+
Wenn Sie nicht die vollständige Manpage-Hilfe benötigen, sondern nur eine kurze Beschreibung der verfügbaren Optionen für einen Git-Befehl, können Sie auch in den kompakteren „Hilfeseiten“ mit der `-h` Option nachschauen, wie in:
Copy file name to clipboardExpand all lines: book/07-git-tools/sections/submodules.asc
+118-4Lines changed: 118 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -185,7 +185,7 @@ Submodule path 'DbConnector': checked out 'c3f01dc8862123d317dd46284b05b6892c7b2
185
185
Now your `DbConnector` subdirectory is at the exact state it was in when you committed earlier.
186
186
187
187
There is another way to do this which is a little simpler, however.
188
-
If you pass `--recurse-submodules` to the `git clone` command, it will automatically initialize and update each submodule in the repository.
188
+
If you pass `--recurse-submodules` to the `git clone` command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.
Submodule path 'DbConnector': checked out 'c3f01dc8862123d317dd46284b05b6892c7b29bc'
207
207
----
208
208
209
+
If you already cloned the project and forgot `--recurse-submodules`, you can combine the `git submodule init` and `git submodule update` steps by running `git submodule update --init`.
210
+
To also initialize, fetch and checkout any nested submodules, you can use the foolproof `git submodule update --init --recursive`.
211
+
209
212
==== Working on a Project with Submodules
210
213
211
214
Now we have a copy of a project with submodules in it and will collaborate with our teammates on both the main project and the submodule project.
212
215
213
-
===== Pulling in Upstream Changes
216
+
===== Pulling in Upstream Changes from the Submodule Remote
214
217
215
218
The simplest model of using submodules in a project would be if you were simply consuming a subproject and wanted to get updates from it from time to time but were not actually modifying anything in your checkout.
Git will by default try to update *all* of your submodules when you run `git submodule update --remote` so if you have a lot of them, you may want to pass the name of just the submodule you want to try to update.
374
377
378
+
===== Pulling Upstream Changes from the Project Remote
379
+
Let's now step into the shoes of your collaborator, who has his own local clone of the the MainProject repository.
380
+
Simply executing `git pull` to get your newly committed changes is not enough:
381
+
382
+
[source,console]
383
+
----
384
+
$ git pull
385
+
From https://github.com/chaconinc/MainProject
386
+
fb9093c..0a24cfc master -> origin/master
387
+
Fetching submodule DbConnector
388
+
From https://github.com/chaconinc/DbConnector
389
+
c3f01dc..c87d55d stable -> origin/stable
390
+
Updating fb9093c..0a24cfc
391
+
Fast-forward
392
+
.gitmodules | 2 +-
393
+
DbConnector | 2 +-
394
+
2 files changed, 2 insertions(+), 2 deletions(-)
395
+
396
+
$ git status
397
+
On branch master
398
+
Your branch is up-to-date with 'origin/master'.
399
+
Changes not staged for commit:
400
+
(use "git add <file>..." to update what will be committed)
401
+
(use "git checkout -- <file>..." to discard changes in working directory)
402
+
403
+
modified: DbConnector (new commits)
404
+
405
+
Submodules changed but not updated:
406
+
407
+
* DbConnector c87d55d...c3f01dc (4):
408
+
< catch non-null terminated lines
409
+
< more robust error handling
410
+
< more efficient db routine
411
+
< better connection routine
412
+
413
+
no changes added to commit (use "git add" and/or "git commit -a")
414
+
----
415
+
416
+
By default, the `git pull` command recursively fetches submodules changes, as we can see in the output of the first command above.
417
+
However, it does not *update* the submodules.
418
+
This is shown by the output of the `git status` command, which shows the submodule is ``modified'', and has ``new commits''.
419
+
What's more, the brackets showing the new commits point left (<), indicating that these commits are recorded in MainProject but are not present in the local DbConnector checkout.
420
+
To finalize the update, you need to run `git submodule update`:
421
+
422
+
[source,console]
423
+
----
424
+
$ git submodule update --init --recursive
425
+
Submodule path 'vendor/plugins/demo': checked out '48679c6302815f6c76f1fe30625d795d9e55fc56'
426
+
427
+
$ git status
428
+
On branch master
429
+
Your branch is up-to-date with 'origin/master'.
430
+
nothing to commit, working tree clean
431
+
----
432
+
433
+
Note that to be on the safe side, you should run `git submodule update` with the `--init` flag in case the MainProject commits you just pulled added new submodules, and with the `--recursive` flag if any submodules have nested submodules.
434
+
435
+
If you want to automate this process, you can add the `--recurse-submodules` flag to the `git pull` command (since Git 2.14).
436
+
This will make Git run `git submodule update` right after the pull, putting the submodules in the correct state.
437
+
Moreover, if you want to make Git always pull with `--recurse-submodules`, you can set the configuration option `submodule.recurse` to true (this works for `git pull` since Git 2.15).
438
+
This option will make Git use the `--recurse-submodules` flag for all commands that support it (except `clone`).
439
+
375
440
===== Working on a Submodule
376
441
377
442
It's quite likely that if you're using submodules, you're doing so because you really want to work on the code in the submodule at the same time as you're working on the code in the main project (or across several submodules).
@@ -393,16 +458,18 @@ First of all, let's go into our submodule directory and check out a branch.
393
458
394
459
[source,console]
395
460
----
461
+
$ cd DbConnector/
396
462
$ git checkout stable
397
463
Switched to branch 'stable'
398
464
----
399
465
400
-
Let's try it with the ``merge'' option.
466
+
Let's try updating our submodule with the ``merge'' option.
401
467
To specify it manually, we can just add the `--merge` option to our `update` call.
402
468
Here we'll see that there was a change on the server for this submodule and it gets merged in.
403
469
404
470
[source,console]
405
471
----
472
+
$ cd ..
406
473
$ git submodule update --remote --merge
407
474
remote: Counting objects: 4, done.
408
475
remote: Compressing objects: 100% (2/2), done.
@@ -433,6 +500,7 @@ Now if we update our submodule we can see what happens when we have made a local
433
500
434
501
[source,console]
435
502
----
503
+
$ cd ..
436
504
$ git submodule update --remote --rebase
437
505
First, rewinding head to replay your work on top of it...
438
506
Applying: unicode support
@@ -793,11 +861,16 @@ This way you can simply run `git supdate` when you want to update your submodule
793
861
794
862
Using submodules isn't without hiccups, however.
795
863
796
-
For instance switching branches with submodules in them can also be tricky.
864
+
===== Switching branches
865
+
866
+
For instance, switching branches with submodules in them can also be tricky with Git versions older than Git 2.13.
797
867
If you create a new branch, add a submodule there, and then switch back to a branch without that submodule, you still have the submodule directory as an untracked directory:
798
868
799
869
[source,console]
800
870
----
871
+
$ git --version
872
+
git version 2.12.2
873
+
801
874
$ git checkout -b add-crypto
802
875
Switched to a new branch 'add-crypto'
803
876
@@ -849,6 +922,47 @@ Makefile includes scripts src
849
922
850
923
Again, not really very difficult, but it can be a little confusing.
851
924
925
+
Newer Git versions (Git >= 2.13) simplify all this by adding the `--recurse-submodules` flag to the `git checkout` command, which takes care of placing the submodules in the right state for the branch we are switching to.
Using the the `--recurse-submodules` flag of `git checkout` can also be useful when you work on several branches in the superproject, each having your submodule pointing at different commits.
956
+
Indeed, if you switch between branches that record the submodule at different commits, upon executing `git status` the submodule will appear as ``modified'', and indicate ``new commits''. That is because the submodule state is by default not carried over when switching branches.
957
+
958
+
This can be really confusing, so it's a good idea to always `git checkout --recurse-submodules` when your project has submodules.
959
+
(For older Git versions that do not have the `--recurse-submodules` flag, after the checkout you can use `git submodule update --init --recursive` to put the submodules in the right state.)
960
+
961
+
Luckily, you can tell Git (>=2.14) to always use the `--recurse-submodules` flag by setting the configuration option `submodule.recurse`: `git config submodule.recurse true`.
962
+
As noted above, this will also make Git recurse into submodules for every command that has a `--recurse-submodules` option (except `git clone`).
963
+
964
+
===== Switching from subdirectories to submodules
965
+
852
966
The other main caveat that many people run into involves switching from subdirectories to submodules.
853
967
If you've been tracking files in your project and you want to move them out into a submodule, you must be careful or Git will get angry at you.
854
968
Assume that you have files in a subdirectory of your project, and you want to switch it to a submodule.
0 commit comments