简体   繁体   中英

How to work on multiple projects simultaneously using Git?

Currently I'm making a site for a client with Wordpress, but within Wordpress building a plugin and theme which I will package and redistribute on their own afterwards.

I'll also use Git to push the site to the server.

In the past, I've used ln -s to symlink the other projects into the main one. One downside is I have to git push them all individually.

What other methods are there for this?

Make one repository per theme and plugin you want to distribute separately.

Then, in the site's 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 repository content inside as if it was cloned.

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

git submodule update

Working on submodules

See the submodule documentation section with the same name .

You can make changes, commit and push on a submodule directly from the project that uses them. Simply cd into the submodule's directory and use it like any other repository.

By default, submodules are in a detached head state, go in the submodule's directory and checkout a branch to have a correct tracking branch (later, when you want to update the submodule, use git submodule update --remote --merge from the main repository to avoid detaching HEAD again). Then do your work as in any git repository:

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

Note that the "main" repository only store what version of the submodules it needs. So, from the main repository's point of view, your submodules are just currently "at 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 the new versions of the submodules, commit this (this will only tell the main repository what version of the submodule it should use).

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

Then, wherever you cloned the main repository:

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

What you're asking for is called Git Submodules .

From the linked page:

Suppose you're developing a web site and creating Atom feeds. Instead of writing your own Atom-generating code, you decide to use a library. You're likely to have to either include this code from a shared library like a CPAN install or Ruby gem, or copy the source code into your own project tree. The issue with including the library is that it's difficult to customize the library in any way and often more difficult to deploy it, because you need to make sure every client has that library available. The issue with vendoring the code into your own project is that any custom changes you make are difficult to merge when upstream changes become available.

Git addresses this issue using submodules. Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM