Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Working with multiple git repositories

14,509 views

Published on

Talk I have given at T-Dose about git submodules and git slave and different worklow around them

Published in: Technology

Working with multiple git repositories

  1. 1. . WWoorrkkiinngg wwiitthhmmuullttiippllee ggiitt rreeppoossiittoorriieess Julien Pivotto T-Dose, Eindhoven October 26, 2014
  2. 2. . wwhhooaammii JJuulliieenn PPiivvoottttoo • System administrator at inuits.eu • Git user for 5+ years • DevOps believer • Open-source defender since 2004 • roidelapluie on twitter/github
  3. 3. . OOppss <<33 DDeevv • Infrastructure as Code ▶ SCM all the things ▶ Monitoring ▶ Configuration ▶ Application deployment • Taking part of software development ▶ Understanding ▶ Monitoring ▶ High Availability ▶ …
  4. 4. . MMee aanndd ggiitt • I first used subversion (10 years ago) • Private forges and sourceforge • 5 years later a lot of projects moved to git • The Puppet community also uses a lot git • Git/Hg are the de-facto standards in OS projects
  5. 5. . Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Source: http://git-scm.com/
  6. 6. . ggiitt lloogg • Initiated in 2005 by Linus Torvalds • Replacement of closed-source BitKeeper • Created for the Linux Kernel development • Now used by thousands of projects
  7. 7. . ggiitt iiss ddiissttrriibbuutteedd • Everything can be done in local (exept pull&push) • Work with several remotes • Share code with anyone • No unique central repository • A lot of workflows possible
  8. 8. . BBrraanncchhiinngg aannddmmeerrggiinngg iiss eeaassyy • You can branch locally • Merging is easy and simple • You can squash commits
  9. 9. . HHooww ttoo aacccceessss aa rreeppoo • Locally (Local filesystems) • SSH • HTTP/HTTPs • Git protocol
  10. 10. . ggiitt iiss ooppeenn--ssoouurrccee • Published under GPL-2 • Written in C • A lot of frontends/backends • A lot of libraries • A lot of hosting services • And a lot of users
  11. 11. . MMuullttiippllee ggiitt rreeppooss:: wwhhyy?? • Code isolation: plugins, libraries • Getting faster feedback from testing • Contributing back to upstream projects • Separate apps that are released together • Share code between projects
  12. 12. . MMuullttiippllee ggiitt rreeppooss uusseeccaasseess • Infrastructure as Code • Projects released together • Projects that embed external libraries • …
  13. 13. . git submodules
  14. 14. . ggiitt ssuubbmmoodduulleess • Built-in with git • Embeds external repositories in a subdirectory or your repo • Points these external repositories in a particular commit
  15. 15. . ggiitt ssuubbmmoodduulleess • Submodules act like traditional git repositories • Super repo contains a file with the submodules urls • Super repo contains objects that can be updated with commit hashes
  16. 16. . IInniittiiaattee ssuuppeerr--rreeppoo . $ git init . Initialized empty Git repository in /home/roidelapluie /super-repo/.git/ $ > README $ git add README $ git commit -m "Initial commit" [master (root-commit) b526be8] Initial commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README .
  17. 17. . IInniittiiaattee ssuubbrreeppoo . $ git init . Initialized empty Git repository in /home/roidelapluie /subrepo/.git/ $ > README $ git add README $ git commit -m "Initial commit" [master (root-commit) 41e64b6] Initial commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README .
  18. 18. . AAdddd ffiirrssttmmoodduullee iinn ppaarreenntt rreeppoo . $ git submodule add ../subrepo/ subrepo Cloning into 'subrepo'... done. $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) . new file: .gitmodules new file: subrepo
  19. 19. . WWhhaatt jjuusstt hhaappppeenneedd?? • git created a .gitmodules file • It cloned the subrepo in a directory • It created a git object that points to the repo • That object contains the git revision
  20. 20. . ..ggiittmmoodduulleess ffiillee . $ cat .gitmodules [submodule "subrepo"] . path = subrepo url = ../subrepo/ • Unique file for all the submodules • Only contains the path and the url • In this case, we use relative URL
  21. 21. . CCoommmmiittiinngg oouurr cchhaannggee . $ git commit -m "Add subrepo" [master 3251a63] Add subrepo 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 subrepo .
  22. 22. . CClloonniinngg tthhaatt ssuuppeerr rreeppoo . $ git clone /home/roidelapluie/super-repo/ . Cloning into '.'... done. $ ls subrepo/ $ git submodule init Submodule 'subrepo' (/home/roidelapluie/subrepo) registered for path 'subrepo' $ git submodule update Cloning into 'subrepo'... done. Submodule path 'subrepo': checked out '41e64b6bc0c97052 1c296b9c5e6a0b9144778962' .
  23. 23. . SShhoorrccuutt ##11 . $ git submodule init $ git submodule update . . $ git submodule update --init Submodule 'subrepo' (/home/roidelapluie/subrepo) registered for path 'subrepo' Cloning into 'subrepo'... done. Submodule path 'subrepo': checked out '41e64b6bc0c97052 1c296b9c5e6a0b9144778962' .
  24. 24. . SShhoorrccuutt ##22 . $ git clone /home/roidelapluie/super-repo/ . $ git submodule init $ git submodule update . . $ git clone --recurse-submodules /home/roidelapluie/ super-repo/ . Cloning into '.'... done. Submodule 'subrepo' (/home/roidelapluie/subrepo) registered for path 'subrepo' Cloning into 'subrepo'... done. Submodule path 'subrepo': checked out '41e64b6bc0c97052 1c296b9c5e6a0b9144778962' .
  25. 25. . WWoorrkkiinngg iinn aa ssuubbmmoodduullee . $ cd subrepo $ > newfile $ git add newfile $ git commit -m "add newfile" [detached HEAD 6ee0e71] add newfile 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 newfile $ git push fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use . git push origin HEAD:<name-of-remote-branch>
  26. 26. . CChheecckkiinngg oouutt aa bbrraanncchh . $ git checkout master Warning: you are leaving 1 commit behind, not connected to any of your branches: 6ee0e71 add newfile If you want to keep them by creating a new branch, this may be a good time to do so with: git branch new_branch_name 6ee0e71 Switched to branch 'master' Your branch is up-to-date with 'origin/master'. .
  27. 27. . MMeerrggiinngg oouurr ccoommmmiitt . $ git merge 6ee0e71 Updating 41e64b6..6ee0e71 Fast-forward newfile | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 newfile .
  28. 28. . PPuusshhiinngg oouurr ccoommmmiitt . $ git push Counting objects: 2, done. Delta compression using up to 8 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 247 bytes | 0 bytes/s, done. Total 2 (delta 0), reused 0 (delta 0) To /home/roidelapluie/subrepo . 41e64b6..6ee0e71 master -> master
  29. 29. . PPuusshhiinngg oouurr ccoommmmiitt . $ date > newfile $ git commit -am "update newfile" [master 8a56806] update newfile 1 file changed, 1 insertion(+) $ git push Counting objects: 3, done. Delta compression using up to 8 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 310 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To /home/roidelapluie/subrepo . 6ee0e71..8a56806 master -> master
  30. 30. . LLeett''ss rreewwiinndd.... • A submodule not attached to a branch ▶ Because submodules are referenced by their commit hash only ▶ It is called the DETACHED HEAD • How to push then? ▶ The easy/safe option is to checkout a branch • You only need to do that once
  31. 31. . ggiitt ssuubbmmoodduullee ffoorreeaacchh • git submodule foreach allow you to run a command on each repo ▶ git submodule foreach git checkout master ▶ git submodule foreach ls ▶ git submodule foreach git commit -am "foo"
  32. 32. . MMeeaannwwhhiillee,, oonn tthhee ssuuppeerr--pprroojjeecctt…… . $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) . modified: subrepo (new commits) no changes added to commit (use "git add" and/or "git commit -a")
  33. 33. . UUppddaattee aa ssuubbrreeppoo . $ git commit -am "update subrepo" [master 731626b] update subrepo 1 file changed, 1 insertion(+), 1 deletion(-) .
  34. 34. . LLooggss . $ git log -p --submodule=log subrepo/ commit 731626bc53d8c248647443b4c0523d96d316cd24 Author: Julien Pivotto <roidelapluie@inuits.eu> Date: Sat Oct 25 22:26:56 2014 +0200 . update subrepo Submodule subrepo 41e64b6..01c1b9c: > work on newfile > update newfile > add newfile
  35. 35. . CChhaannggee aa ssuubbmmoodduulleess UURRLL • Edit .gitmodules and commit the file • Run git submodule sync • Now the submodule will use the new URL
  36. 36. . ggiitt ssuubbmmoodduullee ----rreeccuurrssiivvee • Run commands recursively • You can get multiple levels of submodules • But you add complexity ▶ Each level is 1 more commit you need to write ▶ Playing with remote urls becomes tricky of you work with remotes
  37. 37. . SSuubbmmoodduulleess iinntteeggrraattiioonn • Integrated in git, standard • Integrated in CI tools (Jenkins) • Nothing specific needed for pulling • Need to checkout the submodules after pulling
  38. 38. . SSuubbmmoodduulleess iinn GGeerrrriitt • Gerrit integrates submodules • It can create commits in "super-repo" if a code change is merged • Not a perfect way of doing if changes in two repo's are depending of each other
  39. 39. . SSuubbmmoodduulleess iinn GGeerrrriitt • Gerrit is integrated in CI tools • Gerrit allows fine tuning ACL • Gerrit code reviews with submodules changes don't display changes in the submodules
  40. 40. . SSuubbmmoodduulleess pprrooss aanndd ccoonnss

×