Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (2024)

Step 1. Create the repository

Initially, the repository you create in Bitbucket is going to be empty without any code in it. That's okay because you will start adding some files to it soon. This Bitbucket repository will be the central repository for your files, which means that others can access that repository if you give them permission. After creating a repository, you'll copy a version to your local system—that way you can update it from one repo, then transfer those changes to the other.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (1)

Do the following to create your repository:

1. From Bitbucket, click the+icon in the global sidebar and selectRepository.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (2)

Bitbucket displays theCreate a new repositorypage. Take some time to review the dialog's contents. With the exception of theRepository type, everything you enter on this page you can later change.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (3)

2. EnterBitbucketStationLocationsfor theNamefield. Bitbucket uses thisNamein the URL of the repository. For example, if the userthe_besthas a repository calledawesome_repo, the URL for that repository would behttps://bitbucket.org/the_best/awesome_repo.

3. ForAccess level, leave theThis is a private repository boxchecked. A private repository is only visible to you and those you give access to. If this box is unchecked, everyone can see your repository.

4. PickGitfor theRepository type. Keep in mind that you can't change the repository type after you clickCreate repository.

5. ClickCreate repository. Bitbucket creates your repository and displays itsOverviewpage.

Step 2. Explore your new repository

Take some time to explore the repository you have just created. You should be on the repository'sOverviewpage:

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (4)

Click+from the global sidebar for common actions for a repository. Click items in the navigation sidebar to see what's behind each one, includingSettingsto update repository details and other settings. To view the shortcuts available to navigate these items, press the?key on your keyboard.

When you click theCommitsoption in the sidebar, you find that you have no commits because you have not created any content for your repository. Your repository is private and you have not invited anyone to the repository, so the only person who can create or edit the repository's content right now is you, the repository owner.

Copy your Git repository and add files

Now that you have a place to add and share your space station files, you need a way to get to it from your local system. To set that up, you want to copy the Bitbucket repository to your system. Git refers to copying a repository as "cloning" it. When you clone a repository, you create a connection between the Bitbucket server (which Git knows as origin) and your local system.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (5)

Step 1. Clone your repository to your local system

Open a browser and a terminal window from your desktop. After opening the terminal window, do the following:

1. Navigate to your home (~) directory

$cd~

As you use Bitbucket more, you will probably work in multiple repositories. For that reason, it's a good idea to create a directory to contain all those repositories.

2. Create a directory to contain your repositories.

$mkdirrepos

3. From the terminal, update the directory you want to work in to your new repos directory.

$cd~/repos

4. From Bitbucket, go to yourBitbucketStationLocationsrepository.

5. Clickthe+icon in the global sidebar and selectClone this repository.

Bitbucket displays a pop-up clone dialog. By default, the clone dialog sets the protocol toHTTPSorSSH, depending on your settings. For the purposes of this tutorial, don't change your default protocol.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (6)

6. Copy the highlighted clone command.

7. From your terminal window, paste the command you copied from Bitbucket and pressReturn.

8. Enter your Bitbucket password when the terminal asks for it. If you created an account by linking to Google, use your password for that account.

  • If you experience aWindows password error:
    • In some versions of Microsoft Windows operating system and Git you might see an error similar to the one in the following example.

      Windows clone password error example

$gitclone

https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git

Cloninginto'bitbucketspacestation'...

fatal:couldnotread

Passwordfor'https://emmap1@bitbucket.org':Nosuchfileordirectory

  • If you get this error, enter the following at the command line:
$gitconfig--globalcore.askpass
  • Then go back to step 4 and repeat the clone process. The bash agent should now prompt you for your password. You should only have to do this once.

At this point, your terminal window should look similar to this:

$cd~/repos

$gitclonehttps://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git
Cloninginto'bitbucketstationlocations'...
Password
warning:Youappeartohaveclonedanemptyrepository.

  • You already knew that your repository was empty right? Remember that you have added no source files to it yet.

9. List the contents of your repos directory and you should see yourbitbucketstationlocationsdirectory in it.

$ls

Congratulations! You've cloned your repository to your local system.

Step 2. Add a file to your local repository and put it on Bitbucket

With the repository on your local system, it's time to get to work. You want to start keeping track of all your space station locations. To do so, let's create a file about all your locations.

1. Go to your terminal window and navigate to the top level of your local repository.

$cd~/repos/bitbucketstationlocations/

2. Enter the following line into your terminal window to create a new file with content.

$echo"Earth'sMoon">>locations.txt

If the command line doesn't return anything, it means you created the file correctly!

