Learn basic git with examples

Nguyễn Việt Hưng
The happy lone guy
Published in
7 min readMar 16, 2019

--

What is git?

*yawn* I know it’s boring but please bear with me.

In 2005, git was created by a genius named Linus Torvalds. Yes, that’s the same guy that gave the world the Linux kernel, which then used to create Ubuntu and Linux Mint which is the Linux distribution that I’m using to write this wonderful post for you guys 😅.

Linus Torvalds is on the right…duh!

Enough of the history lesson, let’s move to the main thing.

Remember when you still create multiple copies of a file after every update? Git was born to solve that issue.

For that reason, git is called a source version control system. It’s used to manage different versions (or changes) of a file.

Git concepts

At this point, you probably have some ideas of what git really is (no you don’t). Let’s learn some git-related concepts to further understand how git will be used in a project.

Git repo or repository

Imagine that you have a project, every change that you make will be recorded by git. Therefore, that project is called a git repository. To transform a normal project into a git repo, we use the command below at the root folder.

git init

That’s it. After initializing a git repo, a hidden folder will appear in the project folder. That hidden folder is called .git . Don’t worry about it for now, but just remember, if a folder doesn’t contain a .git folder, it’s not a repository and therefore, if you want to turn the current project as a git repo into a normal project, you can just delete that folder.

Git commit

Remember the image above where you create new files for each change? That’s what git commit is. A git commit (or a commit) is a change that is recorded by git. Think of it as a return point, in games, you can stop at a checkpoint so that when you die, you won’t have to start again from the beginning and you can return back to the latest checkpoint. In programming, when you created feature A and move on to create feature B, but somehow messed up the whole thing. You will lose feature A and B, your boss will mad at you, your colleagues will beat you up, your wife will take your kids away and you will lose everything just because you didn’t use git commit.

So how do we use git commit? Simple, after you’ve made some changes or finished a feature, you open up your terminal and type in the command

git add <THE FILES TO RECORDS>
git commit -m "<THE MESSAGE>"

Let’s try a real-world example

You’ve finished working on the feature A by changing the code in the feature.js file. You now need to work on feature B also by changing the code in the same file. But before you do that you need to mark feature A as your returning point because if you messed up the feature.js file while working on feature B, feature A will also be affected. So you open your terminal and type in

git add feature.js
git commit -m "finished feature A"

Now you can freely work on feature B without fear. If you happen to make a mistake, you can return to the previous commit which means git will remove all the new code that you added and reset to the point where you’ve just finished feature A.

That’s nice but when do we need to commit? Here are some answers to that question

  • When you’re unsure that you would mess up the current code or not.
  • When you finished a feature, bug fix, etc.
  • When the customer wants to try something new. If they don’t like it, just return back to the previous commit.

Git commit history

Each commit that you make will have a unique hash (If you have time to waste then you can read this to know about commit hash). And after you make a commit, git will record it and place it in a list called a git commit history. Why do we need a git commit history and a commit hash? We will know about it later.

A real git commit history of one of my projects

Git reset

I mentioned going back to the previous commit multiple times, but how do we do that? We use this command

git reset --hard <commit>

!!Danger!! Before running this command, remember to check if your working directory is clean or not using git status which we will talk about it later.

In the command above, the <commit> is the hash of the commit that you want to reset to and you can find the commit hash that you want to reset to in the commit history. Make sure to double check everything before you use this command.

For example, this is your commit history

  7a2f2r0 Finished feature A
9f2r1j2 Finished feature B
* 24jr36t Finished feature C

You have finished working on 3 features and make 3 commits for them and you’re currently on feature C commit. Now the users don’t want to have feature C and B anymore. You reset back to when you have just finished feature A.

git reset --hard 7a2f2r0

Git branch

The first thing that you may think of when you heard about this concept is probably a tree branch. It’s fine you thought about something else.

The way git branch work can be understood in this way. Imagine, you’re working on your project and you notice a bug. You try to fix it but it’s going to take a long time to fix. But your teammate is rushing you to finish the new feature. What do you do? Bash him in the head with a rock, no no no no don’t, we have a better way.

Git provides us with an elegant solution, git allows us to make changes to our code on a different branch.

As you can see you can still work on your bug fix in a different branch and at the same time create more features. When you finished fixing your bug, you can just merge it to the current code. So how do we create a new branch? We use this command

git branch -b <name_of_the_branch>

During development, you can switch between branches to work on different things. In the example above, you have to switch to the bug fix branch to fix the bug and you also have to switch to the development branch to add new features. To do that you use the command

git checkout <name_of_the_branch>

For example, if you’re on the branch development you can jump to the bugfix branch by typing

git checkout bugfix

Git merge

This is where things get complicated. Git merge is the command that helps us merging 2 branches into 1. In the example above, after fixing the bug for feature A, you can merge the branch bug fix with the development branch to get the bug fix code into the development branch. Here’s how the git merge command looks like

git merge <branch_name>

For example, if you want to merge the bug fix branch to get the bug fix code onto the development branch. First, you need to make sure that you’re on the development branch by using

git checkout development

Git will jump to the development branch and make sure that you’re on that branch. Now you can use the merge command to merge the bug fix branch.

git merge bugfix

Git status

One of the command that you will use every day is git status . Git status gives you the status of your working directory, branch, etc. For example, if you make changes to some files, if you use git status, it will say that which file is modified and it has been committed or not. If all the files in your working directory have been committed (mark using git commit ) then your working directory is clean.

Git add

Why did I put this at the bottom of the post but use it on the first concept — git commit ? Well, same reason as to why your teacher uses String in Java class but doesn’t talk about it until you learn about OOP.

Git has something called staging area. This is an area for files that needed to be committed to be moved to. When you use git add , you moved your file(s) into the staging area which just like telling git that I need to put these files into a commit so that I can return to it when I need.

Here’s a real-world example. You’ve finished working on feature A but forgot to commit it and went on working on feature B. Those 2 features located in 2 different files.

featureA.js
featureB.js

Now you only want to commit the featureA.js file because you haven’t finished coding with featureB.js yet. So you use git add and then git commit as usual.

git add featureA.js
git commit -m "finished featureA"

Conclusion

Git can be very difficult to learn and it requires a lot of practice and you may end up deleting some of your files. However, once you get the hang of it, your life is going to be much easier (I hope so).

If you like this post, don’t forget to hit the clap button for as many times as you can. And follow me for more cool posts, although I’m slow on releasing new posts and doesn’t know to write English properly. LOL. Til next time. Peace.

--

--

Nguyễn Việt Hưng
The happy lone guy

A web developer who love to create everything using web technology