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.

diagram showing the basics cloning in Git

You are about to use a whole bunch of Git and non-Git commands from a terminal. If you've never used the command line before, learn about where to find it.

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.

    macOS / Linux / Git Bash
    $ cd ~

    Windows Command Prompt
    $ cd <path_to_home>

    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.

    $ mkdir repos

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

    macOS / Linux / Git Bash
    $ cd ~/repos

    Windows Command Prompt
    $ cd repos

  4. From Bitbucket, go to your BitbucketStationLocations repository.

  5. Select the Clone button in the upper-right corner.
    Bitbucket displays the Clone this repository dialog. By default, the clone dialog sets the protocol to HTTPS or SSH, depending on your settings. For the purposes of this tutorial, don't change your default protocol.

  6. Copy the clone command.

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

  8. Enter your app password when the terminal asks for it. 

If you experience a password error:

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 https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git
Cloning into 'bitbucketstationlocations'...
fatal: could not read
Password for 'https://emmap1@bitbucket.org': 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. At this point, your terminal window should look similar to this:

1 2 3 4 5 6 7 8 $ cd ~/repos $ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git Cloning into 'bitbucketstationlocations'... Password remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done.
  1. List the contents of your repos directory and you should see your bitbucketstationlocations directory in it.

    macOS/Linux
    $ ls

    Windows
    $ dir

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.

    macOS / Linux / Git Bash
    $ cd ~/repos/bitbucketstationlocations/

    Windows Command Prompt
    $ cd repos/bitbucketstationlocations/

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

    $ echo "Earth's Moon" >> 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. The git status command 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:

    1 2 3 4 5 6 7 $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) locations.txt nothing added to commit but untracked files present (use "git add" to track)

    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 new locations.txt file using the git add command. Just like when you created a file, the git add command doesn't return anything when you enter it correctly.

    $ git add locations.txt

    The git add command 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.

  5. Check the status of the file.

    1 2 3 4 5 6 $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: locations.txt

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

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

    1 2 3 4 $ git commit -m "Initial commit" [master (root-commit) fedc3d3] Initial commit 1 file changed, 1 insertion(+) create mode 100644 locations.txt

    The git commit takes the staged snapshot and commits it to the project history. Combined with git add, this process defines the basic workflow for all Git users.


    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.

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 using  git push origin master. This command specifies where you are pushing:

  • master — the main branch on your repository (and currently your only branch). A branch allows you do work on a set of code for your repository separate from the main codebase (which would be master in this case).

  • origin— the name of the remote server. In this case, origin indicates that you're pushing to Bitbucket.

You should see something similar to the following response:

1 2 3 4 5 6 $ git push 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) To https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git * [new branch] master -> master Branch master set up to track remote branch master from origin.

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

8. Go to your BitbucketStationLocations repository on Bitbucket.

If you click Commits in the sidebar, you'll see your commit in the repository. Bitbucket combines all the things you just did into that 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).
If you click Source in the sidebar, you'll see your file in the repository, the locations.txt file you just added.

Next

Additional Help