The Command Line
The command line or Terminal application is nothing to be feared. In fact, it is empowering. It is the program that says "hey this is your computer, so I am going to let you do whatever the hell you want". This can be dangerous, but for the purposes of this article, we won't be doing anything stupid that will screw up your system. We just want to have control over ... well our version control :) .
So what is git? It is a version control system, or VCS. Microsoft has TFS (Team Foundation Server), which works nicely for some things, and not so nicely for others. There is subversion, which you are free to look up, and there is Mercurial. Both subversion and Mercurial are good tools, but they don't have the reach in the marketplace that git does, and it doesn't have the backing of places to store your code that git does. With free options like GitHub and BitBucket, which is the brainchild of Atlassian, git is a front runner when choosing a VCS.
Now many people loath the command line. But just like with riding a bike, it takes a little practice, and it isn't that hard. If you are on a Mac, you have the "Terminal" application, and we will talk about getting git installed and running in this environment; if you are on Linux then you have a host of terminal emulators, which is basically the same thing as the OSX Terminal app.
OSX and git in Terminal
If you are going to become a command line commando on your Mac then you might as well have Homebrew installed. What is Homebrew you ask?
The missing package manager for OS X
I suggest you check out their site which can give you some more details, but in the same way you can download terminal applications on Linux, Homebrew enables this functionality on your OSX device as well. In order to obtain Homebrew you will need to open your terminal application and run the following.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Just copy and paste that line above and hit the Enter key. The rest is taken care for you. When this process completes you will have access to the
brew command. More importantly you will have
brew install at your fingertips as well. This install function is what we want to use to install git on our device. Don't download a clunky GUI git client, because you don't need it and it will slow you down and confuse you.
brew at our side we will install git.
In a Terminal window run
brew install git.
I know what you are thinking
Holy shit! That is so hard and I could never do that!
Well you are right. That is why you are reading this. Just kidding, I didn't know how to do that at one point either. Everyone has to learn. If you are serious about learning git then I highly suggest reading this e-book, which will explain the different features and use cases for harnessing the true power of git.
Ok so we installed git using the
brew command and we are ready to actually use it. You need a few things or git is useless.
You need a place to store your code I recommend both GitHub for code you want to share with the world, and BitBucket for code you need to keep the lid on. Sign up for accounts with both of these sites.
Second, you need code that you are going to work on either by yourself or with collaborators. Git is best served when working with others, but if you just need a place to store your source that is certainly a valid use case for working with git.
First on GitHub I need to create my repository. Go to GitHub and sign in.
Then you will want to create your repository or 'repo'.
Notice the '+' sign in the top right corner next to your avatar. This is where we can select "New Repository". So this is what we want. After clicking new repository, you will see this page:
You will want to name this repository. Whatever you like is fine. You DO NOT need to initialize this with a README file. This most likely is not going to be the repo that starts trending on GitHub so documentation isn't our primary concern at the moment.
So we have a new repository, now it is time to push our files to it.
cd path/to/files/for/repo in terminal will take us to the root directory of our project. Since we installed git, we have all the lovely git commands at our disposal. The first command to issue is the
git init command. This initializes an empty repository in our root directory.
This doesn't help us a whole lot, because our newly created git repo doesn't know where to go when we push these changes. In order for this to happen we must execute a few steps.
git remote add origin https://github.com/username/newrepo git add . git commit -m 'my commit message
The commit message should be something unique to what you have changed. Since this is a new repository, I suggest
git commit -m 'initial'.
Now that we have made our commit, we can push it to our newly created repo on GitHub. To do this we run the following:
git push -u origin master
Since I am not going over SSH and git, you will be prompted to login in order to push your files.
There you go! You have your repo set up and you are set to begin making changes to the repository. Every time you make a change you want to push just follow the same steps:
git add . git commit -m 'my new commit' git push -u origin master`
This repo can exist on your local machine and your server so if you made changes on your local machine and want to make them live on the server all you have to do is be in the root directory of the repo on your server and run the
git pull command. This updates the project and your site will reflect the changes you have made.
The Linux version of this workflow is identical minus the Homebrew portion so if you are running Linux, you have nothing to fear.
I wish you all the best, and I will answer any questions in the comment section below :)
Happy git hunting!
Subscribe to Cameron Banowsky
Get the latest posts delivered right to your inbox