Working on BitBucket using Git

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 which may be seen by other developers.
Let’s suppose you 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.

BitBucket is a git-based tool that helps on 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.

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:

  • 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:

    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: Commiting changes to BitBucket Repository
    Your 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 into 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 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.

1. git init // git initialization.
2. git add –all // this stages the newly added files and prepares them for commit.
3. 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.
4. git commit -m “Initial Commit”
5. git push origin master // push the files into your Bitbucket repository.

This shall add the existing folder into your Bitbucket repository.

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

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.

My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.