How to work with multiple projects at the same time using Git?

I am currently building a site for a client with Wordpress, but in Wordpress I am building a plugin and theme that I will package and redistribute myself.

I am also using Git to move the site to the server.

In the past I have used ln -s

to symbolize other projects in the main. One drawback is that I have to Git push them all individually.

What other methods are there for this?

+3


source to share


3 answers


Create one repository for each theme and plugin that you want to distribute separately.

Then, in the site repository, add them as submodules .

Using submodules

git submodule add [clone url of your plugin]
git submodule add [clone url of your theme]

      

This will create one folder for each submodule with the contents of the repository inside, as if it were cloned.

When you want to "pull" your submodules, run:

git submodule update

      

Working on submodules

See the documentation section of the submodule of the same name .



You can make changes, commit and click on a submodule directly from the project that uses them. Just go cd

to the submodules directory and use it like any other repository.

By default, submodules are in a state detached head

, go to the submodule directory and check out the branch for the correct tracking branch (later, when you want to update a submodule, use git submodule update --remote --merge

from the main repository and don't detach HEAD

again). Then do your work like in any repository git

:

cd my_plugin
vim readme
git commit -m "Modified from main"
git push # Push the changes to the submodule remote.

      

Note that the "master" repository only stores the version of the submodules it needs. So, from the point of view of the main repository, your submodules are only now "in a different version":

$ git status
On branch master
Your branch is up-to-date with 'origin/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:   my_plugin (new commits)
    modified:   my_theme (new commits)

      

If you want your project to use newer versions of submodules, commit this (this will only tell the main repository which version of the submodule it should use).

git commit sub1 sub2 -m "Use newer version of plugin and theme"
git push

      

Then, where would you clone the main repository:

git pull # Pull the main repository changes, along with the information telling it what version of it submodules it should use.
git submodule update # Effectively update the submodules by fetching said versions.

      

+2


source


What you are asking is called Git Submodules .

From the linked page:



Let's say you are designing a website and creating Atom feeds. Instead of writing your own code to generate Atom, you decided to use a library. You will probably have to include this code from a shared library such as installing a CPAN or Ruby gem, or copy the source code into your own project tree. The problem with including a library is that it is difficult to customize the library in any way, and often more difficult to deploy because you need to make sure every client has the library. The problem with distributing code in your own project is that any custom changes you make are difficult to merge when the upstream changes become available.

Git solves this problem using submodules. Submodules allow you to store a Git repository as a subdirectory of another Git repository. This allows you to clone another repository into your project and keep your commits separate.

Optional: http://git-scm.com/book/en/v2/Git-Tools-Submodules . You can also search the web for Git submodules to find many tutorials.

+1


source


0


source







All Articles