How to git push an existing project to Bitbucket

How to add a Git project to Bitbucket

To import an existing project into Bitbucket, follow these steps:

  1. Create a Bitbucket project to import the existing project into
  2. Copy the Bitbucket project’s Git URL
  3. Issue the git init command in the root folder of your existing project
  4. Add all of your files to the Git repository and perform a commit
  5. Add the Bitbucket repository as a remote reference for your local project
  6. Run a git push operation with your Bitbucket app password
  7. Confirm that all of the files from your existing project have been pushed to Bitbucket

Import into Bitbucket commands

This tutorial will provide full details on how to push an existing project to Bitbucket. For those who just want the ‘tl:dr’ version, here are all of the Git commands used in this example:

  • git init
  • git add .
  • git commit -m "Add project to Bitbucket example"
  • git remote add source https://[email protected]/cameronmcnz/example.git
  • git push -u -f source master

Create the Bitbucket repository

To push an existing project to Bitbucket, you need a Bitbucket repository to push into.

Log into your Bitbucket account and create a new Bitbucket repository.

Once created, copy the Bitbucket’s Git URL, as it will be needed when you perform the git remote add operation later.

bitbucket push git URL

You will need the Bitbucket Git URL to push your existing project to the Atlassian server.

Initialize Git in the existing project

The local project being pushed to Bitbucket must be initialized as a Git repository.

If the existing project has a hidden .git folder in the project’s root directory, then it has already been initialized as a Git project. If not, you must issue the git init command in the root folder of the project.

git init

After the repository is initialized, you must add all of the project’s files to the Git index and perform a commit:

git add .
git commit -m "Add files to git before the Bitbucket push."

Possible git errors and roadblocks

You may run into to issues as you perform the git init and git commit commands in this Bitbucket push example.

Common problems users run into at this stage are:

  • You may not have Git installed. If not, download it and install Git.
  • You have not told Git about your username and email

If the git commit fails due to the lack of a username and email, just issue these two command:

git config --global "[email protected]"
git config --global "Bitbucket Expert"

After these commands complete, issue the git commit operation again.

Add the Bitbucket remote reference

To push and pull back and forth between your existing project and Bitbucket, you must add a remote reference in your local Git repository that points to your Bitbucket project.

To do that, issue the following command, using the Bitbucket URL you copied to your clipboard in an earlier step:

git remote add source https://[email protected]/cameronmcnz/example.git

Upload your project to Bitbucket

With the remote reference added, you can push your existing project to Bitbucket with the following command:

git push -u -f bitbucket master

The existing project’s push to Bitbucket will require you to provide your username and your Bitbucket app password in the password field. If you do not have a Bitbucket app password, you will need to create one.

Note that this example attempts to push the master branch. Newer Git repositories will have a main branch, not a master branch.

You will need to specify the branch name that is used by your existing Git project when you perform the initial push to Bitbucket.

git push -u -f bitbucket main

View the existing project imported into Bitbucket

After the git push to Bitbucket completes, log into your Atlassian account and view your Bitbucket repository.

All of the files from your existing project should be visible online. This proves that the git push of your existing project to Bitbucket was a complete success.