3. Get the status of your local repository. Thegit statuscommand tells you about how your project is progressing in comparison to your Bitbucket repository.

At this point, Git is aware that you created a new file, and you'll see something like this:

$gitstatus
Onbranchmain
Initialcommit
Untrackedfiles:
(use"gitadd<file>..."toincludeinwhatwillbecommitted)
locations.txt
nothingaddedtocommitbutuntrackedfilespresent(use"gitadd"totrack)

The file is untracked, meaning that Git sees a file not part of a previous commit. The status output also shows you the next step: adding the file.

4. Tell Git to track your newlocations.txtfile using thegit addcommand. Just like when you created a file, thegit addcommand doesn't return anything when you enter it correctly.

$gitaddlocations.txt

Thegit addcommand moves changes from the working directory to the Git staging area. The staging area is where you prepare a snapshot of a set of changes before committing them to the official history.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (7)

5.Check the status of the file.

$gitstatus
Onbranchmain
Initialcommit
Changestobecommitted:
(use"gitrm--cached<file>..."tounstage)
newfile:locations.txt

Now you can see the new file has been added (staged) and you can commit it when you are ready. Thegit statuscommand displays the state of the working directory and the staged snapshot.

6. Issue thegit commitcommand with a commit message, as shown on the next line. The -m indicates that a commit message follows.

$gitcommit-m'Initialcommit'
[main(root-commit)fedc3d3]Initialcommit
1filechanged,1insertion(+)
createmode100644locations.txt

Thegit committakes the staged snapshot and commits it to the project history. Combined withgit add, this process defines the basic workflow for all Git users.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (8)

Up until this point, everything you have done is on your local system and invisible to your Bitbucket repository until you push those changes.

  • Learn a bit more about Git and remote repositories
    • Git's ability to communicate with remote repositories (in your case, Bitbucket is the remote repository) is the foundation of every Git-based collaboration workflow.
    • Git's collaboration model gives every developer their own copy of the repository, complete with its own local history and branch structure. Users typically need to share a series of commits rather than a single changeset. Instead of committing a changeset from a working copy to the central repository, Git lets you share entire branches between repositories.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (9)

  • You manage connections with other repositories and publish local history by "pushing" branches to other repositories. You see what others have contributed by "pulling" branches into your local repository.

7.Go back to your local terminal window and send your committed changes to Bitbucket usinggit push originmain. This command specifies that you are pushing to the mainbranch (the branch on Bitbucket) on origin (the Bitbucket server).

You should see something similar to the following response:

$gitpushoriginmain
Countingobjects:3,done.
Writingobjects:100%(3/3),253bytes|0bytes/s,done.
Total3(delta0),reused0(delta0)Tohttps://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git
*[newbranch]main->main
Branchmainsetuptotrackremotebranchmainfromorigin.

Your commits are now on the remote repository (origin).

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (10)

8. Go to yourBitbucketStationLocationsrepository on Bitbucket.

9. If you clickCommitsin the sidebar, you'llsee a single commit on your repository.Bitbucket combines all the things you just did into that commit and shows it to you. You can see that theAuthorcolumn shows the value you used when you configured the Git global file(~/.gitconfig).
If you clickSourcein the sidebar, you'llsee that you have a single source file in your repository, thelocations.txtfile you just added.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (11)

Remember how the repository looked when you first created it? It probably looks a bit different now.

Pull changes from your Git repository on Bitbucket Cloud

Next on your list of space station administrator activities, you need a file with more details about your locations. Since you don't have many locations at the moment, you are going to add them right from Bitbucket.

Step 1. Create a file in Bitbucket

To add your new locations file, do the following:

1. From yourBitbucketStationLocationsrepository, clickSourceto open the source directory. Notice you only have one file,locations.txt, in your directory.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (12)

A. Source page:Click the link to open this page.
B. Branch selection:Pick the branch you want to view.
C. More options button:Click to open a menu with more options, such as 'Add file'.
D. Source file area:View the directory of files in Bitbucket.

2. From theSourcepage, click theMore optionsbutton in the top right corner and selectAdd filefrom the menu. TheMore optionsbutton only appears after you have added at least one file to the repository.

A page for creating the new file opens, as shown in the following image.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (13)

A. Branch with new file:Change if you want to add file to a different branch.
B. New file area:Add content for your new file here.

3. Enterstationlocationsin thefilenamefield.

4. SelectHTMLfrom theSyntax modelist.

5. Add the following HTML code into the text box:

<p>Bitbuckethasthefollowingspacestations:</p>
<p>
<b>Earth'sMoon</b><br>
Headquarters
</p>

