Search code examples
gitgit-branchgit-mergegit-submodules

Git Submodule in branch not master


EDIT: According to @VonC in the comments I have a nested repo, not a submodule. However my problem remains the same below:

I created a branch off of my local repo to work on a new feature. The new feature required me to install the Azure PHP SDK via composer. Turns out that the Azure SDK has created a git repo of it's own within the /vendor folder. So I now have a Submodule in this new branch.

I have committed everything within this branch, and when I do git status I receive

nothing to commit, working directory clean

Now when I switch back to my master branch to merge this new feature branch and do a git status I see:

Untracked files : vendor/

I have googled and seen that if I add and commit this vendor/ directory to the master branch, I will not be able to merge the new feature branch because of some sort of conflict. I think it will also mess up the submodule on the feature branch?!? I haven't tried to merge the new feature branch leaving the vendor/ untracked because I am not sure it will work, and I am not sure what it might screw up.

How should I proceed? I am sitting on the master branch, with the new feature branch sitting there with a submodule in vendor/. And on the master branch the vendor/ folder is in untracked files.

What do I do to merge the feature branch successfully in to master?


Solution

  • I don't think you need to track the "vendor" directory in your git repo.

    So I would just remove it (untrack it), commit the corresponding changes, and add the vendor directory into the project's .gitignore directory so that it remains untracked in the future.

    The steps for this would be simply:

    git checkout feature_branch
    git rm -r --cached vendor
    echo "vendor" >> .gitignore && git add .gitignore
    git commit -m "untrack vendor directory"
    

    The --cached flag above will keep the files locally even after the git rm in an untracked state.

    FWIW, the contents of the vendor directory are dependencies that Composer installs, and they can be generated on the fly. As such, it should be part of your deployment script, so that you can setup your environment using composer and therefore should not be checked into your repo.

    (Note: You might want to explore maintaining a shared soft links for common vendor in your deployment to avoid fresh download time during each build)

    Quoting the composer docs for Should I commit the dependencies in my vendor directory?

    The general recommendation is no. The vendor directory (or wherever your dependencies are installed) should be added to .gitignore/svn:ignore/etc.

    The best practice is to then have all the developers use Composer to install the dependencies. Similarly, the build server, CI, deployment tools etc should be adapted to run Composer as part of their project bootstrapping.