Using Version Control: Git/Github
What is Github? Why and how to use it?
Github is a web based version control repository i.e its there to host your application while preserving its history of versioning. There are several version control systems out there such as Git, Subversion, Mercurial etc. GitHub is based on (you guessed it right) Git. ITS THE BEST THING SINCE SLICED BREAD! Never used it? You’ve come to the right place my friend, you’ll forever use it after this article I promise.
Step 1) Signup
To start using GitHub, you’ll have to signup. Head over to GitHub and hit that register button. Proceed through the usual hoops and be done with it.
Step 2) Create new repository
At the time of writing, there is a small + symbol in the top right corner. This may change but there is a way to create new repository. Hit it! Choose a name and a description for your repository. There are public/private repositories. A public repository means the world can search and see your code once uploaded. Private is the inverse. Choose your option. Then comes the README and LICENSE. Choose whether you want one created by GitHub or whether you already have one in your code. Hit the Create button!
You will be given instructions on how to proceed on the page. You should also get a GitHub repository address either in SSH or HTTPS format. Keep that handy, you’ll need it later down the line.
Step 3) Install Git on your machine
Whether you’re using windows or mac, process is fairly simple and straightforward. Bing or Google ‘install Git’. Head over to the Install page in the Git website and download the software. Then follow the installation process as you would. REMEMBER TO CHECK THE CLI TOOLS IF PRESENTED WITH THE OPTION.
Verify that you’re installation was successful by opening a terminal window and type in
git --version. It should show you a version number etc. If not, uninstall and follow above steps again.
Step 4) Instantiate your Git repo locally
Rightio, here comes the low level part. There are two ways to work with Git version control system. Either through a nice looking UI or with the command line (terminal). Go with a UI if you’re not command line savvy, if you’re comfortable using the command line, never go with UI. Command line gives you way more control over things. We’re going to assume that you’re command line savvy, these actions will translate to some form of button clicking if you’re a UI guy so don’t panic!
- Open your terminal window. (iTerm if you’re lucky!)
- Navigate to your project
- Instantiate Git repository
That is it. Your code is now under version control.
Step 5) Connect local repository (your machine) to remote repository (GitHub)
In order to upload your code to the GitHub repository you will have to tell your version controlled Git repository locally where the GitHub repository is. So go ahead and execute command in the terminal.
git remote add origin <GitHub repository address>
Remember what I said above about your GitHub repository address? Yeah copy that, paste it in place of the above placeholder <GitHub repository address>. Hit Enter!
Verify it worked by executing the command
git remote -v
You should see an origin with the address to your GitHub repository.
Step 6) Save your changes/new files
Before you can upload your code, you have to tell Git that you’re happy with the changes to be uploaded. Git won’t assume any change you make is worth keeping. This is a great feature and one that subversion (another version control system) lacks. You can selectively add your changes and upload them. Here is what you do.
To see all changes execute
You will see all your files and folders and whether they are ready to be committed or added. There are 2 steps to save your changes. First is add. You add any changes that you wish to save. Then there is commit. You commit your changes for which you can supply a message stating the reasoning behind the change. This way you can keep track of your changes easily for later review. Here are the two steps.
Lets assume you want to save the
web/index.php file to version control. This file runs the application. Execute
git add web/index.php
git commit -m "Index file that runs the application."
The above will save the file. You can add multiple files and then commit them all at once with one message. For example,
git add file1 file2 file3 file4 and then
git commit -m "Committing four files". Or
git add . and then
git commit -m "All files in the current directory and subdirectories".
Step 7) Upload your code to GitHub
Once you’re code is committed, you will have to upload your code to the GitHub repository we created early on. This is done via the
git push command. Go ahead and execute
git push origin master
This will push your changes to the remote repository. If you remember
origin is the repository address from one of the above steps.
master is the branch you are on.
Git has the concept of branches. You can have different branches that you work on to implement different features. Once you’re ready and happy to have it distributed you merge that branch into the main
master branch and your code is all in one place again. Saving more information for another article!
Head over to your GitHub repository page, refresh it and see your code appear! Yeah you’ve just version controlled it all. Now you can access this . code from anywhere.
Step 8) Download on another machine
To access your code on another machine and keep making changes to it, you have to download it. Here is how:
- Copy your GitHub repository address from GitHub
- Open a terminal window
- Go to a directory where you would like to download the code
git clone <GitHub project repository>
Clone will create a copy of the remote repository onto your machine. The above will create a folder which will already be version controlled and your master branch selected. Go inside and start making your changes. Once you’re ready, follow the
git commit and
git push instructions to save your changes back to GitHub!
Just so you know, you don’t have to use GitHub with Git. Git is the underlying version control strategy and GitHub is just one of the many repositories that support Git repositories. You may choose another such as bitbucket or Gitlab. Or a different version control system altogether. From experience I’ve found Git to be excellent at managing versions, but don’t be shy of others. Try them out and make your decision for yourself.