Commit ccbaa03fa7c46a1a6bef072c088fd2fc4ad70f40

Authored by Lars Tiede
1 parent 35640b10

Wrote instructions for retracing demo

Showing 1 changed file with 100 additions and 1 deletions   Show diff stats
README.md
1   -I'm the parent repository that uses "git subtree"!
  1 +### This is the parent that uses "git subtree".
  2 +
  3 +Here's how you can re-trace the demo given at utviklerlunsj.
  4 +
  5 +First, make yourself a directory and clone the repository into it:
  6 +
  7 +```bash
  8 +git clone git@source.uit.no:lars.tiede/lunsjdemo_parent_with_subtree.git .
  9 +```
  10 +
  11 +Now reset your working copy of this repository to the commit right before we added the subtree:
  12 +
  13 +```bash
  14 +git reset --hard 8ae28dc5401ed9f4227c3a5514dc3a82221fdb90
  15 +```
  16 +
  17 +Okay, now there shouldn't be any my\_lib. Let's make ourselves a branch in order to not mess up the master branch:
  18 +
  19 +```bash
  20 +git checkout -b YOU_CHOOSE
  21 +```
  22 +
  23 +Now add my\_lib as a subtree:
  24 +
  25 +```bash
  26 +git log # just our boring history so far
  27 +git subtree add --prefix my_lib git@source.uit.no:lars.tiede/lunsjdemo_my_lib.git master
  28 +git status # everything has already been committed, no changes reported
  29 +git log # wow! Lots of stuff here all of a sudden
  30 +```
  31 +
  32 +We can inspect what git actually did:
  33 +
  34 +```bash
  35 +git show # all changes in last commit, note that git has the contents of my_lib (unlike when you use submodules)
  36 +```
  37 +
  38 +You'll find that in the directory 'my\_lib', you are still in the same repository (unlike when you use submodules):
  39 +
  40 +```bash
  41 +git remote -v # shows that origin is "parent_with_subtree"
  42 +cd my_lib
  43 +git remote -v # shows that origin is "parent_with_subtree"
  44 +cd ..
  45 +```
  46 +
  47 +Now do some random change in my\_lib and commit and push it. You will see that the files have only changed in your repository, not my\_lib's.
  48 +
  49 +```bash
  50 +# change something in my\_lib. Then:
  51 +git add my_lib
  52 +git commit -m "did some changes in my_lib"
  53 +git push --set-upstream origin YOUR_BRANCH_NAME
  54 +```
  55 +
  56 +For good measure, do some change in parent's files and commit and push that as well.
  57 +
  58 +Let's assume that there are changes in the master branch, and you want to rebase your branch on master. This is what many people do very often.
  59 +
  60 +```bash
  61 +git rebase master
  62 +```
  63 +
  64 +Ouch. This is what happens:
  65 +```bash
  66 +First, rewinding head to replay your work on top of it...
  67 +Applying: [my_lib] first commit
  68 +Using index info to reconstruct a base tree...
  69 +Falling back to patching base and 3-way merge...
  70 +Auto-merging README.md
  71 +CONFLICT (add/add): Merge conflict in README.md
  72 +Failed to merge in the changes.
  73 +Patch failed at 0001 [my_lib] first commit
  74 +```
  75 +
  76 +The rebase failed instantly. What's worse is that there is no way to fix it. When you add a subtree, you can no longer rebase the branch you were working on. So let's bail out of this:
  77 +
  78 +```bash
  79 +git rebase --abort
  80 +```
  81 +
  82 +All changes you made to my\_lib so far only live in your repository. Also, all changes to my\_lib that might be available in my\_libs origin are not automatically pulled at any time to your repository. Here is how you pull from, and push to my\_lib's origin. Pull like this:
  83 +
  84 +```bash
  85 +git subtree pull --prefix my_lib git@source.uit.no:lars.tiede/lunsjdemo_my_lib.git master
  86 +```
  87 +
  88 +And push like this:
  89 +
  90 +```bash
  91 +git subtree push --prefix my_lib git@source.uit.no:lars.tiede/lunsjdemo_my_lib.git master
  92 +```
  93 +
  94 +You'll notice the longer-than-usual output. This is because git actually has to go through your repo's commit history and splice out all the commits that changed something in my\_lib, extract the changes to my\_lib, then make commits to my\_lib based on those changes.
  95 +
  96 +That's basically it, now you know how submodules work, and what their major caveat is. Delete your branch now so that there is no pollution on source.uit.no:
  97 +
  98 +```bash
  99 +git push origin --delete YOUR_BRANCH_NAME
  100 +```
... ...