Reputation: 1371
Is there an easy way to just copy a branch from a repository and keep its history?
I thought I got it when I did a git clone , but when adding an origin to the new repository, it says it has already one, i.e. the old repository.
I am really new with git, so be explicit with your answer. :-)
Thanks.
Upvotes: 0
Views: 1732
Reputation: 3758
When you clone a repository, then the URL of the repository you clone it from will be used as the remote (usually called "origin"). That's normal. However, you can have more than one remote repository with Git. (Or you could just change the remote origin
to point to another location, if you only need one.)
So let's assume that the "old" repository is at http://example.com/old.git and the new repository with only one branch from the old remote will be located at http://example.com/new.git. In that case, the process would be as follows:
# clone the repository into directory old
git clone http://example.com/old.git old
cd old
# checkout the branch you want to keep, in this case "my-branch"
git checkout my-branch
# create new remote for new repository
git remote add new http://example.com/new.git
# push that branch to the new repository
git push new my-branch
This assumes that the new repository already exists, but is empty.
Bonus: If you want to know, which remote repositories exist, you can list them with
git remote -v
Output will be something like
origin http://example.com/old.git (fetch)
origin http://example.com/old.git (push)
new http://example.com/new.git (fetch)
new http://example.com/new.git (push)
You can remove remotes with
git remote remove <name of remote>
This will just remove the connection to the remote repository, though. Any local branches you got from these remotes will still remain in your local repository.
Upvotes: 4