Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Working on BitBucket using Git

  • Difficulty Level : Easy
  • Last Updated : 23 Nov, 2021

Git provides various tools and applications to perform better handling of the work environment. Git is a type of version control system that is used to allow more than one developer to access the source code of a specific application and can modify changes to it that may be seen by other developers. BitBucket is a git-based tool that helps with collaborations on large projects across various teams and helps keep a track of different versions of a project that can access it from your local machine. It is specially designed for teams to collaborate on large projects in big enterprises. It is also used for automated testing and deployment of code. Since this is a git-based tool, you need to have git in your system to work with Bitbucket.

Let’s suppose many developers are developing a project that is divided between various teams and will work on different versions during and after development. For this kind of project, Bitbucket is the best option for individuals or teams.

Now you might be wondering that what is the difference between Github and Bitbucket? 

The most elemental difference between Github and Bitbucket is that Github is more public-friendly, and is used by individual developers, whereas Bitbucket is more private-friendly, and hence is used by large-scale enterprises for the development of big projects.

Note Another way to create a repository and clone it is using the source tree in BitBucket.   

Working with BitBucket

Now’s let’s see how we can work with the Bitbucket using git. It’s pretty simple. You will need to create a repository first. Visit the link below and create an account at Bitbucket. After you have created your account, you will see a button “create repository” in the center of the screen, click it and create a repository. You can choose the repository to be private or public. public repositories are accessible to everyone while private is only accessible to you. Now we will get started with the Bitbucket. 

We will need to clone the project folder into your local machine, which can be done in a few steps as shown below with visual aids 

Step 1: Clone the repository into your local machine

Click on the “clone” button on the right corner of the dashboard. A prompt with the URL link would appear. the URL link would look like “https://USERNAME@bitbucket.org/USERNAME/REPOSITORY_NAME.git”. You can clone using either https or ssh links. 
 

Step 2: Copy and paste the link in the terminal after typing git clone. 

Use the following commands to clone your repository as provided below as follows:



git clone (REPOSITORY URL)
git clone https://USERNAME@bitbucket.org/USERNAME/REPOSITORY_NAME.git

Now, we need to know what does clone means? Clone means, it will retrieve all the contents from that directory to your local machine where you are working, without affecting the Bitbucket repository, until you edit it from the terminal. Using git clone would add the folder to your local machine. 

Step 3: Check your progress by typing “git status” command into the terminal 

git status

Step 4: Creating and adding a file to your Bitbucket repository 

Now, let’s suppose you want to create and add a file to your Bitbucket Repository. Type in the commands below in the terminal.

echo "This is a test file" >> file.txt
git add file.txt

Step 5: Committing changes to BitBucket repository 

The file is now added and is ready to be committed and pushed on your Bitbucket repository. 

git commit -m "Initial Commit"
git push origin master // to push the changes on the Bitbucket repository.

If there are some changes made to your repository. You can use the git pull command to restore them to your local machine. 
 



git pull
git pull origin master 
git pull --all

Any of the three commands would restore the changes into your folder in the local machine. 

Step 6: Performing operations on branches 

As discussed, since this is a tool for working across separate branches, let’s learn how to create, and delete a new branch, and how to switch across branches. 

To create a branch, type in the terminal 

git branch new_branch_name
git branch testbranch // to create a new branch named testbranch

To switch the branch, use the command git checkout

git checkout new_branch_name
git checkout testbranch // to switch to a branch named testbranch

Finally, to merge your branch with the master branch, use the command git merge to merge the two branches. 

git merge branch_name

Step 7: To delete a branch, use git branch -d to delete the branch. 

git branch -d branch_name

In case you have an existing project, switch the current directory to that existing repository in the Terminal or CMD. Then type in the commands below in the terminal or CMD.  

Command Action performed 
git initgit initialization.
git add –allThis stages the newly added files and prepares them for commit.
git remote add origin (repository_url)Use the https or ssh URL link from the bitbucket website to connect to remote Bitbucket repository that you want to add the folder into
git commit -m Initial Commit
git push origin masterPush the files into your Bitbucket repository

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!