6. ClickCommit. TheCommit messagefield appears with the message:stationlocations created onlinewithBitbucket.

7. ClickCommitunder the message field.

You now have a new file in Bitbucket! You are taken to a page with details of the commit, where you can see the change you just made:

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (14)

If you want to see a list of the commits you've made so far, clickCommitsin the sidebar.

Step 2. Pull changes from a remote repository

Now we need to get that new file into your local repository. The process is pretty straight forward, basically just the reverse of the push you used to get thelocations.txtfile into Bitbucket.

To pull the file into your local repository, do the following:

1. Open your terminal window and navigate to the top level of your local repository.

$cd~/repos/bitbucketstationlocations/

2. Enter thegit pull--allcommand to pull all the changes from Bitbucket. (In more complex branching workflows, pulling and merging all changes might not be appropriate.) Enter your Bitbucket password when asked for it. Your terminal should look similar to the following

$gitpull--all
Fetchingorigin
remote:Countingobjects:3,done.
remote:Compressingobjects:100%(3/3),done.
remote:Total3(delta0),reused0(delta0)
Unpackingobjects:100%(3/3),done.
Fromhttps://bitbucket.org/emmap1/bitbucketstationlocations
fe5a280..fcbeeb0main->origin/main
Updatingfe5a280..fcbeeb0
Fast-forward
stationlocations|5++++++++++++++
1filechanged,5insertions(+)
createmode100644stationlocations

Thegit pullcommand merges the file from your remote repository (Bitbucket) into your local repository with a single command.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (15)

3. Navigate to your repository folder on your local system and you'll see the file you just added.

Fantastic! With the addition of the two files about your space station location, you have performed the basic Git workflow (clone, add, commit, push, and pull) between Bitbucket and your local system.

Use a Git branch to merge a file

Being a space station administrator comes with certain responsibilities. Sometimes you’ll need to keep information locked down, especially when mapping out new locations in the solar system. Learning branches will allow you to update your files and only share the information when you're ready.

Branches are most powerful when you're working on a team. You can work on your own part of a project from your own branch, pull updates from Bitbucket, and then merge all your work into the main branch when it's ready.Our documentationincludes more explanation of why you would want to use branches.

A branch represents an independent line of development for your repository. Think of it as a brand-new working directory, staging area, and project history. Before you create any new branches, you automatically start out with the main branch. For a visual example, this diagram shows the mainbranch and the other branch with a bug fix update.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (16)

Step 1. Create a branch and make a change

Create a branch where you can add future plans for the space station that you aren't ready to commit. When you are ready to make those plans known to all, you can merge the changes into your Bitbucket repository and then delete the no-longer-needed branch.

It's important to understand that branches are just pointers to commits. When you create a branch, all Git needs to do is create a new pointer—it doesn’t create a whole new set of files or folders. Before you begin, your repository looks like this:

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (17)

To create a branch, do the following:

1. Go to your terminal window and navigate to the top level of your local repository using the following command:

cd~/repos/bitbucketstationlocations/

2. Create a branch from your terminal window.

$gitbranchfuture-plans

This command creates a branch but does not switch you to that branch, so your repository looks something like this:

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (18)

The repository history remains unchanged. All you get is a new pointer to the current branch. To begin working on the new branch, you have to check out the branch you want to use.

3. Checkout the new branch you just created to start using it.

$gitcheckoutfuture-plans
Switchedtobranch'future-plans'

Thegit checkoutcommand works hand-in-hand withgitbranch . Because you are creating a branch to work on something new, every time you create a new branch (withgit branch), you want to make sure to check it out (withgit checkout) if you're going to use it. Now that you’ve checked out the new branch, your Git workflow looks something like this:

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (19)

4. Search for thebitbucketstationlocationsfolder on your local system and open it. You will notice there are no extra files or folders in the directory as a result of the new branch.

5. Open thestationlocationsfile using a text editor.

6. Make a change to the file by adding another station location:

<p>Bitbuckethasthefollowingspacestations:</p>
<p>
<b>Earth'sMoon</b><br>
Headquarters
</p>
<p>
<b>Mars</b><br>
RecreationDepartment
</p>

7. Save and close the file.

8. Entergit statusin the terminal window. You will see something like this:

$gitstatus
Onbranchfuture-plans
Changesnotstagedforcommit:
(use"gitadd<file>..."toupdatewhatwillbecommitted)
(use"gitcheckout--<file>..."todiscardchangesinworkingdirectory)
modified:stationlocations
nochangesaddedtocommit(use"gitadd"and/or"gitcommit-a")

