Connecting a new git repository to Bitbucket
Whenever I start a new project (or sub-project, such as a new custom plugin for a WordPress site I’m working on) I almost always create a new git repository for it. But I’m forever forgetting the process for connecting the two. So here’s how I do it.
Initial context
- I’m using git on the command line
- My repository is hosted by Bitbucket
What I do first…
- I open a command prompt using Terminal (I’m on MacOS) or the terminal built into VS Code in the my main working folder
- I run
>git init - I add a file named “.gitignore” to my folder
- I add the content listed below to this file
- I run
>git add --all>git commit -m 'Initial commit'
to commit the changes
In Bitbucket…
- I click the Create button and then Repository
- I select a project, give the repository a name, set the default branch name to “main” and click Create repository
- On the new repository screen I click the button labelled clone, make sure SSH is selected and copy the URL (the text after “git clone”)
Back in terminal on my local machine…
- I run
>git remote add origin git@bitbucket.org:[url copied above]
>git push --force origin main
Note, the --force flag will delete whatever was in the remote repo, so don’t use this for repositories where there are already commits and content
Next time you push, you may have to align the branches with:>git push --set-upstream origin main
…as you would with a new branch.
.gitignore
# These are some examples of commonly ignored file patterns.
# You should customize this list as applicable to your project.
# Learn more about .gitignore:
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore
# Node artifact files
node_modules/
dist/
# Log files
*.log
# Generated by MacOS
.DS_Store
# Generated by Windows
Thumbs.db
# Applications
*.app
*.exe
*.war
# Large media files
*.mp4
*.tiff
*.avi
*.flv
*.mov
*.wmv
# Environment variables
config/