How to use Git and BitBucket (and maybe TortoiseGit)

This tutorial is not finished as of yet.

Note: if you're a web developer you might be interested in registering to become a ProgClub member. ProgClub is a free international club for computer programmers and we run some mailing lists you might like to hang out on to chat about software development, life, etc.



1. What is this tutorial about?

This tutorial explains how to install and use Git and TortoiseGit, and how to use BitBucket to store source code 'off-site'.

This tutorial is written with a particular audience in mind: my fellow, aspiring developers who have not used Git before or would like to revise in it, and whom I'll be working with on a school project. Since the code used in said project should not be shared with anyone (that isn't in the team), and even though I managed to have my code openly lying around on the Internet whilst I did my past two programming assignments, I'd rather not push (Hah! Pun!) my luck. Anyway, since GitHub doesn't provide any private repos for free nowadays, I'll be showing how to use BitBucket who still do. :)

The only 'downfall' with BitBucket's private repos is that only five developers can work on the same project [12]. But I have a feeling we won't go over the limit. Hopefully.

I'm also assuming these people use Windows when it comes to installing Git and TortoiseGit. But even if you aren't using Windows, it's okay; we won't be using TortoiseGit much. It's time to remember how to use the command-line interface, Bash, which will come with the Git installation if you haven't gotten it on your system.

2. What's with the blue or bold words?

If a word's in blue it means I'm going to or have explained what it is. If it's bold, uh, I'm just trying to wake you up from the monotony of plain text. :)

3. What the heck is TortoiseGit?

It's a user interface; in short, it lets you click on buttons and commands and lets you see things via text, and windows.

It's also only available on Windows systems. Don't worry though, it's the only tool I'm going to mention here that's strictly for Windows. And you can survive without it.

tortoisegit example of git log

Really nice to look at when you're lacking sleep and cannot survive walls of text, I guess.

4. What the heck is Bash?

As mentioned above, it's a command-line interface (CLI). In short, it lets you type commands and lets you choose used commands via log history and lets you see text, text and more text.

It's also more robust than TortoiseGit. ...Huh, maybe their names are too telling.

bash example of git log

Do not underestimate this minimalist method. It involves less clicking. Especially when you're using a mousepad.

5. What the heck is Git?

No, we are not referencing Ron Weasley and his fondness of the word here.

Maybe [1].

Git is a version control system (VCS). Which means you can:

  • share your code with your collaborators with just a few commands, instead of having to email them every time
  • revert back to your last working code with ease
  • back up your code with each commit (and comment of what you did since the last back up) instead of each archive with tools such as Winzip, Winrar, and 7-zip (and no quickly accessible comments at all!)

Git is a distributed VCS. You might have heard of another VCS called Subversion, which is a centralised VCS. Some of the advantages of a distributed VCS are listed here. You can:

  • work offline when committing your code (imagine having to archive/backup your code only when you have Internet connection!)
  • merge with your collaborators' code, which doesn't involve a lot of Ctrl+C/Ctrl+P at your end
  • add a really tricky, new feature on top of working code. And if it doesn't work out you can easily kill the code related to the tricky feature, leaving the working code unscathed

6. Wait wait wait. What the heck is a repo?

Repo is short for repository.

And, unlike some computer jargon *coughtoastcough*, it is exactly what it means: 'a place, room, or container where something is deposited or stored' [10].

To make a repo, you'll need to use a Git command. More on that later.

So are you ready to version and share your repo with others easily? Then git ready [2] to install! Hehe.

References


7. Installing Git (for Windows)

Go to the downloads section of the msysgit site and download the latest full installer. At the time of writing, Git-1.7.11-preview20120710 was the latest version.

Choose the default options ...

git installation phase 1 git installation phase 2 git installation phase 3

... until you reach Select Components.

git installation phase 4

Under Context menu entries, select Git Bash Here and Git GUI Here. We won't be using Git GUI in this tutorial, but you might as well install it just in case you decide you prefer it over Bash and TortoiseGit after a bit of mucking around. :)

You may keep In the Quick Launch and On the Desktop selected if you want. I've unchecked mine 'cause I do not need those additional icons. Nor will you need to use them in this tutorial.

I've also kept the Use a TrueType font in all console windows (not only for Git Bash) unchecked, which is not shown in the above screenshot, by default.

