Git 101 - Quick Start

Git is the most popular version control system. But it can be quite confusing, so I'd advise you to read the other parts of this series to understand the core concepts, which allows you to use the more advanced features.

This article is just a quick and dirty guide that gets you started quickly.

General Workflow

This is a general basic workflow for using Git, each part will be explained under its own heading:

  1. Get a repository
  2. Make changes to the source code
  3. Add the changes to the stage (git add)
  4. Commit changes from the stage (git commit)

The stage acts like a shopping cart. With a shopping cart, you add multiple products and check them all out in one transaction. The stage allows you to add changes and then commit them all in one go.

If you have the project source code on multiple machines (e.g. on your local machine as well as a production server), it's good to create a central remote repository everyone will work from. You'd then:

  1. Push the changes you've made to the remote respository (git push)
  2. Pull the changes made by others from the remote repository (git pull)

Getting a Repository

If you want to create a new repository, run git init in your project's root directory.

$ git init

If you have an existing repository, use git clone:

$ git clone /path/to/repository

Or if the existing repository is in a remote location (e.g. BitBucket or GitHub):

$ git clone username@host:/path/to/repository

For example,

$ git clone git@bitbucket.org:d4nyll/test.git # Using SSH
$ git clone https://d4nyll@bitbucket.org/d4nyll/test.git # Using HTTPS

Making Changes

Now you have a repository, you need to store your files in there so it can be version-controlled. Run git status to see which files have changed since your last commit.

$ git status

You'll get something like this:

On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   css/screen.css
    modified:   sass/components/_content.scss
    modified:   sass/components/_footer.scss
    modified:   sass/components/_topbar.scss
    modified:   sass/lib/responsive-nav/_responsive-nav.scss

no changes added to commit (use "git add" and/or "git commit -a")

This shows that you have made changes to 5 files since your last commit; or if it's a new repository, everything will be untracked.

Adding Changes

You can now do git add <filename> to add the modified/untracked files to the stage.

git add css/screen.css

Do this for all the files you want to be staged. Alternatively, you can run git add -A which adds all the modified/untracked files in one go.

Usually you'd stage changes that are relevant to each other - so changes relating to content updates should be in a separate commit to ones for adding a new responsive menu.

Committing Changes

Run git status again to check that you have staged everything you want to. Once happy, run git commit.

$ git commit -m "Your commit message"

The -m flag allows you to write a message for the commit, which helps others using the repository to quickly understand what changes you've made. If it's your first commit, people usually just write "Initial commit" as the message.

Reviewing Changes

Now if you run git log, you can see all the previous commits, the author and the time the commit was made.

$ git log

Pushing

If you have a remote repository from running git clone, you can push the changes you've made to the remote repository by running git push.

$ git push

For your first commit, use git push -u origin --all instead

If you started a new repository but now wants to push to a remote repository, create the remote repository first, get the repository's address, and run:

$ git remote add origin <address>

For example:

$ git remote add origin git@bitbucket.org:d4nyll/test.git

And now run:

$ git push -u origin --all

Pulling

Now you've made the changes locally and pushed to the remote repository. If you're working on different machines and want to start from the changes you last made, running git pull will pull those changes down.

$ git pull

If it helps, you can think of git pull the same way as git clone - both are getting the latest version from the remote repository; but you'd only use git clone on the first run.

All together

Here's an example putting everything together. We're working on a local environment but want to push changes to production.

Initial Set Up

Development

$ git init
$ git add -A
$ git commit -m "What I changed"
$ git remote add origin git@bitbucket.org:d4nyll/test.git
$ git push -u origin --all

Production

$ git clone git@bitbucket.org:d4nyll/test.git
Normal Use

Development

$ git add -A
$ git commit -m "What I changed"
$ git push

Production

$ git pull

Quick Help

If you get stuck, try

$ git help

If you get stuck on a particular command, run

$ git help <command>
comments powered by Disqus