Git and Bitbucket Quickie

pixel_tapHi soon-to-be Git fans!

This guide is for people who kind of know what Git is but perhaps haven’t had much experience in using it.

The first thing to do is to install Git if you haven’t already. Everything you need to do that is over at http://git-scm.com/. If you are installing on Windows make sure you are installing it with Git Bash if you want to follow the guide below (you should be fine using the default options throughout the install), otherwise the instructions provided on the site should see you through nicely.

Now head over to bitbucket.org, sign up if you haven’t already and create a new repository to play around with. I won’t go in to to much detail about this step either as Bitbucket is nice and easy to use and will hold your hand through the process.

Next, a quick Git primer might be useful if you are a bit rusty or new to all this. Now, I don’t want anyone to think that I am an on-line course fan-boy or too lazy to write my own guide to Git from scratch but it just so happens that there is a rather fancy free interactive GIT on-line training course available at try.github.io which covers the basics really nicely.

Beautiful!

Rightio, now that you have had some time with Git online we can set a Git repository up on our local machine and link it to our remote repository on Bitbucket!

First of all we need to create a local Git repository on our machine. Each local repository (which is really just a bunch of files describing the directory’s structure) sits in the same directory as the files it manages. To add a repository:

  • Using command line (or Git Bash) to navigate to the folder you want to keep all your files in. For example you could use: ‘ cd work/project
    • If the project directory doesn’t exist yet you can use, as an example, ‘ mkdir project ‘ from the /work directory
  • Once you are inside your work/project directory you can create a repository with ‘ git init

Go team! we have a repository! Type ‘ git status ‘ to check that everything is working out so far.

Next up we need to connect with the remote version on Bitbucket. For now we can do that quickly though you will have to re-authenticate with your password every time you want to push / pull… We’ll fix that in another post though there are loads of guides out there already for the impatient.

  • Making sure you are in the directory with your new repository use:
    git remote add origin https://your_username@bitbucket.org/repository_owner_name/remote_repository_name.git ‘ (you can copy and paste this address from your Bitbucket repository page).
  • To make sure that has worked pull all the remote files to your local repository with ‘ git pull origin master

Fingers crossed you should have all the previously remote files in your local file.

Now lets make sure you can push stuff back.

  • Save a .txt file within your local folder
  • use ‘ git add -A ‘ (The -a bit is asking git to add all changes – this includes deletions and amendments, quick and handy for later)
  • Use ‘ git status ‘ to see what file changes are waiting to be committed
  • Now use ‘ git commit -am ‘A description of the commit ‘ Remember to add these -m messages to all of your commits, it makes me sad when people forget.
    • At this point Git might ask you to add some details about yourself if you haven’t provided them already, just follow the instructions given.

Your local Git repository is now up to date, we are ready to send your changes back to BitBucket!

  • First of all I always like to do a pull so that my local repository is as up-to-date as possible before doing a push – do that! ‘ git pull origin master
  • Now all we need to do is push! You may have already guessed the command… ‘ git push origin master

And there you have the basics!  Of course this covers a only a tiny fraction of everything that Git is good at but it is the first step on that long road.

Let me know if you have any problems!

MrGraeme

Leave a Reply

Your email address will not be published. Required fields are marked *