I have been working my way up the development ladder for a while now and one thing I’ve seen most developers get bucked up about is getting around pushing code to the little G (GitHub). Just in the case you don’t know what GitHub is all about, the following paragraph should put you in the right frame of mind to follow this little tutorial of mine to wrapping your head around the little G.
Disclaimer: I am still an aspiring software engineer, not a GitHub power user and also this is my first time putting out a technical article. Kindly bear this in mind as you go through this article.
So, what the heck is the little G and what does it do?
Github, or the little G as I like to call it, is a Version Control System that allows users host code and review code, manage projects and build software alongside millions of other developers.
It’s a platform that allows for social coding, allowing you and your development team, or random developers contribute to and help make your code base better, add more features for you, clean your code out and help push the boundaries of your code.
Now that we have gotten ourselves a little familiar with what the little G is and what it helps us do, I think it’s time we focus on how to get started, how to put up our code and in a later article, I will write on how to contribute to other developers’ code, so you should have that in mind too.
Step 1: Create an account
As you all know, you have to get an account before been able to enjoy the features the website has to provide. Signing up is as simple as going to the website http://github.com and clicking on the sign up link. After you have signed up, you need to fill out your profile to the maximum.
Step 2: Create a repository
Now that you have an account and have filled out your profile to the maximum, the next step to getting your code on to GitHub is to create a repository. Now, a repository is like a bucket, a container containing your code. To create a repository on GitHub, click the + sign and select ‘New Repository’, like this:
Once you have created a new repository, you can give it any name you want, but make it related to the project you are working on so you can easily get to know what the repository is all about. Also, even though you can always initialize your repository with a README file as github suggests, I would not do that, I’ve always had problems whenever I do that, so I would not check the box beside that and I would just go on to create my repository. I can always create a README file locally and add it to the repository.
Step 3: Let’s grab our link for PC integration
If you are able to see the page above, you have successfully created your github repository and the next stage is to work on integrating it with your local repository so you can push and pull between them. First thing I usually do, so I wouldn’t have to keep switching between my browser and my local environment is to first grab everything I need from the browser and what we only need here is to click on the little icon I marked in the image. What this little link does for us is to copy the link to the remote repository. We would need this, as a connection between our local repository and the remote (GitHub) repository. You can copy this somewhere, but I don’t do that, as I wouldn’t be copying anything once that is copied to my clipboard.
Step 4: Setup local repository little man!
Now, you would go into your development folder (where your working files are located). I usually put mine in my “~/Documents/Dev/” path.
Note: I would be focusing more on the Ubuntu user here but most of the commands are generally the same, although for the Windows user, you would need to get Git Bash installed to be able to run these commands. You can get Git Bash from this URL: https://git-scm.com/downloads. Now, in the folder where you would be doing all your development, create a folder for this project, go into the folder, right click and select ‘Use terminal here’ (for windows, it’s ‘Use Git Bash here’).
Once in the terminal (or Git Bash), run:
This little command initialises git in your folder and you can now successfully run git commands without wahala (problem).
Step 5: Add a little remote brother
Now that you have initialised git, you need to add a little remote brother (could be origin, or whatever name you wanna call it). To achieve that, you would need to do:
git remote add origin (remote url)
For every git code you run, it must be prefaced with git, that’s why there’s a git in the website. The git command that adds a link to your remote repository is the ‘remote add’ command. The part ‘origin’ is where you would input the name you want to use to identify the remote on your local machine, most people normally use ‘origin’ but you can make use of whatever you are comfortable with. Also, the last part of the command, the ‘(remote-url)’, is the part where you will insert the url you copied to your clipboard from the beginning of the tutorial where you created the remote repository on github. This is telling your local repository that anytime you interact with the remote ‘origin’ it should make use of that url, if the url is not correct, your remote would not work. You would enter your remote url here, without the brackets of course.
Step 6: Now let’s test out our little remote brother
Now that we have remote linked on our local repository, let’s test it out if it works. The way I used to do this is to create a sample file in the folder, mostly I like creating the README.md file, and putting a few text in it, after which I will go into my terminal (or Git Bash in the windows case), the same terminal you used to create the init and remote commands, or a new one, if you have closed that, but it’s location should be the folder you are working with or you might have errors.
Once you have created the README.md file, filled it with a little text and saved it. You can go into your terminal and do:
git add .
What this does is to add all your newly edited or created files that have not been pushed to GitHub, so you can commit them to the staging area to be pushed to your GitHub repository. Now once that is done, you can now run:
git commit -m "Your Commit Message"
To commit your added files to staging to be pushed to your GitHub repository, you would need to run the command as stated above, now inside the quotation marks, you would need to put in your commit message, a simple one liner explaining why you are making the push or the changes you made and are pushing to the GitHub repository. The first commit you are making is usually given a “Initial commit” message because it’s the first time you are making a push to that repository, some peeps don’t like that though.
Finally, to make the final push so your code can show on GitHub, you should do a:
git push origin master
This is the final command that pushes your code to GitHub, now we are already familiar with the git part of the command, all we need to know more about is the push, origin and master part. Ok, cool.
The push part is the command that uploads your code to your GitHub repository that you have already linked to using the ‘remote add’ command.
The origin part is the name you gave the remote, most peeps would give their remote any name, but origin is the most common.
Now the last part, mostly would always be master because you are the only one working on this project, although, when you start working in teams, you would have to deal with something called ‘branching’ where you create a form of another folder on the GitHub repository so your code changes does not affect other people’s own and cause a problem called ‘merge error’. So, the master part is the default branch and that is where you will be pushing, all because you are the only one working on this project up until now.