Home >Technology peripherals >It Industry >Learn Git with Bitbucket Cloud
This article was sponsored by Bitbucket. Thank you for supporting the sponsors who make SitePoint possible.
Learn the basics of Git with this space themed tutorial.
Your mission is to learn the ropes of Git by completing the tutorial and tracking down all your team’s space stations. Commands covered in this tutorial:
As our new Bitbucket space station administrator, you need to be organized. When you make files for your space station, you’ll want to keep them in one place and shareable with teammates, no matter where they are in the universe. With Bitbucket, that means adding everything to a repository. Let’s create one!
Initially, the repository you create in Bitbucket is going to be empty without any code in it. That’s okay because you will start adding some files to it soon. This Bitbucket repository will be the central repository for your files, which means that others can access that repository if you give them permission. After creating a repository, you’ll copy a version to your local system—that way you can update it from one repo, then transfer those changes to the other.
Do the following to create your repository:
The system displays the Create a new repository page. Take some time to review the dialog’s contents. With the exception of the Repository type, everything you enter on this page you can later change.
Take some time to explore the repository you have just created. You should be on the repository’s Overview page:
Click items on the Navigation menu to see what is behind each one. To view the shortcuts available to navigate these items, press the ? key on your keyboard.
When you click the Commits option on the menu bar, you find that you have no commits because you have not created any content for your repository. Your repository is private and you have not invited anyone to the repository, so the only person who can create or edit the repository’s content right now is you, the repository owner.
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.
Open a browser and a terminal window from your desktop. After opening the terminal window, do the following:
$ 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.
$ cd ~
$ mkdir repos
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.
If you experience a Windows password error:
$ cd ~/repos
$ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git Cloning into 'bitbucketspacestation'... fatal: could not read Password for 'https://emmap1@bitbucket.org': No such file or directory
At this point, your terminal window should look similar to this:
$ git config --global core.askpass
You already knew that your repository was empty right? Remember that you have added no source files to it yet.
$ cd ~/repos $ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git Cloning into 'bitbucketstationlocations'... Password warning: You appear to have cloned an empty repository.
Congratulations! You’ve cloned your repository to your local system.
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.
$ ls
$ cd ~/repos/bitbucketstationlocations/
If the command line doesn’t return anything, it means you created the file correctly!
At this point, Git is aware that you created a new file, and you’ll see something like this:
$ echo "Earth's Moon" >> locations.txt
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.
$ cd ~
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.
$ mkdir repos
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.
$ cd ~/repos
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
You should see something similar to the following response:
$ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git Cloning into 'bitbucketspacestation'... fatal: could not read Password for 'https://emmap1@bitbucket.org': No such file or directory
Your commits are now on the remote repository (origin).
You should 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.
Next on your list of space station administrator activities, you need a file with more details about your locations. Since you don’t have many locations at the moment, you are going to add them right from Bitbucket.
To add your new locations file, do the following:
A page for creating the new file opens, as shown in the following image.
$ cd ~
$ mkdir repos
You now have a new file in Bitbucket! You are taken to a page with details of the commit, where you can see the change you just made:
If you want to see a list of the commits you’ve made so far, click the Commits link on the left side.
Now we need to get that new file into your local repository. The process is pretty straight forward, basically just the reverse of the push you used to get the locations.txt file into Bitbucket.
To pull the file into your local repository, do the following:
$ cd ~
$ mkdir repos
The git pull command merges the file from your remote repository (Bitbucket) into your local repository with a single command.
Fantastic! With the addition of the two files about your space station location, you have performed the basic Git workflow (clone, add, commit, push, and pull) between Bitbucket and your local system.
Being a space station administrator comes with certain responsibilities. Sometimes you’ll need to keep information locked down, especially when mapping out new locations in the solar system. Learning branches will allow you to update your files and only share the information when you’re ready.
Branches are most powerful when you’re working on a team. You can work on your own part of a project from your own branch, pull updates from Bitbucket, and then merge all your work into the main branch when it’s ready. Our documentation includes more explanation of why you would want to use branches.
A branch represents an independent line of development for your repository. Think of it as a brand-new working directory, staging area, and project history. Before you create any new branches, you automatically start out with the main branch (called master). For a visual example, this diagram shows the master branch and the other branch with a bug fix update.
Create a branch where you can add future plans for the space station that you aren’t ready to commit. When you are ready to make those plans known to all, you can merge the changes into your Bitbucket repository and then delete the no-longer-needed branch.
It’s important to understand that branches are just pointers to commits. When you create a branch, all Git needs to do is create a new pointer—it doesn’t create a whole new set of files or folders. Before you begin, your repository looks like this:
To create a branch, do the following:
$ cd ~
$ mkdir repos
This command creates a branch but does not switch you to that branch, so your repository looks something like this:
The repository history remains unchanged. All you get is a new pointer to the current branch. To begin working on the new branch, you have to check out the branch you want to use.
$ cd ~/repos
The git checkout command works hand-in-hand with git branch. Because you are creating a branch to work on something new, every time you create a new branch (with git branch), you want to make sure to check it out (with git checkout) if you’re going to use it. Now that you’ve checked out the new branch, your Git workflow looks something like this:
$ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git Cloning into 'bitbucketspacestation'... fatal: could not read Password for 'https://emmap1@bitbucket.org': No such file or directory
$ git config --global core.askpass
Notice the On branch future-plans line? If you entered git status previously, the line was on branch master because you only had the one master branch. Before you stage or commit a change, always check this line to make sure the branch where you want to add the change is checked out.
$ cd ~/repos $ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git Cloning into 'bitbucketstationlocations'... Password warning: You appear to have cloned an empty repository.
$ ls
With this recent commit, your repository looks something like this:
Now it’s time to merge the change that you just made back into the master branch.
Your space station is growing, and it’s time for the opening ceremony of your Mars location. Now that your future plans are becoming a reality, you can merge your future-plans branch into the main branch on your local system.
Because you created only one branch and made one change, use the fast-forward branch method to merge. You can do a fast-forward merge because you have a linear path from the current branch tip to the target branch. Instead of “actually” merging the branches, all Git has to do to integrate the histories is move (i.e., “fast-forward”) the current branch tip up to the target branch tip. This effectively combines the histories, since all of the commits reachable from the target branch are now available through the current one.
This branch workflow is common for short-lived topic branches with smaller changes and are not as common for longer-running features.
To complete a fast-forward merge do the following:
$ cd ~
$ mkdir repos
$ cd ~/repos
$ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git Cloning into 'bitbucketspacestation'... fatal: could not read Password for 'https://emmap1@bitbucket.org': No such file or directory
You’ve essentially moved the pointer for the master branch forward to the current head and your repository looks something like the fast forward merge above.
$ git config --global core.askpass
When you delete future-plans, you can still access the branch from master using a commit id. For example, if you want to undo the changes added from future-plans, use the commit id you just received to go back to that branch.
$ cd ~/repos $ git clone https://emmap1@bitbucket.org/emmap1/bitbucketstationlocations.git Cloning into 'bitbucketstationlocations'... Password warning: You appear to have cloned an empty repository.
Here’s what you’ve done so far:
Next, we need to push all this work back up to Bitbucket, your remote repository.
You want to make it possible for everyone else to see the location of the new space station. To do so, you can push the current state of your local repository to Bitbucket.
This diagram shows what happens when your local repository has changes that the central repository does not have and you push those changes to Bitbucket.
Here’s how to push your change to the remote repository:
$ cd ~
You can also see that the line to the left of the commits list has a straight-forward path and shows no branches. That’s because the future-plans branch never interacted with the remote repository, only the change we created and committed.
Not sure you will be able to remember all the Git commands you just learned? No problem. Bookmark our basic Git commands page so that you can refer to it when needed.
The above is the detailed content of Learn Git with Bitbucket Cloud. For more information, please follow other related articles on the PHP Chinese website!