Copy your Git repository and add files
- Create a Git repository
- Copy your Git repository and add files
- Pull changes from your Git repository on Bitbucket Cloud
- 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.
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:
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.
Create a directory to contain your repositories.
$ mkdir repos
From the terminal, update the directory you want to work in to your new
$ cd ~/repos
- From Bitbucket, go to your BitbucketStationLocations repository.
- Click Clone.
The system 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.
- Copy the highlighted clone command.
- From your terminal window, paste the command you copied from Bitbucket and press Return.
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 click here
At this point, your terminal window should look similar to this:
$ cd ~/repos
$ git clone
Cloning into 'bitbucketstationlocations'...
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.
List the contents of your
reposdirectory and you should see your
bitbucketstationlocationsdirectory in it.
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.
Go to your terminal window and navigate to the top level of your local repository.
$ cd ~/repos/bitbucketstationlocations/
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!
Get the status of your local repository. The
git 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:
$ git status
On branch master
(use "git add <file>..." to include in what will be committed)
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.
Tell Git to track your new
locations.txtfile using the
git addcommand. Just like when you created a file, the
git addcommand doesn't return anything when you enter it correctly.
$ git add locations.txt
git 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.
Check the status of the file.
$ git status
On branch master
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 statuscommand displays the state of the working directory and the staged snapshot.
git commitcommand with a commit message, as shown on the next line. The
-mindicates 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
git committakes 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.
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
masterbranch (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://firstname.lastname@example.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).
- Go to your BitbucketStationLocations repository on Bitbucket and click the Commits item on the menu bar.
You should 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 (
- Click the Source option.
You should see that you have a single source file in your repository, the
locations.txtfile you just added.
Remember how the repository looked when you first created it? It probably looks a bit different now.
Was this helpful?
Thanks for your feedback!