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.

Important reminders about these instructions

The git commands on this page are for a GitBash terminal. However, the commands are nearly identical in a Mac OSX terminal and in an Ubuntu Linux terminal. You shouldn't have any trouble following along but if you do, please comment on the page and we'll make corrections.

This tutorial teaches you a little Git first and later a little Mercurial. All the operations that you do with Git have Mercurial equivalents and of course the reverse is also true. You are free to use Mercurial exclusively with this tutorial if you want, but you'll need to do that on your own.

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:

  1. 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.
  2. Create a directory to contain your repositories.

    mkdir repos
  3. If you haven't already done so, go to Bitbucket in your browser and log into your Bitbucket account.
  4. Go to your bb101repo Overview page.
  5. Click Clone button.
    The system displays a pop-up clone dialog. By default, the clone dialog sets the protocol to HTTPS. Leave it there.
  6. Click on the command line field that accompanies the protocol.
    When you click on the command field it automatically highlights the command for you.
  7. Copy the highlighted git command.
  8. Switch back to your terminal window.Change directory to your new repositories directory.

    cd repos

    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 INSERT on 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.

    Windows password error

    In some versions of the Microsoft Windows operating system and Git you might see an error similar 
    to the one in the following example.
    Windows clone password error example
    $ git clone
    Cloning into 'bb201repo'...
    fatal: could not read Password for '': No such
    file or directory

    If you get this error enter the following at the command line:

    git config --global core.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.

  9. 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:

    $ cd ~/repos
    $ git clone
    Cloning into 'bb101repo'...
    warning: You appear to have cloned an empty repository.

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

  10. List the contents of your repos directory and you should see your bb101repo directory 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:

$ ls -al bb101repo/
total 0
drwxr-xr-x 3 manthony staff 102 Dec 14 10:50 .
drwxr-xr-x 3 manthony staff 102 Dec 14 10:50 ..
drwxr-xr-x 9 manthony staff 306 Dec 14 10:50 .git

The .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:

  1. Remove the repository you just created.

    rm -irf bb101repo/
  2. 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 bb101repo-practice:

    $ git clone bb101repo-practice

    Again, Git will tell you that you are cloning an empty repository. 

  3. List your ~/repos directory, you should see something similar to the following:

    $ ls ~/repos

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.

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

    cd ~/repos/bb201repo-practice/
  2. 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.

  3. Copy the first command line from the Create your first file, commit, and push and past into your terminal window.

    echo "Daniel  Stevens" >> contributors.txt

    You've just created a file!

  4. Go ahead and get the status of your local repository.
    The git status command tells you about how your project is progressing in comparison to your Bitbucket repository. You should see something like this:

    dstevens:teams-in-space dstevens$ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
        modified:   contributors.txt
    no changes added to commit (use "git add" and/or "git commit -a")

    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.

  5. Tell git to track your new contributers.txt file using the git add command.

    git add contributors.txt

    You must perform this step before you can commit a file.   What happens if you run the status command now? Git sees that you have a new file in your local repository and that you can potentially commit it.

  6. Commit all the changes you added.
    Right now, the only change pending in your local repository is the new file. When you issue the commit command you see this:

    $ git commit -m 'Initial commit with contributors'
    [master (root-commit) fedc3d3] Initial commit with contributors
     1 file changed, 1 insertion(+)
     create mode 100644 contributors.txt

    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 push command.  You can test this to see if it is true by opening the Bitbucket bb201repo Overview page in your browser.  You should see that the Overview looks exactly as it did when you started.

  7. Go back to your local terminal window and push your committed changes using the git push command.
    You should see something similar to the following:

    teams-in-space-repository-2 dstevens$ git push -u origin master
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 253 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
     * [new branch]      master -> master
    Branch master set up to track remote branch master from origin.

  8. 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 ( ~/.gitconfig).  

  9. 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.

Powered by Confluence and Scroll Viewport