Clone Your Git Repo and Add Source Files
On this page, you'll learn how to add code to your repository. At this point, you have created a
bb101repo which is a private repository that only you, the owner, can see or work in. This is not typical. Usually, you want some help on a project and you won't be the only one working in a repository. However, this is a tutorial and right now you need to learn without being distracted by other people. We'll add more people to your project later.
Step 1. Clone your repository to your local system
Open a browser and a Git Bash window (also called a terminal window) from your desktop. The terminal window on your local system which is the system you code on as opposed to the remote Bitbucket server. After opening the terminal window, do the following:
- Navigate to your home (~) directory.
As you work with code, you will find that you have multiple repositories that you work in. It is a good idea to create a directory to contain all those repositories. Generally, this is your home directory but it can be anywhere you want it to be.
Create a directory to contain your repositories.
- If you haven't already done so, go to Bitbucket in your browser and log into your Bitbucket account.
Go to your
- Click Clone button.
The system displays a pop-up clone dialog. By default, the clone dialog sets the protocol to HTTPS. Leave it there.
- Click on the command line field that accompanies the protocol.
When you click on the command field it automatically highlights the command for you.
- Copy the highlighted
Switch back to your terminal window.Change directory to your new
Paste the command you copied from Bitbucket onto the command line and press Return.Click to view GitBash cut 'n paste tips...
Use the menu to go into Mark mode and then select text. Pressing Enter adds the text to the clipboard. Paste text by pressing
INSERTon your keyboard or using the menu.
Git will ask you for the repository password. This is the password you entered when you created your Bitbucket account. If you created an account by linking to Google or Facebook and you are following this tutorial, you should have created a password already.
Enter your password.
Git creates the repository but warns you that you have cloned an empty repository. At this point, your terminal window should look something like this:
You already knew that your repository was empty right? Recall that you have added no source files to it yet.
List the contents of your
reposdirectory and you should see your
bb101repodirectory in it.
Step 2. Explore your repository and fix a problem
Git said you had cloned an empty repository. This is true, your repository is empty but the directory that git created is not entirely empty. List the contents of your local repository – including the hidden files. A local repository is the copy of a repository you have on your local system. After listing the contents of your local repository, you should see something like this:
git directory contains special files and directories used by the Git system. For now, you should be aware that it is there.
Notice that Git went ahead and named your repository's directory with the same name as you did when you created it. You could have cloned the repository under a totally different name. For example, you could cloned the repository to a directory called
bb101repo-practice. The advantage of this name is that it provides a clue about what you were going to do with clone. In fact, it is good not to simply use the repository name when you clone but indicate what you are doing with the clone. Why don't you fix that right now:
Remove the repository you just created.
Reissue the clone command but this time give Git a name that indicates what you are doing.
For example, you might want to call the clone
Again, Git will tell you that you are cloning an empty repository.
~/reposdirectory, you should see something similar to the following:
Great. Now you are ready to add content to your repository.
Step 3. Create a file, add it locally, and push it to the Bitbucket Server
Bitbucket lets you set a repository's description but you may want to provide a file to get you started. The commands for this section will appear in the Command line section of the Repository setup page.
Go to your terminal window and navigate to the top level of your local repository.
Click the I'm starting from scratch section of the Repository setup page.
We have already completed the first portion of this process (Set up your local directory) in great detail. However these commands are available when you set up a new repository for your own use.
Copy the first command line from the Create your first file, commit, and push and past into your terminal window.
You've just created a file!
Go ahead and get the status of your local repository.
git statuscommand tells you about how your project is progressing in comparison to your Bitbucket repository. You should see something like this:
You can see that Git is aware that you created a file in your local repository. The status output also shows you the next step, the add.
Tell git to track your new contributers.txt file using the
You must perform this step before you can commit a file. What happens if you run the
statuscommand now? Git sees that you have a new file in your local repository and that you can potentially commit it.
Commit all the changes you added.
Right now, the only change pending in your local repository is the new file. When you issue the
commitcommand you see this:
Up until this point, everything you have done is local – that is on your local system. It is not visible in your Bitbucket repository until you put it there with the
pushcommand. You can test this to see if it is true by opening the Bitbucket
bb201repoOverview page in your browser. You should see that the Overview looks exactly as it did when you started.
Go back to your local terminal window and push your committed changes using the
You should see something similar to the following:
Go to your Bitbucket bb201repo repository in your browser and click the Commits item on the menu bar.
You should see a single commit on your repository.
Bitbucket traps a lot of information with your commit and shows it to you. You can see that the Author column shows the value you used when you configured the Git global file (
- Select the Source option.
You should see that you have a single source file in your repository, the contributors.txt file you just added
Remember how the Overview page looked when you first created your repo? Take some time and explore a little. Click on buttons and travel down some links.
Now, you have finished the simplest workflow between Bitbucket and a DVCS system. You created a local repository, added a new file to it, and pushed those changes to the Bitbucket repository. At this point you may have a few thoughts in your head such as:
- Hey, I don't consider a contributers.txt file to be source code.
- Aren't other people supposed to help me with my repository?
- This Git stuff is fine but I want to use Mercurial.
The next page of this tutorial will resolve some of these thoughts because you will use the fork operation and use Mercurial to add code to another user's repository.