Git is essential knowledge for every programmer. As you practice coding, you will begin to notice that you have a lot of projects on your computer. You get to make a lot of changes in other projects that you can’t track. Sometimes you are just worried about the safety of your work when your little brother keeps borrowing your computer. You may also need to access them remotely from other computers for various reasons. Git is here for you.
What Are Git and GitHub?
Git is a free and open-source distributed version control system that allows you to track the progress of your development work. You can save versions of your work and get back to it if you need to. GitHub, on the other hand, is an online git repository hosting service that allows you to store your projects online.
I need you to picture a baker making bread, an analogy I have created to help you understand Git. He makes the yeast dough (workspace), then takes it out in the sun for a few minutes to let it rise (index/staging area) and finally takes it to the oven (remote repository/GitHub). Each process is essential. It is almost the same when working with Git. There is your workspace, the index/staging area, and the remote repository and I need you to keep this in mind just in case you get lost since you will be working from the terminal the entire time.
Install Git and Create a GitHub Account
Get your Git downloads and then sign up for the GitHub account if you don’t have one. The best and most efficient way to learn Git is using the terminal or bash or cmd depending on what seems familiar to you. So the first thing you need to do after installation is run the following command in your project folder:
This initializes a Git repository in the root folder of your project before or after putting in your work. When you show hidden files, you will discover a new .git folder in your project.
Git will start noticing the changes you make in your files within the project. This will happen for the new files created and modifications done on existing ones, but not for the committed changes. Just hang in there we will talk about commit in a few. To see the changes you have made, you need to run the following command:
This will show you the files that you have changed and are ready for the next step. You have been working from your workspace and now you are moving to the index.
Add Your Changes to the Index/Staging Environment
This is done using the following command:
You can either put one file by adding the file name after the command or you could add everything by running
git add .notice the dot which represents “all“. Your files are now in the staging area waiting for you to commit them.
Creating a Commit for Your Staged Files
You are doing well so far. This is done using the command
git commit which is incomplete at this point for explanations first. This command captures a snapshot of the project’s currently staged changes. Committed changes are saved changes and therefore considered “safe” versions of the project. Following this command is usually a message defining the changes for example: “initial commit” or “home component text fix“. Use the precise and significant description of the changes made for future reference. Time to create your first commit!
git commit -m "first commit"
To avoid confusion always name the repository after the name of your project. Run the following command to add your remote repository to your local one:
git remote add origin https://github.com/wachiya/popcorn-game.git
The base branch, one you have been working on is called master. Time to push our project. Run the following command:
git push -u origin master
Your project is now on GitHub and it will look like this:
Ignore the security alert, we will talk about it much later. This pretty cool, right? Now that you know how to push your projects to GitHub, it is time to learn how to get it back to your local repository.
Cloning a GitHub Project
As the name suggests, this is acquiring a copy of a GitHub project. Might come in handy when you lose your project or when you need to work on it from a different computer other than your own and probably when you are working with other guys on the same project and each one of you needs a copy of the repository on their computers.
When you open a project on GitHub, it will look like the screenshot above with a big green clone or download button. Click it and it will open up a dropdown with a URL. Copy that URL and head back to your terminal and into your target preferred directory. Run the
git clone command attaching the project link to it like in the example below.
git clone https://github.com/wachiya/TO-DO.git
Well done. Now you have a local repository of the project on your computer. What can I say? Ain’t the magic beautiful? Definitely This is the most basic thing about Git and you can try it several times until you get the hang of it.
There is so much more to learn about Git especially when teaming up is involved. In our next tutorial, we will learn about branches, upstream, merging, squashing commits and other useful stuff that will help you track your progress on projects with Git. If you find this helpful, let me know and always remember to ask questions when you get stuck.