Getting started with git can be a daunting task. It's amazingly powerful and with that it allows you just enough
rope to hang yourself if you aren't careful. So where do you begin? You know you need to use source control because
all software developers do, right? This is a misconception, most smart software developers do use source control.
However, horror of horrors, some developers still don't embrace source control. So the fact you are looking into
source control puts you ahead of those that aren't using it. At one time I too was ignorant of source control,
and then I learned SVN, and quickly grew to despise SVN. I needed a better way to keep revisions of my
source code. Git was the answer I needed.
So what makes git so different? Why is it better? and most importantly what is git? It's a distributed source
management tool. What does this mean and how is it distributed? Essentially git operates on the idea that there will
be many copies of the repository and no one repository is the correct version. They are all correct and equal. The only unifying
factor is that they share a history of snapshots in time called "commits". This allows git to have 2 people or 100 work
on the same code base. Its extremely powerful, a by product of this distribution is the ability to branch you code and
start it down a different development line since the tool is built to cope with multiple versions of code existing in
copies of the repository, why couldn't you have a marker telling your repository to clone itself from a certain point
in time, and track that history separately? Well you can and that essentially is git in a nutshell. The ability to branch
your code and collaborate easily with other developers.
So let's get started with git.
First I highly recommend you check out this brief tutorial. It will walk you through some of the simple features of git.
Now that we are kinda familiar with git, what is this github thing? Well github is essentially a hosted network of git repositories. We can host it on github and allow the world to copy the code as well as work on the code and then collaborate on the code. Once you know git, github isn't too hard. Here is the normal workflow for git so work on mastering these commands.
creates a repository in your current directory. Use in you project directory. This only gets used on initial creation of a repo.
check to see what is staged and ready to be commit and what has changed since last commit and can be added to staging.
git add .
Adds everything in your current directory to staging the "." is a command line short cut for "this directory". Think of this as telling git to prepare to save these files.
Stash your changes on your current branch and save them for later so they can be reapplied. using git apply
git checkout -b <name of branch>
This command branches the git repo and allow you to have a separate copy of your source code to work on and if you break it you can always go back to another branch that is still "good" and no hard has been done. Note: can be replaced like so git checkout -b my-feature-branch
git commit -m "some message here"
This command will commit anything you have added to your staging to the repository. Think of this as saving those files permanently.
Those are the normal workflow commands. Typically everything gets put into a new branch and you do not work on the default branch called "master". Master is meant to be the pristine back up copy that everyone knows is the most production ready code.
Finally there are a few commands that you will need for working on github, or any other hosted service for git.
git remote -v
That command will list all of the current remote repositories you know about and can track. Typically you will just have one called "origin" this is the default one, and comes included if you cloned this repo. If you initialized the repo yourself you will need to set the origin yourself.
git remote add origin https://github.com/someone/somerepo
You can see this documented on github here https://help.github.com/articles/adding-a-remote/
git clone https://github.com/someone/somerepo
Cloning a repo copies that repository to you local machine so you can work on it. Think of this command as "download source code"
Pulls latest data from origin or any remote specified. git fetch origin. It does not pull the changes down it simply updates your repository with any branches from github so your repo contains local caches of that information.
git pull origin master
This command tells your repository to pull changes down from origin on the master branch. You can specify any remote and any branch you want.
git push origin master
This pushes your changes to a desired destination. So it will push the new commits up to your github repo.
If you want to work with a GUI instead of command line you may want to start with Source Tree
it gives you a nice interface to preform most of these commands.
It may help your familiarize yourself with how git works initially, but git is first a foremost a command line tool, most of its power is on the command line interface. So I highly recommend you learn to use the command line version.
Subscribe to Sublimecoding
Get the latest posts delivered right to your inbox