Git For Dummies (1/2)
Git is a software that is used by all the developers in the world. But what is Git and why you should learn Git.
Why Git?
Do you find yourself always going through lines of code and delete it just because what you have done is not what you wanted. Do you ever wished that you can have a checkpoint in your code where you can just go back to the checkpoint when you messed up your codes? Here is where git will help you in those situations.
What is Git?
Git is an open sourced distributed version control software system developed in 2005 by the creator of Linux OS kernel, Linus Torvalds. In a simple matter, Git is a software that keeps track the history of all the files in a folder.
Installing Git
Before starting with the Git commands let’s install Git into our computer. Head to Git website and download the installer according to your operating system. Check the version of git in the terminal to check if you have installed git correctly in your computer.
git --version
Getting Started
I will be using Linux environment in this article but all commands will be the same across different platform. I will create a folder called git with one index.js file inside.
Initializing Git
We will need to initialize Git in our folder so that git will start keep tracking all the files it. By typing the command below, git will create a .git folder in your project folder. Here is an great article explaining what is the .git folder.
git init
Checking the status of our repository
The git status
command will let us see the current status of our repository.
As you can see above we are on out master branch and has no commits. You can also see the index.js
file created earlier under the Untracked files.
Adding files into the staging area
After making the changes in our file, it is important to add the files into the staging area. A staging area is a place where git stores the draft of our file, in the staging area changes will be saved and ready to be committed.
git add <filename> //only add one file
git add <file1> <file2> .... <fileN> //adding multiple files
git add . //add all files to the repository
After running git add
we now run git status
again.
You can now see that the index.js
file has been added and ready to be commit.
Committing changes
After adding the files into the staging area it is time to commit our changes made.
git commit -m "Your commit message here"
After committing all the changes, we have successfully set a checkpoint for our code and we can revert back to the current state whenever we messed up our code in the future.
Now run git status
again to see look at the status of our repository.
You can see now we have a clean working tree and we can continue adding new files and repeat the steps above.
Look at the history of the repository
As we know Git will store all the commits made in the repository, to see it run git log
in your terminal.
It will show information such the hash of the commit, who made it, when it was made and the commit message.
If we just want to look at the commits history without the extra information, we can run the following command:
git log --pretty=oneline
It will only show the commit hash and the commit message instead, this is very useful when you just want to see the full history of the repository.
That’s all guys, thanks for reading the article. This is a basic introduction about Git, there’s more to learn about Git. Stay tune on part 2 of Git For Dummies where we will be learning about branches, GitHub and many more!