With Git, I'd like to include some common JS/CSS library and/or utility methods (i.e. specific files from another repo) in my project, and I'd like it so they're always up-to-date. I don't really want the entire remote repository(s). Bonus if I can work on my "local copies" of the remote files and push the changes back up.
A somewhat parallel example: in ASP.NET, I would do this by referencing the ("remote") other libraries' .dll
files (without "copy local"), so that whenever I recompile the external libraries my project would already point to the newest versions.
My project is...MyProject, and I just want a couple files from external repositories ExternalLibrary and External2. I'd prefer not to clone each repository somewhere else and copy-paste the files into my repo. If I make an improvement to MyProject/scripts/common1.js
, I'd like to push that back to ExternalLibrary.
Related references:
Step 1: Make a copy of repository B if you don't have one already. Step 2: Go to that directory. Step 3: Create a remote connection to repository A as a branch in repository B. Step 4: Pull files and history from this branch (containing only the directory you want to move) into repository B.
Open . Change the current working directory to your local repository. Stage the file for commit to your local repository. $ git add .
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.
Git does not have any support for picking out select files in a remote project. You could pick out the files you want with subtree and then figure out some sort of strategy for getting it back in, but that usually is way to complicated, especially if you suddenly decide you want a couple more files than you originally picked.
What I usually end up doing is using symlinks
. This can be done by adding the projects as submodules in your project, then symlinking the files into the right location.
Your example would go something like this:
$ git clone <url> myproject $ cd myproject $ git submodule add <url> external_library $ git submodule add <url> external2 $ cd scripts $ ln -s ../external_library/common1.js $ ln -s ../external_library/plugin2.js $ cd ../styles $ ln -s ../external2/common.css
To get symlinks to work in windows:
Then before you make any changes I would go and create a new branch:
$ cd external_library $ git checkout -b myproject # make changes back in ../myproject $ git commit <files> # in external_library
Then you can push your branch to github or something and submit a pull request to the author in order to contribute your changes back.
You can stay up to sync like this:
$ cd external_library $ git checkout master $ git pull $ git checkout myproject $ git merge master
You also need to update the parent project when the submodule changes (e.g. git commit external_library
). Anyone using your project would need to do a git clone --recursive <url_to_myproject>
.
NOTE: You can achieve most of this without using a separate branch for your project. Use whatever makes most sense to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With