Add project to bitbucket

In this article, we will take a look at the steps to add a project which exists on a local machine to bitbucket repository initially.
That is, push code to bitbucket repository for the first time using Git bash terminal.

Import local project to bitbucket

Following is the step by step procedure to add a project from your system to bitbucket.

The steps also cover the errors which may arise in each step.
1. Open Git bash terminal.
If you are on Windows, right click and you will see an option of Git Bash in context menu.
2. Navigate to the location where your local project resides using cd command.
3. Initialize the project with Git configuration using command

git –init

This will create a new directory .git, which is hidden by default in the project folder.
init will convert your project folder into local git repository.
4. Add files that you need to add to bitbucket repository using git add command followed by the files or folders to add.
If you want to add all the files, then use

git add –all

5. Commit the files to your local repository using command

git commit -m <message>

6. Login to your bitbucket account.
7. Create a repository by clicking Create -> Repository buttons.
8. Once your repository is created, click on Clone button as shown below

Clone bitbucket repository

Once you click on Clone, the URL of the repository will be visible. Copy that.
9. In the terminal, check the URL at which the local repository points, with the below command

git remote -v

If the URL is different than the one in Step 8, execute Step 10 to point local repository to remote one.
10. In the terminal execute below command

git remote add <URL>

where URL, is the one that was copied in Step 8.
11. Execute git push command as below

git push –set-upstream origin master

This will associate the remote branch with the local one.
--set-upstream is required for the first time only. Further commits will require only git push.

You can also execute git push with -u flag as

git push -u origin master

12. Optional
It might happen that the remote repository already exists.
This means that there is already one or more commits and your local branch is behind the remote or origin branch.
If this is the case, git push will fail with below error

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes

So, you need to execute git pull to bring remote changes to your local branch and bring it to the same state(or fast-forward it).
13. Optional
If you executed git pull, you might encounter below error

There is no tracking information for the current branch.
Please specify which branch you want to merge with.

To resolve this, execute

git pull origin <branchName>

to specify which remote branch you want to pull from.

Congratulations! your local project is now successfully imported into bitbucket repository.

Refer the article from bitbucket support page for reference.
Hope the current article was useful.