gitgithub

GIT Updating subtree: how can I update my subtree?


We have a few repos like:

├── Site1
│   └──lib
└── Site2
    └──lib

As you see lib is shared in both sites. Now when I use

# on master
$ git fetch lib

Nothing happens. After that I use:

$ git merge --squash -s subtree --no-commit lib

Then it starts deleting loads of files that belongs to the site.

If I try this: Update the Library Sub-Project from the Shared Library Remote http://www.codeproject.com/Articles/562950/GitplusSubtreeplusMergeplus-e2-80-93TheplusQuickpl

# on master
$git checkout lib
Checking out files: 100% (4521/4521), done.
Switched to branch 'lib'

$ git pull
Already up-to-date.

Then just checkout to the master, and run the command:

git merge --squash -s subtree --no-commit lib

This just deletes a whole load of files from my site and leaves a few files like its in a subfolder of lib. Why doesn't this just update the lib to the correct version? and leave the files for the site?

After a reset I do get my project back

git reset --hard origin/master

How can I update the library?

There is a remote branch setup.

$ git remote -v
lib     https://github.com/***/lib.git (fetch)
lib     https://github.com/***/lib.git (push)

Solution

  • Considering that you already set your subtree in your app (git remote add subtree_name git@github.com:user/subtree_proj.git) your have to:

    1- Subtree split

    git subtree split -P lib -b new_branch_to_split where the lib is the path to your subtree project and the new_branch_to_split will only contain your lib code and will be pushed to your lib repo.

    2- Subtree push

    git push subtree_name new_branch_to_split:original_subtree_branch(i.e. master)

    or

    git push subtree_name new_branch_to_split:master

    3- Always delete the temporary branch

    git branch -D new_branch_to_split

    4- Pull the changes in your other project that's using the subtree project

    git subtree pull -P lib subtree_name original_subtree_branch(i.e. master)