Importing a GIT repository with script
Asked by G_ W_:

Hello,

I came across [this article](https://support.atlassian.com/bitbucket-cloud/docs/import-a-repository/) showing how to importing GIT repositories from other hosting services. I was wondering, if there's a way to automate this with scripting?

Thank you and best,

G.W.


Answers:
author: Michael Walker
body: The steps necessary to "import" a repo to any platform, Bitbucket included, is to:

1. Clone the original repo
2. Create a new empty placeholder in the desired destination and copy the git URL
3. Explore into the local clone's directory
4. Set a new "remote" targeting the desired destination (the git URL from step 2) within the local repo's config
5. Push to the new remote

These steps would need to know what credentials to use for the initial clone as well as the creds for pushing into the new remote (since they'll likely differ) as well as knowing the URLs for the source and destination. All of this should be possible in bash or really any scripting language that can interact with CLI for the purpose of calling git operations.

Example:

```
source_username = ""
source_password = ""
source_repo_url = ""
destination_username = ""
destination_password = ""
destination_repo_name = ""
destination_project_key = ""
destination_workspace_slug = ""

# clone the repo
git clone --mirror https://$source_username:$source_password@source_repo_url temp
# create empty placeholder in destination workspace
curl -u "$destination_username:$destination_password" -X POST -H "Content-Type: application/json" -d '{ "scm": "git", "project": { "key": $destination_project_key } "https://api.bitbucket.org/2.0/repositories/$destination_workspace_slug/$destination_repo_name"
# explore into repo dir
cd temp
# add new remote
git remote add cloud https://$destination_username:$destination_password@bitbucket.org/$destination_workspace_slug/$destination_repo_name.git
# push to new remote
git push -u cloud --all
git push -u cloud --tags
```

Then you'd just need to loop it if there's multiple repos that you want to migrate, passing in the source_url and the repo name/project key for each itteration.

You can see an example of this in python for migrating from GitHub to Bitbucket Cloud [here](https://bitbucket.miwalker.net/projects/BBCS/repos/migrate_github_repos_to_bitbucket/browse)

comments: author: G_ W_
body: Hi Michael,

This is really helpful. Thank you! I have a question about the username format in the curl command: should it just be my email address verbatim?

Best,

G.W.


author: Michael Walker
body: For the Bitbucket (destination) side of things, you'll need your username (not email address)

1. Log in to Bitbucket cloud
2. Select settings (cog) in the top right corner
3. Choose "Personal Settings"
4. Copy your "username"

* [How to update your username](https://support.atlassian.com/bitbucket-cloud/docs/update-your-username/)

For the password, I'd recommend just using an [app password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) as they can be used for git operations (clone/push) as well as interacting with the REST API (the curl command). You could also use an [access token](https://support.atlassian.com/bitbucket-cloud/docs/access-tokens/) but that's probably an unnecessary added complication since app passwords work great for this sort of thing.


author: G_ W_
body: Thank you Michael!


author: Jim Knepley - ReleaseTEAM
body: I think we could replicate the steps one way or another.

Operations like [creating the repository](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-post) are supported by the Bitbucket API, and moving the content would be a [series of git operations](https://www.atlassian.com/git/tutorials/git-move-repository).

Since you have to check out the repository first, it's not as direct as importing with Bitbucket cloud, but workable.

comments: author: G_ W_
body: Thank you very much for the info!


Tags: bitbucket-cloud

cloud