CSE-300 - Git and Bitbucket User Manual
CSE-300 - Git and Bitbucket User Manual
org
using Git
by Phong Cao (ID: 108512924)
Contents at a Glance
Contents at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1. Create a Bitbucket Academic Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2. Download and Install Git for Windows and/or Mac OSX . . . . . . . . . . . . . . . . . . . . . . . 2
3. First-time set-up for Git. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
4. Generate SSH Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5. Make Git Bash remember SSH Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6. Add SSH Key to Bitbucket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
7. Fork a project to your Bitbucket account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
8. Subscribe to a Bitbucket repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
9. Grant fork repository access to team members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
10. Clone a fork to local computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
11. Update, Add and Delete Remotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
12. Git Commit & Git Push Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
13. Git branch commands, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
14. Update (Synchronize) local fork with the original repository. . . . . . . . . . . . . . . . . . . 30
15. Create a Pull Request. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
16. (For Administrators) Merge the Pull Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
17. References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.2. By default, Git lets you type in your commit message using the systems commandline. However, you can set Git to use a different editor for your commit message:
$ git config --global core.editor <Your Text Editor of Choice>
3.3. If you want to check all of your Git settings, issue the following command:
$ git config --list
Or the following command if you just want to check for a particular setting:
$ git config <Settings Name>
4.1.c. Now that you have ssh installed, enter the following commands to check if you
already have an SSH key:
$ ls -a ~/.ssh
If you have not used SSH on Git Bash or Terminal you will see something like this:
If you have already had a default identity, you will see two id_* files (Skip immediately to
Step 5):
4.1.d. If you havent yet had the SSH Key files, issue the following command to create
them:
$ ssh-keygen
If it asks Enter file in which to save the key: and Enter passphrase: and Enter same
passphrase again: just leave them blank and press <Enter>. The final output should
look like this:
After this, issue the command in Part (c) above to make sure that the SSH Key has been
generated:
$ ls -a ~/.ssh
which should show the following output:
6.2. In the Account Settings page click on SSH keys on the left sidebar, which takes
you to the SSH Keys page. Now click on Add Key to add your SSH Key to Bitbucket:
6.3. A dialog will appear letting you add the key. Simply enter a name for the key in the
Label textbox and paste the whole content of ~/.ssh/id_rsa.pub file into the Key
textarea (the extension *.pub indicates that this file contains SSH Key for registering on
public sites & repos like Bitbucket. The id_rsa file on the other hand only responsible for
logging-in when you do a Git push):
6.4. Finally, click Add to add the SSH Key into your Bitbucket account.
7.2. Now click on the URL for <Project_Name> project, which would lead you to the
<Project_Name> project main page:
7.3. On the <Project_Name> project homepage, click on Expand Sidebar at the bottom
to expand the Bitbucket Sidebar:
7.4. On the expanded Bitbucket Sidebar youll see an option called Fork. Click on that
Fork link will lead you to the Fork settings page where you can set options for your
fork:
7.5. After the Fork settings page is displayed, enter the information as below and click
Fork Repository to fork the original <Project_Name> repo to your Bitbucket account:
10
7.6. After this step, go back to your homepage. You should see a project called
<your_Bitbucket_username>/<Project_Name> under your Bitbucket homepage. Click
on that project link to get to the Overview page of your <Project_Name> fork:
7.7. When youre at the Overview page of your <Project_Name> fork, you will see a line
called Fork of which indicates the original repository that this <Project_Name> fork
belongs to, and on the top-right of the screen you will see an SSH link that you will (later)
use to clone the fork to your local computer and update it as changes are made to the
original repo:
7.8. Now as youre done with the forking of <Project_Name> to your Bitbucket account,
copy the SSH link (shown in the above screenshot) and move on to the next step!
11
8.1. From the top Bitbucket navigation bar, go to the Bitbucket page for <Team_Name>
Team and then <Project_Name> project repository:
8.2. When youre at the Bitbucket page of the <Project_Name> repository, click on the
Down arrow next to the Watch button on the top right of the project Bitbucket page.
Check all the checkboxes so that you can watch for all the changes (Wiki, Push, Pull,
etc.) of the project:
12
8.3. Now go to your (fork) <Project_Name> repository on Bitbucket and repeat the 2
steps above. This is very important because it lets you know in case there is any team
member that forks your <Project_Name> fork repository.
9.2. On your <Project_Name> fork repository Overview page, expand the left sidebar.
Click on the item called Settings to access the settings of your <Project_Name> fork
repository:
13
9.4. When youre on the Access Management page, click on the drop-down menu in the
Groups section and choose Administrators (<team_name>:administrators) then click
Add. Do the same for Developers (<team_name>:developers) group in the drop-down
menu. The final result is in the second screenshot following:
14
15
10.2. After the cloning process is finished, you can check to see if the Git remote repo of
your local <Project_Name> match with the SSH link of the <Project_Name> fork on your
Bitbucket account by cd-ing into the projects folder (which means getting inside the
project folder using the command-line) and issuing the following command:
$ git remote -v
10.3. So in the above screenshot, I got my remote addresses correctly. The remotes, by
default, are called origin and their addresses should match the SSH addresses of your
<Project_Name> fork.
As you can see, after the URL of each remote there is a parentheses indicates what
kind of remote it is. In this case, (fetch) remote means that when you do a Git pull and
a Git fetch (youll see it later), the source code will be fetched from this URL to your
local computer. When you do a Git push (which means uploading your code to your
Bitbucket <Project_Name> fork), the (push) remotes URL will be where your code is
gonna get uploaded.
16
As you can see in the above screenshot, I changed the URL of the origin remote from
[email protected]:phongvcao/<Project_Name>.git to phongvcao.com
11.2. In order to update the Name of a Git Remote, issue the following command:
$ git remote rename <old_remote_name> <new_remote_name>
As you can see in the above screenshot, I changed my origin remotes name from
origin to phongvcao.
17
As you can see in the above screenshot, I added a remote named origin
([email protected]:phongvcao/<Project_Name>.git) into my <Project_Name>s Git repo.
As you can see in the above screenshot, I removed the remote named phongvcao
while keeping the origin remote.
18
12.2. Remove a file (or a folder and all of its sub-folder) from a Git commit (without
deleting it from the local filesystem):
$ git rm -rf --cached <path/to/file/or/folder>
As you can see in the above screenshot, the file LICENSE was removed from the Git
commit record, yet still existing in the local file system.
12.3. Remove a file (or a folder and all of its sub-folder) from a Git commit (and also
delete it from the local filesystem):
$ git rm -rf <path/to/file/or/folder>
19
As you can see from the above screenshot, my deletion of the LICENSE file was
unsuccessful because the LICENSE file was deleted from the Git commit record in the
previous step.
12.4. Add all files in the current project folder (or sub-folder, whatever folder your
command-line client is currently at) to a Git commit (but still keep the deleted files):
$ git add *
12.5. Add all files in the current project folder (or sub-folder, whatever folder your
command-line client is currently at) to a Git commit (but record the deleted files):
$ git add -A
20
The commit message typically contains the list of all of the changes you made in your
project and what is still left to do.
To enter a newline like in the above screenshot, you have to press <Shift><Enter>.
The Strings highlighted in Yellow are the Hash Codes of the commits.
The Git commits listed by git log are sorted in reversed chronological order, with the
latest and most recent Git commit on top and the oldest one at the bottom.
12.9. View the current Git commit that you are at:
21
$ git log -n 1
22
As you can see in the above screenshot, I switched from my current Git commit which
is 051f1d to the first initial Git commit which is afd3f3.
23
Note: Before issuing this command, make sure that the core.editor variable of your Git
global settings has been set to your favorite text editor (see Step 3.2 above) by issuing
the following command:
$ git config core.editor
24
If your Git commit global editor is not yet set, you can set it using the following
command (mentioned in Step 3.2):
$ git config --global core.editor <Your Text Editor of Choice>
25
As you can see in the above screenshot, the new_feature1 branch has been
successfully created.
As a convention when you create a new branch please indicates your Bitbucket
username in the name of the branch, separated by underscore _ so we know who is
mainly responsible for working on this particular branch.
Also note that when you create a new Git branch, your Git HEAD pointer is still pointing
to the branch youre currently at. You have to explicitly issue a Git switch (Git checkout)
command to switch to the new Git branch you just created.
26
As you can see in the above screenshot, the * character first highlighted the master
branch. Then after the command is issue the * character highlighted the new_feature1
branch.
As you can see in the above screenshot, the * character first highlighted the
new_feature1 branch. Then after the command is issue the * character highlighted the
newly-created new_feature2 branch.
27
As you can see in the above screenshot, I first checkout the master branch which is
more stable than new_feature1. After that, I merge the master branch with the
new_feature1 branch.
13.6. List all Git branches that WERE merged to your current branch:
$ git branch --merged
13.7. List all Git branches that WERE NOT merged to your current branch:
$ git branch --no-merged
28
After this command is executed, the branch <branch_name> is deleted from the local
repository on your computer. However, if youve already pushed this <branch_name> to
Bitbucket, the branch <branch_name> still exists online on Bitbucket.
29
As you can see in the preceding section, the git branch -d command only deletes the
local <branch_name> branch on your computer. When you do a Git commit again and
issue a Git push, the remote <branch_name> branch hosted on Bitbucket will not get
deleted. To also delete the remote <branch_name> branch, issue the following
command:
$ git push --delete <branch_name>
30
As you can see in the above screenshot, I name the official, **original** <Project_Name>
remote main (so basically I replaced <a_name_for_<Project_Name>_original_remote>
with main and <original_<Project_Name>_remote_url> with
[email protected]:<Team_Name>/<Project_Name>.git which is the URL to the official,
**original** <Project_Name> remote on Bitbucket.
After this, you can do a $ git push origin master normally to push your newly-updated
local <Project_Name> fork to the <Project_Name> fork repository on your Bitbucket
account.
15.2. After getting into your <Project_Name> fork repository on Bitbucket, expand the
Bitbuckets left Sidebar (see Step 7.2). Click on the option called Create a Pull
Request, which will lead you to the Create Pull Request page:
15.3. On the Create a Pull Request page, make sure that the Git branches and Git
repositories information are correct (meaning they are what you want to merge):
31
When youre done reviewing, click on Create pull request button to start the Pulling
process from the team members <Project_Name> fork repository to the official,
**original** <Project_Name> repository.
32
16.2. Click on a Pull Request. Edit anything if needed, and when youre done please
click on Approve and then Merge to start the Merging process:
16.3. Now youll be presented a Merge dialog. Just click on Merge to finish the Merging
process:
16.4. Youre now finished! The team members <Project_Name> fork repository has
been successfully merged to the official, **original** <Project_Name> repo on Bitbucket.
You should now notify other team members about the Merge so they know to update
their local <Project_Name> fork repositories accordingly (see Step 14):
33
34
17. References
1. "Bitbucket Documentation." Atlassian Documentation. Atlanssian, 9 Jan. 2014. Web.
31 Oct. 2014. <https://confluence.atlassian.com/display/BITBUCKET/Bitbucket
Documentation Home>.
2. Chacon, Scott. Pro Git. 1st ed. Apress, 2009. 1-288. Print.
35