Two weeks ago, we started Beginner’s Monday with introduction to Git and we appreciate your patience for the two weeks as you waited for the next article. For those that pinged us last Monday asking when the next article will be, here it is finally
In the last article we covered what version control is and we went through installation of git. In case you didn’t read, you can catch it here. Today, we’ll create our repositories (both online and local and see how to have changes on our files reflect on both)
Creating a local repository.
After installing git, create a folder on your desktop and call it repo.
Open your terminal and navigate to the folder. It should look like this:
When you get there type this: git init and press Enter.
This initializes an empty git repository and you will see that it is named master. Executing the command git init creates a .git subdirectory that contains metadata about the repository. You can view it by viewing hidden files in the folder.
What is a repository and what is master?
A repository is where everything you are saving will be stored You can have an empty repository as we have just done and add files later, or you can create a repository in a folder already containing files. You will still have to add these files to the repository, as I will show you later.
Local repository is also called working directory, in case you come across that term elsewhere
When you create the repository, you will see master initialized in square brackets like this:
This represents a branch. You can look at a branch as a line of development. What do I mean? Let’s imagine you are working on a fancy app that automatically applies filters and uploads your selfies (with eyebrows on fleek :D) to Instagram. Currently, Instagram is the only platform that it supports. You decide you also want to invite Facebook to the party, but you are not sure what the repercussions that will have for your app. This is how branches help. You will create a new branch and experiment your new idea there.
When you create a new branch, you create it off the branch that has all your latest edits. You then experiment with your new edits on the new branch you created leaving your other branch secure. If your edits work out well, you can merge the two branches, if they don’t you can always delete the branch. Branches are a way for you to work on different ideas of your app without affecting others:)
With that long explanation let us head over to GitHub and create our online repository You will only need to have an Email address to open an account. There are different plans that GitHub offers, but for now you will want to go with the Free plan. GitHub automatically chooses it for you. Once you verify your Email, you can access your dashboard. Click on +New Repository to begin creating.
Our repository name is repo, so that is the name we will give it. You can give a small description if you’d like. Your repository is public, meaning anyone can be able to access it, to have it private you will have to provide other credentials. A ReadME file is important, but for now we will skip that. Click Create repository.
You should now be on a page that looks like this:
Let us go back to our local working directory and add a HTML file, name it repo.html. You can use any text editor to write this code, I am using Atom. Write this code on your file:
Go back to your terminal and run the command git status. This tells you the status of your directory, the files that you have changed/deleted/added
You should see this:
The last line says nothing added to commit but untracked files present.
What in the world is that?
When using git, you have to add files that you have changed/added and commit them to the repository. We have the html file with content in the folder, but we still need to add and commit it to the repository I know this sounds difficult to understand but after a while you will begin to get the hang of things and completely understand the workflow.
Let’s head over to the terminal and run this command, git add repo.html An alternative command is git add . which adds everything in the directory. Nothing shows up on your terminal, but if you run git status, you should see this:
It tells you that there are changes to be committed. Let us now commit them. Every commit that you make needs to have a message that describe what that commit is about. This helps with tracking. Write this on your terminal: git commit -m “Adds html file” This commits the change with that message.
Your terminal now tells you 1 file changed together with other information Congratulations if you have made it this far, you have learned a lot. Let us now have these changes reflect on our online repository and we can call it a day
Note: Before we move further, let us set our credentials. On your terminal run the following:
git config user.email and set the email to the email you used to open your account. i.e git config user.email “firstname.lastname@example.org”
git config user.name and set it to your username i.e git config user.name “gertiesheshe”
When you go back to you GitHub page, you will see some instructions about how to create a repository on the command line, or how to push a repository from the command line. We want to push a repository from the command line so copy the first line as it appears on your tab:
After adding your remote repository, now we want to push our committed changes to it. Run the second line now, you should see this now (ignore the git push command on my terminal, I ran into a couple of problems with my credentials due to another account I own ):
And when you check your GitHub dashboard, you will see this:
Great job beginners! See you in the next article:) If you run into any problems do ping us on Twitter and we shall follow up.