Copy your Git repository and add files

このページの内容

  1. Create a Git repository
  2. Copy your Git repository and add files
  3. Pull changes from your Git repository on Bitbucket Cloud
  4. Use a Git branch to merge a file

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.

 

 

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 where to find it at The Command Line Crash Course.

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.

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

    $ cd ~/repos
  4. From Bitbucket, go to your BitbucketStationLocations repository.
  5. Click the + icon in the global sidebar and select Clone this repository.
    Bitbucket displays a pop-up clone 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 highlighted clone command.
  7. From your terminal window, paste the command you copied from Bitbucket and press Return.
  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 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.

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

    $ cd ~/repos
    $ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git
    Cloning into 'bitbucketstationlocations'...
    Password
    warning: You appear to have cloned an empty repository.

    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 your bitbucketstationlocations directory 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'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:

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

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

    $ 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 that you are pushing to the master branch (the branch on Bitbucket) on origin (the Bitbucket server).
    You should see something similar to the following response:

    $ 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 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 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 that you have a single source file in your repository, the locations.txt file you just added.

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

次へ

 

最終更新日 2017 年 7 月 19 日

この翻訳に満足しましたか?

はい
いいえ
この記事についてのフィードバックを送信する

お探しの情報が見つかりませんか?

コミュニティへの質問

Powered by Confluence and Scroll Viewport.