hotmeatballsoup
hotmeatballsoup

Reputation: 605

How to connect BitBucket repository and existing local git project once there is already code on BitBucket

Please note: although I mention BitBucket here, I think this is a git question at heart and can be answered by any experience git user. I only mention BitBucket so that folks are aware that I am not using GitHub and as such, can't solve the problem with GitHub-specific solutions. But again, I think the answer is 100% on the git command-line.

Also, if given the preference between a BitBucket UI-based solution, or a pure git command-line solution, I certainly prefer the latter!


I've got a cart-before-the-horse scenario going on here.

I was asked to create a new project and write some code for it, because at the time the people I'm working with weren't ready for a new repository to be created in their BitBucket workspace. So I got a POC working locally, named my project some-service and now am ready to push that project up to a new BitBucket repository to share with other devs. I would like both a main and develop branch (or whatever is the norm for git/BitBucket these days).

Problem is, they are not willing to give me "Create New Repository" permissions, and so they are insisting that they create the repository and simply give me read/write access to it. So, on BitBucket, they have created a new repository called some-service that contains a README.md and a .gitignore and it has a sole master branch.

Normally on GitHub, I create the repos myself, and they have a main branch, and GitHub provides me with a few commad lines to connect/sync up my existing local repo with the new one on GitHub.

What can I do here, ideally solely from the git command line, so that:

Upvotes: 5

Views: 9605

Answers (1)

Zois Tasoulas
Zois Tasoulas

Reputation: 1533

You can add a remote for your local repository.

Specifically

# Add bit bucket repository as remote
git remote add origin <link to bit bucket repository>

# Fetch remote changes (Readme etc)
git fetch origin

# Rebase local commit on top of remote
git rebase origin/master

# Push main branch to origin
git push -u origin main

# GUI interface necessary at this point for BitBucket.
# Navigate to the repository, then enter "Repository settings" (appears on
# the left list menu), enter "ADVANCED", under "Main branch" select 
# "main". Finally, click "Save changes".

# Delete master branch on remote
git push origin --delete master

# Create the develop branch, assuming it doesn't exist
git checkout -b "develop"

# Push develop branch to origin
git push -u origin develop

Upvotes: 13

Related Questions