git installation phase 5 git installation phase 6

Here's the CLI, Bash, we were talking about! Let's choose the safest option for Windows, which is selected by default.

git installation phase 7

Choose OpenSSH, which is selected by default. TortoisePlink has some problems in terms of performance [3] at Bitbucket, and GitHub only supports OpenSSH [4].

git installation phase 8

Choose Checkout Windows-style, commit Unix-style line endings, which is selected by default. You can later reconfigure how line endings are treated by Git (for each existing repo or future ones).

git installation phase 9 git installation phase 10

And you're done!

8. What the heck did Git mean by line endings?

Remember how you would have to use \r\n or \n in a Java String in order to get a newline? Those are line endings.

Unix and Mac OS X and later use LF (line feed) which means for every newline in a file, it adds a hidden \n. Mac OS 9 and earlier use CR (carriage return), which means it uses \r. Windows 7 and earlier use CRLF (carriage return line feed), which means it uses \r\n [5]. However, it looks like Windows is catching up with the others in regard to line endings: the developer/pre-beta version of Windows 8 uses LF [6].

Trivia: 'CR is ASCII code 13, LF is ASCII code 10. Thus, CRLF is two bytes, while LF is one' [7]. When it comes to creating websites this extra byte can stack up (try to count how many newlines you do for code readability), to the point where the webpage loads noticably slower--especially during the times of when human beings first heard dubstep.

So, our current configuration of dealing with line endings (Checkout Windows-style, commit Unix-style line endings) will convert all the line endings of the code you back up (commit) to LF. And when you get your code from your fellow developers (check out), all the line endings of the code will be converted to CRLF.

You might go, 'so what'? Well, if you find it difficult to merge files together, it might be because the line endings of those files are different. For now, since I'm assuming we're all using Windows here, our current configuration is fine. [This might be why a lot of developers who use Git work on Unix or Mac systems.]

9. I'm ONE of those developers who work on a Unix or Mac system

Ah. If you're using a Mac, I hope it's Mac OS X. Because if it's an earlier version ... you'll need to configure your editor/IDE to use LF. For existing files that are in CR you can convert them via the CLI [9]. Here's the link to a tutorial that does both (the editor being gedit).

For others: well, to make sure you've chosen the recommended line endings when you installed Git, open a Terminal and type the command that's in white:

Key720@ProgClub ~'Key720' is my username; 'ProgClub' is my computer name. Yours will be obviously different$ git config --global core.autocrlf input

This will ensure that all future repos are set to the option where Git does not convert the line endings of the code you get from your fellow developers (checking out). But when you are backing it up (committing), any CRLF will be converted to LF.

If you have an existing repo, open a Terminal and make sure the shown, current path you are on is correct; to configure the line endings of the repo, type the command that's in white:

Key720@ProgClub /d/BitBucket/projects/tutorialAgain, make sure you're in the correct directory of the repo!$ git config core.autocrlf input

Cannot get to the directory you want because you've forgotten the change directory command? Worry not, here's an example of how to get to the directory shown above.

Key720@ProgClub ~
$ cd /d/BitBucket/projects/tutorialYou can press Tab for Bash to autocomplete your inputKey720@ProgClub /d/BitBucket/projects/tutorial
$
cursor

Then, all committed files should be in LF, making Git happy. :)

10. What the heck is OpenSSH?

'OpenSSH is a FREE version of the SSH connectivity tools that technical users of the Internet rely on. Users of telnet, rlogin, and ftp may not realize that their password is transmitted across the Internet unencrypted, but it is. OpenSSH encrypts all traffic (including passwords) to effectively eliminate eavesdropping, connection hijacking, and other attacks.' [8]

11. Setting up SSH keys

Remember Web Systems? Remember public key/asymmetric cryptography? If you skipped the subject or you don't remember:

'In public key cryptography, a public and private key are created simultaneously using the same algorithm (a popular one is known as RSA) by a certificate authority (CA). The private key is given only to the requesting party and the public key is made publicly available (as part of a digital certificate) in a directory that all parties can access. The private key is never shared with anyone or sent across the Internet. You use the private key to decrypt text that has been encrypted with your public key by someone else (who can find out what your public key is from a public directory).' [11]

