Last active
October 8, 2016 15:56
-
-
Save davidmyersdev/f5a2555c48a3c5720a119403a44d5918 to your computer and use it in GitHub Desktop.
Copy a git repo's files into your own project in one command.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # @param: <repository> parameter for `git clone <repository>`. For more information, please see https://git-scm.com/docs/git-clone | |
| repository=${1} | |
| # @param: <directory> parameter for `git clone <repository> <directory>`. For more information, please see https://git-scm.com/docs/git-clone | |
| directory=${2} | |
| # @param: (optional) <branch> parameter for `git checkout <branch>`. For more information, please see https://git-scm.com/docs/git-checkout | |
| branch=${3} | |
| read -r -d '' usage <<- MESSAGE | |
| git-carbon-copy v0.0.1 | |
| David R. Myers II <davidrmyersii@gmail.com> | |
| Usage: git carbon-copy <repository> <directory> [<branch>] | |
| Examples: | |
| - (without <branch> param): git carbon-copy https://github.com/drm2/vebs new-copy-dir | |
| - (with <branch> param): git carbon-copy https://github.com/drm2/vebs new-copy-dir develop | |
| MESSAGE | |
| # make sure $repository param is passed in | |
| test -z ${repository} && echo -e "${usage}" 1>&2 && exit 1 | |
| # make sure $directory param is passed in | |
| test -z ${directory} && echo -e "${usage}" 1>&2 && exit 1 | |
| # clone the git repo | |
| git clone ${repository} ${directory} | |
| # store the working directory | |
| initial_pwd=`pwd` | |
| # move into the directory | |
| cd ${directory} | |
| # make sure we actually moved into the new directory | |
| if [ "${initial_pwd}" != "`pwd`" ]; then | |
| # optionally checkout the specified branch/tag/commit | |
| if [ ! -z "${branch}" ]; then | |
| git checkout ${branch} | |
| fi | |
| # remove the git tracking | |
| rm -rf ./.git | |
| fi |
Author
Author
TODO:
- use newer git conventions to only pull the single necessary commit instead of the entire history (will save on bandwidth)
- allow user to pass
.as the directory (requires change of some logic that ensures the user actually changed directories)
IDEAS:
- maybe download the directory into
/tmpbefore cleaning and then copying back - possibly use rsync for copy to allow ignoring files that already exist in the target directory
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why?
Have you ever needed to create a simple copy of a repository (usually a template) just to add it to your project? The process usually involves cloning the repo, (possibly) checking out a specific branch/tag/commit, and removing the
.gitdirectory. This git addition slims these steps down into one command.Installation:
As with any custom git command, all you need to do is download this file, place it in your
$PATH, and then give it execute permissions. E.g.Usage:
git carbon-copy <repository> <directory> [<branch>]The
<repository>and<directory>params are required.Examples:
Example 1: Copying a web client template into your existing project
Let's say you are in your current project at
/path/to/project, and you want to add a copy of a custom Vuex template to use as your web client. It would be as simple as runninggit carbon-copy https://github.com/<username>/<template-repo> web. This would create a copy of the default branch of your repository at/path/to/project/web. If you wanted to instead create a copy of a specific branch/tag/commit (let's go with the v1.2.3 tag), that would be as simple as runninggit carbon-copy https://github.com/<username>/<template-repo> web v1.2.3.Example 2: Copying a framework boilerplate for a new project
Let's say you are in your current workspace at
/path/to/workspace, and you want to create a new project calledawesomebuilt on the Laravel framework. All you have to do is rungit carbon-copy https://github.com/laravel/laravel awesomeand thengit inityour new project at/path/to/workspace/awesome! If you'd prefer to use an older version of the framework (such as v5.1.33), you can always pass that in withgit carbon-copy https://github.com/laravel/laravel awesome v5.1.33.Enjoy!