Keeping a track of your code changes and its history is very important for a developer. Bitbucket is a Git-based source code repository hosting service owned by Atlassian. Bitbucket offers both commercial plans and free accounts with an unlimited number of private repositories.
In this post, we will push our code to the created repository in Bitbucket using the Command line with Git. I hope, you already have the Bitbucket repository created. If not, refer to my post on How to Create a new Bitbucket Code Repository? Make sure, not to include .gitignore and README.md files while creating the repo.
- Git : Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
- Bitbucket Account
Push your Code to Bitbucket Repo
You can make use of Git Bash or Command line for this purpose. Open the terminal from the project folder. To do that, on the navigation path type cmd and hit enter. It will open the terminal from the project directory.
To initialize an empty local git repository, type the below command.
Next, we need to add files to the staging area for Git. Before the files are committed to a repository, the files need to be added to the Staging area. You can either add individual files or directories or add all unstaged files using the below command.
git add .
For adding individual file, type the file name in place of the dot.
git add README.md
Now we need to commit our code changes made to the files to a local repository. Each commit will have a unique ID for reference. It is important to add a commit message as well, that will tell us what changes we have made.
git commit -m "first commit"
Below is the output similar to this you will get when you commit any changes.
To see the branches in your repository, use the below command.
-M option, <oldbranch> will be renamed to <newbranch>. If <oldbranch> had a corresponding reflog, it is renamed to match <newbranch>, and a reflog entry is created to remember the branch renaming. If <newbranch> exists, -M must be used to force the rename to happen.
git branch -M main
To connect a local repository with a remote repository. In this case, our remote repository is Bitbucket. Please give the URL of your repository.
git remote add origin <Bitbucket Repo URL>.git
You can find the URL from Bitbucket. When you open the Repo in the portal, you will see that your repo is empty, there you will find the URL.
Finally, we push our code to the Bitbucket. You may be asked to log in to Bitbucket if you are pushing your code for the first time.
git push -u origin --all
Below is similar response you will get when code is pushed.
Sometimes there are changes in your Bitbucket (may be changed by your team member) that are not present locally, in such cases, you might receive an error message as below.
So first we need to get the changes from the remote repository to our local repository. This command will get all the changes to the local repo.
It is a good practice to pull the changes first and then make your changes on top of it. There are many other git commands available. I have covered the basic ones which are sufficient for you to get started. To learn more about the git commands, refer to the official documentation here.