Importing code from an existing project

This page describes the methods you can use to import code from existing projects into Bitbucket Data Center and Server. When creating a new repository, you can import code from an existing project into Bitbucket using either the terminal or the web interface.

If you're using Bitbucket Cloud, take a look at our Import or convert code from an existing tool page for more information.

On this page

Import code using the web interface

Introduced in Bitbucket Server 4.9, you can import code and its version/branching history into Bitbucket from existing Git projects hosted with Bitbucket Cloud, GitHub, GitHub Enterprise, or a standalone Git repository using the web interface.

To start importing code

  1. While viewing a project within Bitbucket click Import repository in the sidebar.
  2. Select a source to import code from, provide the required information, then click Connect.
    1. For Bitbucket Cloud, include the Username and App password for the account (set at 

      https://bitbucket.org/account/admin/app-passwords)

      to import from, and ensure read access for account, team, project, and repository is enabled.
    2. For GitHub, include the Username and the GitHub personal access token (set at 

      https://github.com/settings/tokens not your GitHub password)

      for the account to import from, and ensure the repo and read:org scopes are enabled.
    3. For GitHub Enterprise, provide the Server URL, Username, and the GitHub personal access token (set at https://github.com/settings/tokens not your GitHub password) for the account to import from, and ensure the repo and read:org scopes are enabled.
    4. For a single Git repository, provide the Clone URL, and Username and Password (if required).
  3. Choose which repositories to import.
    1. All repositories imports all the repositories owned by the account provided.
    2. Select repositories allows you choose specific repositories to import.
  4. Click Import.

Once importing completes, you can refresh the project page to see the imported repositories.


Import code using the terminal

Import code from an existing project using the terminal by first cloning the repository to your local system and then pushing to an empty Bitbucket repository.

Import an existing, unversioned code project

If you have code on your local machine that is not under source control, you can put it under source control and import it into Bitbucket.

Assuming you have Git installed on your local machine, then:

  1. Locally, change to the root directory of your existing source.
  2. Initialize the project by running the following commands in the terminal:

    git init
    git add --all
    git commit -m "Initial Commit"
  3. Log into Bitbucket and create a new repository.
  4. Locate the clone URL in the nav panel on the left (for example:  https://username@your.bitbucket.domain:7999 /yourproject/repo.git).
  5. Push your files to the repository by running the following commands in the terminal (change the URL accordingly):

    git remote add origin https://username@your.bitbucket.domain:7999/yourproject/repo.git 
    git push -u origin master
  6. Done! Your repository is now available in Bitbucket.

Import an existing Git project 

You can import your existing Git repository into an empty repository in Bitbucket. When you do this, Bitbucket maintains your commit history.

  1. Check out the repository from your existing Git host. Use the --bare parameter:

    git clone --bare https://username@bitbucket.org/exampleuser/old-repository.git
  2. Log into Bitbucket and create a new repository (we've called it repo.git in this example).

  3. Locate the clone URL in the nav panel on the left (for example:  https://username@your.bitbucket.domain:7999 /yourproject/repo.git).

  4. Add Bitbucket as another remote in your local repository:

    cd old-repository
    git remote add bitbucket https://username@your.bitbucket.domain:7999/yourproject/repo.git
  5. Push all branches and tags to the new repository:

    git push --all bitbucket
    git push --tags bitbucket
  6. Remove your temporary local repository:

    cd ..
    rm -rf old-repository

Mirror an existing Git repository

You can mirror an existing repository into a repository hosted in Bitbucket.

  1. Check out the repository from your existing Git host. Use the --mirror parameter:

    git clone --mirror https://username@bitbucket.org/exampleuser/repository-to-mirror.git
  2. Log into Bitbucket and create a new repository (we've called it repo.git in this example).

  3. Locate the clone URL in the nav panel on the left (for example:  https://username@your.bitbucket.domain:7999 /yourproject/repo.git).

  4. Add Bitbucket as another remote in your local repository:

    git remote add bitbucket https://username@your.bitbucket.domain:7999/yourproject/repo.git
  5. Then push all branches and tags to Bitbucket:

    git push --all bitbucket
    git push --tags bitbucket
  6. Use git fetch --prune origin  ('–prune' will remove any branches that no longer exist in the remote) followed by the git push commands from step 5 to update the Bitbucket mirror with new changes from the upstream repository.
Last modified on Jan 4, 2023

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.