Skip to content

Instantly share code, notes, and snippets.

@davidmyersdev
Last active October 8, 2016 15:56
Show Gist options
  • Select an option

  • Save davidmyersdev/f5a2555c48a3c5720a119403a44d5918 to your computer and use it in GitHub Desktop.

Select an option

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.
#!/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
@davidmyersdev

davidmyersdev commented Sep 26, 2016

Copy link
Copy Markdown
Author

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 .git directory. 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.

# download the file
wget https://gist.githubusercontent.com/drm2/f5a2555c48a3c5720a119403a44d5918/raw/89232f4cf93600fa30d10f4198c5cfe4fe0aa4ee/git-carbon-copy

# move the file to somewhere in your $PATH (e.g. /usr/bin)
mv git-carbon-copy /usr/bin/

# give it execute permissions
chmod +x /usr/bin/git-carbon-copy

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 running git 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 running git 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 called awesome built on the Laravel framework. All you have to do is run git carbon-copy https://github.com/laravel/laravel awesome and then git init your 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 with git carbon-copy https://github.com/laravel/laravel awesome v5.1.33.

Enjoy!

@davidmyersdev

davidmyersdev commented Sep 26, 2016

Copy link
Copy Markdown
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 /tmp before 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