Marty Wallace
Marty Wallace

Reputation: 35734

Start a git repository using files from an external repository

I would like to start a new git repository but using the files of an external 3rd party respoitory as my starting point.

I have an empty repository sitting on branch master with no files in it. I now want to add files as my first commit, but the should be copied from another git repository.

Is there a git way to do this, or should the repository be checkout out and the files copied in to my repository.

Upvotes: 2

Views: 143

Answers (2)

Tuxdude
Tuxdude

Reputation: 49473

If you would like to preserve the history from the remote repository, you can do this:

# Initialize your local repository
git init
# Add the source-repo where you want to pull the files from
git remote add source-repo <URL of Source repo>
# Make git fetch the history for the source-repo
git fetch source-repo

# Merge from BRANCH_NAME of source-repo into
# the current branch (which should be called master by default)
git merge source-repo/BRANCH_NAME

If you just want a single commit to add (i.e. merge) the files, instead of multiple commits, you can make use of squash-merge. As of now (March 2013) git does not support squash merges on an empty repo. So you would need to have at least one commit prior to doing such a squash merge.

So you could do something like this:

# Initialize your local repository
git init
# Add the source-repo where you want to pull the files from
git remote add source-repo <URL of Source repo>
# Make git fetch the history for the source-repo
git fetch source-repo

# Create a dummyfile and commit the change
touch dummyfile
git add dummyfile
git commit -m "Dummy file change."

# Create a squash merge from BRANCH_NAME of source-repo into
# the current branch (which should be called master by default)
git merge --squash source-repo/BRANCH_NAME

Upvotes: 1

gcbenison
gcbenison

Reputation: 11963

No need to check out and copy. You just need to let git know about the remote repository, and then you can merge from it, like this:

git remote add the-remote-repository <url of remote repo>
git remote update
git merge the-remote-repository/master   # or whatever branch you want to pull in

Upvotes: 0

Related Questions