Notice theOn branch future-plansline? If you enteredgit statuspreviously, the line was onbranchmainbecause you only had the onemainbranch. Before you stage or commit a change, always check this line to make sure the branch where you want to add the change is checked out.

9. Stage your file.

$gitaddstationlocations

10. Enter thegit commitcommand in the terminal window, as shown with the following:

$gitcommitstationlocations-m'makingachangeinabranch'
[future-planse3b7732]makingachangeinabranch
1filechanged,4insertions(+)

With this recent commit, your repository looks something like this:

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (20)

Now it's time to merge the change that you just made back into themainbranch.

Step 2. Merge your branch: fast-forward merging

Your space station is growing, and it's time for the opening ceremony of your Mars location. Now that your future plans are becoming a reality, you can merge yourfuture-plansbranch into the main branch on your local system.

Because you created only one branch and made one change, use the fast-forward branch method to merge. You can do a fast-forward merge because you have a linear path from the current branch tip to the target branch. Instead of “actually” merging the branches, all Git has to do to integrate the histories is move (i.e., “fast-forward”) the current branch tip up to the target branch tip. This effectively combines the histories, since all of the commits reachable from the target branch are now available through the current one.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (21)

This branch workflow is common for short-lived topic branches with smaller changes and are not as common for longer-running features.

To complete a fast-forward merge do the following:

1. Go to your terminal window and navigate to the top level of your local repository.

$cd~/repos/bitbucketstationlocations/

2. Enter thegit statuscommand to be sure you have all your changes committed and find out what branch you have checked out.

$gitstatus
Onbranchfuture-plans
nothingtocommit,workingdirectoryclean

3. Switch to themainbranch.

$gitcheckoutmain
Switchedtobranch'main'
Yourbranchisup-to-datewith'origin/main'.

4. Merge changes from thefuture-plansbranch into themainbranch. It will look something like this:

$gitmergefuture-plans
Updatingfcbeeb0..e3b7732
Fast-forward
stationlocations|4++++
1filechanged,4insertions(+)

You've essentially moved the pointer for themainbranch forward to the current head and your repository looks something like the fast forward merge above.

5. Because you don't plan on usingfuture-plansanymore, you can delete the branch.

$gitbranch-dfuture-plans
Deletedbranchfuture-plans(wase3b7732).

When you deletefuture-plans, you can still access the branch frommainusing a commit id. For example, if you want to undo the changes added fromfuture-plans, use the commit id you just received to go back to that branch.

6. Entergit statusto see the results of your merge, which show that your local repository is one ahead of your remote repository. It will look something like this:

$gitstatus
Onbranchmain
Yourbranchisaheadof'origin/main'by1commit.
(use"gitpush"topublishyourlocalcommits)
nothingtocommit,workingdirectoryclean

Here's what you've done so far:

  • Created a branch and checked it out
  • Made a change in the new branch
  • Committed the change to the new branch
  • Integrated that change back into the main branch
  • Deleted the branch you are no longer using

Next, we need to push all this work back up to Bitbucket, your remote repository.

Step 3. Push your change to Bitbucket

You want to make it possible for everyone else to see the location of the new space station. To do so, you can push the current state of your local repository to Bitbucket.

This diagram shows what happens when your local repository has changes that the central repository does not have and you push those changes to Bitbucket.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (22)

Here's how to push your change to the remote repository:

1. From the repository directory in your terminal window, entergit push originmainto push the changes. It will result in something like this:

$gitpushoriginmain
Countingobjects:3,done.
Deltacompressionusingupto8threads.
Compressingobjects:100%(3/3),done.
Writingobjects:100%(3/3),401bytes|0bytes/s,done.
Total3(delta0),reused0(delta0)
Tohttps://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git
fcbeeb0..e3b7732main->main

2. Click theOverviewpage of your Bitbucket repository, and notice you can see your push in theRecent Activitystream.

3. ClickCommitsand you can see the commit you made on your local system. Notice that the change keeps the same commit id as it had on your local system.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (23)

You can also see that the line to the left of the commits list has a straight-forward path and shows no branches. That’s because thefuture-plansbranch never interacted with the remote repository, only the change we created and committed.

4. ClickBranchesand notice that the page has no record of the branch either.

5. ClickSource, and then click thestationlocationsfile. You can see the last change to the file has the commit id you just pushed.

6. Click the file history list to see the changes committed for this file, which will look similar to the following figure.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (24)

You are done!

Not sure you will be able to remember all the Git commands you just learned? No problem. Bookmark ourbasic Git commandspage so that you can refer to it when needed.

Learn Git with Bitbucket Cloud | Atlassian Git Tutorial (2024)

References

Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 6088

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.