With Git, we'll create a pair of SSH keys via Bash, keep the private key on our system, and share our public key on our BitBucket account. Speaking of BitBucket, if you haven't created an account yet I suggest you go and do it right now. :)

Now go to your start menu and search for the Git folder. Git Bash should be in there. Open it.

GitHub has a great tutorial [4] in how to create SSH keys, but since it's for GitHub, I will copy what they say with some changes so we can use BitBucket.

1. Let's check if you have a pair of SSH keys. Type the command in white.

Key720@ProgClub ~'Key720' is my username; 'ProgClub' is my computer name. Yours will be obviously different$ cd ~/.sshChange directory to your home's .ssh directory

If you get a No such file or directory message skip to step 3.

2. Looks like you have an existing pair of SSH keys. Let's back them up and make a new pair.

Key720@ProgClub ~/.ssh
$ lsList the files and directories in this directoryid_rsa  id_rsa.pub  known_hosts
$ mkdir key_backupMake a directory called key_backup in the current directory$ cp id_rsa* key_backupCopy the files starting with id_rsa to the key_backup directory$ rm id_rsa*Remove the files starting with id_rsa$ lsList, again, the files and directories in this directorykey_backup  known_hosts

3. Make a pair of SSH keys.

Key720@ProgClub ~/.ssh
$ ssh-keygen -t rsa -C "insert.email@address.here"Creates a pair of SSH keys by using your email, preferably the one you used for BitBucket$ Generating public/private rsa key pair.
Enter file in which to save the key (/c/UsersWhere Key720 is should be your username/Key720/.ssh/id_rsa): 
cursor

Press Enter to save the keys in the default directory.

They will then ask you for a passphrase. Wait, a passphrase you say? Yes, a passphrase. They are more secure than passwords [13].

Enter passphrase (empty for no passphrase):
cursor

Now enter it! (Note, Bash will not show any form of notice that you are indeed typing something. Yes, your friendly asterisks are gone.)

Enter same passphrase again:
cursor

And enter it again!

Bash will then show you something similar to this:

Your identification has been saved in /Users/
Key720/.ssh/id_rsa.
Your public key has been saved in /Users/Key720
/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db 
insert.email@address.here

4. Now let's share your public key on BitBucket.

Hover over your username and choose Account. Scroll down till you see SSH keys.

Now go and find that id_rsa.pub file. Which should be in, by default, C:\Users\Your_User_Name\.ssh\ - the .ssh folder might be hidden, so make sure your file manager (Windows Explorer) is configured where hidden files can be seen.

Open id_rsa.pub with a text editor (such as Notepad or gedit), right click the text and choose Select All and then Copy.

Go back to your BitBucket page and paste it in the box under SSH keys. Click the Add key button.

You're done! :D

12. Setting up your information

GitHub sums it up perfectly:

'Git tracks who makes each commit by checking the user’s name and email. In addition, this information associates your commits with your [BitBucket] account. To set these, enter the [commands] below, replacing the name and email with your own. The name should be your actual name, not your [BitBucket] username.' [4]

$ git config --global user.name "InitialName Surname"
$ git config --global user.email "insert.email@address.here"

13. Optional: Installing TortoiseGit (Windows only application)

Go to the downloads section of the tortoisegit site and download the latest appropriate bit installer. My Windows is 64-bit so at the time of writing, I chose TortoiseGit 1.7.12.0 - 64-bit.

Choose the default options ...

tortoisegit installation phase 1 tortoisegit installation phase 2

... until you reach Choose SSH Client.

tortoisegit installation phase 3

Choose OpenSSH, Git default SSH Client, which is not the default option.

tortoisegit installation phase 4

Continue with the default options.

tortoisegit installation phase 5 tortoisegit installation phase 6 tortoisegit installation phase 7

Done! Well, that was easy!

References

Working with BitBucket

Table of Contents

  1. Creating a repo

14. Creating a repo

NOTE: Choose one person in the team to do this!

With that note out of the way, go to BitBucket. Hover over Repositories and choose create repository.

creating a repo on bitbucket

Let's name our repo ass_1 which is an abbreviation of assignment_1, obviously, and choose Git under Repository type. The rest of the details you can later add or change, so you can skip them for now if you want.

Click on the Create repository button.

*To be continued* I'm on the quest of killing assignments and levelling up motivation so I can finish this tutorial.

Back to Top