Nathan Czachur   —   2 July 2013   —   Code & Drupal

When starting at Access I had no experience of 'version control', unlike everyone else, so I had to learn the basics quickly. This article shows my quick-start guide to setting up Git and how easy it is to use on a daily basis.

First, some basic concepts need to be explained:

For a project, your files are stored in a repository (or repo). You can have a repo on your local machine, on the machine of a team member, or on a hosted service such as GitHub or BitBucket (remote repo).

Git tracks the changes you make to your project files (edited, moved, renamed, deleted), this allows you to review what you can done before you commit the changes to your local repo, and then push the files onto your remote repo. Using the remote repo is not required if you work alone but is invaluable for team projects and as an off-site backup.

So, that's the basic concept, now follow the steps below to create a repo and connect it to a BitBucket account to start controlling your source code!

First download and install the latest version of Git, then open up your terminal.

  1. Set a name and email for your local machine:

    1
    2
    
    git config user.name "FIRST_NAME LAST_NAME"
    git config user.email "[email protected]"

  2. Make a folder on your local machine to hold your project source and 'change directory' there:

    1
    2
    
    mkdir my_project
    cd my_project

  3. In your web browser, create a free BitBucket account and create a new repo for your project 'my_project'.

  4. Back in terminal, initialise 'Git' on your project folder to begin tracking changes: git init

  5. Next, add the your BitBucket repo as a 'remote origin' to allow you to push changes up to BitBucket: git remote add origin http://gituser@bitbucket.org/my_username/my_project.git

  6. Begin to code your project as normal. At any point you can access a list of modified files with this command: git status

  7. Changes to your project must be 'staged' using the git add command before being committed: git add . Using '.' will add all files in the local directory. Alternatively you can add single folders or files using the following:

    1
    2
    
    git add images/*
    git add images/logo.jpg

    If you stage a file by mistake you can reset it: git reset images/logo.jpg

  8. Commit files you have staged to your local repo using: git commit -m "Added file, fixed #2" The '-m' sets a message to accompany your commit to describe the changes you made. Try to be a specific as possible and where relevant include issue numbers from a bug reporting system (the BitBucket website has one built in).

  9. When you're ready to share your commits, you can push them up to your BitBucket remote repo: git push -u origin master

    The '-u' stores your branch names so future pushes can be completed using: git push

    You will be prompted for a password associated with your BitBucket account. It appears as if you're not typing, but the text is hidden for security. Now your files are on the remote repo for other team members to pull down to work on themselves.

  10. Continue making more changes to your project and when you're ready do another commit and push:

    1
    2
    3
    
    git add images/*
    git commit -m "Added photo gallery images"
    git push

When you've got the basics you may want to read about git branches. For much more detail take a look at the Git documentation.



Our Partners / Accreditations / Networks

0161 872 3455

Sign up to our newsletter