Create a repository for your existing files

Redirection notice

This page will redirect to /bitbucket/set-up-a-repository-877174034.html .

If you've been working on a project on your local system, it's probably a good idea to put it into source control. That way, you won't lose it and you can go back and look at previous versions.

Alternatively, you may already have it versioned using Git or Mercurial, but you now want to start collaborating with a few others. Either way, you're at the right place for getting started.

This page

Step 1: Create a repository on Bitbucket

To create a repository, do the following:

  1. Log into Bitbucket.
  2. Click Repositories > Create repository or the Create new repository link.
  3. Choose a repository Owner.
    This only appears if you are part of a team. If you create the repo under your team name, you'll have to be removed from the team to be removed from the repository.
  4. Enter a Name and Description for your repository.
  5. Tick Private if you want to hide your repository from the general public, so that only selected people can see it.
  6. Select the Repository type.
  7. Click Create repository.

If you create a repository with a mixed upper and lower case name, Bitbucket converts the name to all lower case in the repository URL. The name appears in mixed case in the UI. You cannot create two repositories with names that result in the same URL.

After you create a repository, Bitbucket provides you with a help panel. This help panel is an easy way to get command tips for moving forward.

Step 2: Get your code onto Bitbucket

Now that you have a repository, let's get your code into it. You can either add unversioned code or push an existing Git or Mercurial project.

Add an existing, unversioned code to an empty repository

If you have code on your local machine that is not under source control, you can prepare it by making it's directory a Git or Mercurial repository. From there, you push it to Bitbucket.

If you want a Git repository

  1. From your terminal, change to the root directory of your existing code.
  2. Initialize the directory under source control from the following command:

    $ git init
  3. Add the existing files to the repository you have initialized:

    $ git add .
  4. Commit the files:

    $ git commit -m 'initial commit of full repository'
  5. From the Overview page of the repository you created in Bitbucket, choose I have an existing project.
  6. Connect your new local repository to the remote repository on Bitbucket. To do that, copy the git remote path that you see under I have an existing project and enter it in the command line.
  7. Push all the code in your local repo to Bitbucket with the following command:

    $ git push -u origin --all

If you want a Mercurial repository

  1. From your terminal, change to the root directory of your existing code.
  2. Initialize the directory under source control from the following command:

    $ hg init
  3. Add the existing files to the repository you have initialized:

    $ hg add
  4. Commit the files:

    $ hg commit -m 'initial commit of full repository'
  5. From the Overview page of the repository you created in Bitbucket, choose I have an existing project.
  6. Connect your new local repository to the remote repository and push all your code to Bitbucket at the same time. To do that, copy the hg push path that you see under I have an existing project and enter it in the command line.

Pushing a Git or Mercurial project to an empty repository

You can upload an existing repository to a empty project in Bitbucket. When you do this, Bitbucket maintains your commit history.

Pushing a Git project

This kind of push overwrites the contents of the Bitbucket repository. You should use it with great caution.

  1. From the command line, verify your SSH key is working.

    $ ssh -T git@bitbucket.org
    conq: logged in as tutorials.

    You can use git or hg to connect to Bitbucket. Shell access is disabled.

    The message should report you are logged in as your Bitbucket account. In this example, the ssh key was on the tutorials account. If you don't get this message, stop and troubleshoot your SSH connection to Bitbucket. (See Use the SSH protocol with Bitbucket Cloud for information about doing this.)

  2. Navigate to the root directory of the repository you want to push.

    $ cd ~/repos/<repo_name>
  3. Push the local repository up to Bitbucket.

    $ git push --mirror git@bitbucket.org:<account_name>/<repo_name>
Push Several Branches at Once

If you want, you can push several branches at once to your Bitbucket repository. To do this, do the following:

  1. Checkout each branch to your local repository from your existing Git host.
  2. List all the branches to make sure you have what you want:

    $ git branch -a
  3. Make sure the remote origin in your local repository points to Bitbucket.
    For example, if you are using ssh with Bitbucket, you would use the following:

  4. Then push all the branches to Bitbucket:

    $ git push -u origin --all

Pushing a Mercurial project

This kind of push overwrites the contents of the Bitbucket repository. You should use it with great caution.

  1. From your Mercurial repository on your local system, open the .hg subdirectory.

    Files that start with a . (period) are hidden files in Mac OSX. By default, the Finder does not show these files. To reveal hidden files, enter the following command into the terminal:

    $ defaults write com.apple.finder AppleShowAllFiles YES

    To hide them files once again, enter the following command:

    $ defaults write com.apple.finder AppleShowAllFiles NO
  2. Open the hgrc file.
  3. Edit the default value by changing it to the address of the empty repository you created in Bitbucket. The top of your file will look similar to the following:

    [paths]
    default = ssh://hg@bitbucket.org/<account_name>/<repo_name>
  4. Save and close the file.
  5. Push the repository:

    $ hg push

Credits

The following users contributed provided information that improved this page. Thank you.

Where do you go next?

Was this helpful?

Thanks for your feedback!

Why was this unhelpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport