Git Basics

For anyone new to git, I thought I’d share some tips.

This tutorial assumes you are using the terminal (Mac/Linux) or command console (Win).  You should have a basic knowledge of the terminal/console.  You should also already have git installed.  There are many ways of doing this and they are well documented elsewhere.

Here is an overview of what git is in video format… http://learn.github.com/p/intro.html

The main things you need to know are the following: git clone, git add, git commit, git pull, git branch, git status, and git push.

 

git clone

You have two options for creating a github repo, download or create one from scratch.

To download a git repository from github for the first time.

git clone <git-url>

example

git clone git@github.com:rails/rails.git

This will pull the rails user’s rails repository from the github.com.

If you want to create one from scratch, you can login to github.com and create a repo, then follow the instructions on how to create it.  If you don’t want to pay for it (I don’t pay for any of my personal repos), you can create one on github and just make it public.

You can do that by going to github.com when logged in and clicking on New Repository.  Just don’t put any sensitive data in there as it’s all public, just create new ones for testing or if you want to have a github repo for someone else to use.  You mostly won’t have to do this, but it helps to be familiar with how it works to get a general understanding.

You will then you make your changes.  Add new files, edit existing ones, what have you.

 

git status

At all times, you can run git status.  This will tell you what branch your on and if you have any edited files or if you have files that aren’t getting tracked.

Example

git status
# On branch master
# Changed but not updated:
#   (use “git add <file>…” to update what will be committed)
#   (use “git checkout — <file>…” to discard changes in working directory)
#
# modified:   public/wp-content/themes/wordpress-theme/style.css
#
# Untracked files:
#   (use “git add <file>…” to include in what will be committed)
#
# deploy/foo.js
no changes added to commit (use “git add” and/or “git commit -a”)

This says you are on the master branch, you made a change to the style.css file, and you have one file that is not being tracked by git.

 

git add

When you have made your awesome work–HTML/CSS/JS changes, a new drupal/django module/app, what have you–and want to save your work to your local repository, you need to then add your changes to the commit.  You can either add individual files or add every file.

Add specific files

git add path/to/new/file.html

Example

git add public/newfile.html

Add everything

git add .

 

git commit

Once you’ve added your changes, you need to commit them locally.

git commit -am “message about what I’m committing so other people know my commit”

This will commit all your changes to whatever branch your local repository is on.  You can commit locally as many times as you want.  Nobody will see these until you push them to the github server.  This is the best part of git really.

 

git pull

Then you need to pull changes that others may have made from github.com (origin)

git pull origin master

This is where the magic happens… this is pulling from the github repo.  It will merge in any changes that other developers might make.  You can get errors here and you may have issues that you need to fix manually, grab a developer and we can help you take a look.

 

git push

Now you need to push your changes to the github server so other developers have them.

git push origin master

This says to push to the origin your version of the master branch.

That’s it!   There are a lot of other concepts we will try to introduce you guys to, but you should at least be familiar with these.  As with a lot of programs, there are a whole lot of things you can do.

 

git checkout/branch

Git uses branching to create sub-repositories of your repository.  The default branch on a project is called master.  Whenever you see master referenced, it means the default branch of the repo.

Branching and merging

git checkout -b branch-name # create a new branch named “branch-name”
<make changes>
git commit -a
git checkout master # go back to master branch
git merge branch-name # merge changesets from branch-name
git pull origin branch-name # merge changesets from branch-name

 

Pro tip

For you *nix users, to add colors to your terminal window, you need to change your global git configuration. Since git version 1.5.5, you can change  all color options with:

git config –global color.ui “auto”

If you only want colors for certain commands (Why? That’s so weird.), run the following for the status, branching, and diff commands:

git config –global color.branch “auto”
git config –global color.status “auto”
git config –global color.diff “auto”

 

 

Finally, remember–there’s a lot of info about git. We’ve only covered the basics here–the nitty gritty to get you by. For more information, look at the following URLs.

Here’s a quick start guide that walks you through some of the concepts above https://git.wiki.kernel.org/index.php/QuickStart.

Here’s complete documentation https://git.wiki.kernel.org/index.php/GitDocumentation,

Here are some common questions https://git.wiki.kernel.org/index.php/GitFaq

It’s also important to understand the concepts of branching, so you should pay attention to that in the quickstart as well.  If you want to test it out, just create a test github repo with your account, again, it’s free if you make it publicly available, you can create one for yourself with just test code.

Comments are closed.