When I started using Git, the first thing I thought was:
“Why don’t we use Google Drive? Isn’t it easy enough? You just need to drag and drop to share files, and there’s a version control system too. No need to remember many commands like pull, push, commit, fetch, etc.”
When you want to learn something, you need to understand ‘why is it important for me?’. Do not follow my mistake. I understood to use Git in my fifth semester. Yes you read that right. Fifth semester. What did I do in the first four semester? Didn’t I use Git for several courses project? Yes, I used git. But at that time, I am still confused when I needed to merge with another branch. How do I fix the conflicts? Why can’t I commit right now? What does remote mean? Lot of questions were in my head. Here, I will share what I think that you need to know about Git. Let’s Git going! (pardon the pun)
Git? What’s that?
Git is the best friend of a developer. Let’s first thank Linus Torvalds, creator of Git that has made developer’s life easier. The creation of Git allowed us to code and collaborate easily. Wait, haven’t you already code without Git? Of course you can. First, let’s learn why Git can make your coding experience better.
Git, your best friend to code
Let’s say you are doing a simple website for taking reminder/schedule for yourself. Assume that the website is now working perfectly for your needs. But you want to integrate your application with Google Calendar, so your Android phone can give you notifications if an event is going to start. You start to code too excited, so you stay until midnight still coding. You want to sleep, you save your project without a backup.
The next morning, you woke up realized there’s a fatal error in your code, because you code while your mind is half-asleep. Oh shoot, what have I done? You tried to CTRL+Z to undo your code, but your text editor cannot undo it. You started to scratch your head thinking, “If only I can revert to the previous version of my project”. This is where Git as a version control system useful. As it is said in its name, Git can control your version of a project. Git stores the history of your project. Let’s say you want to revert to the project state 1 day ago. Wait, does that mean Git stores every change you made? Of course no. Here, we use the term commit. Before you commit, you need to add the files you want to add to a commit. When you add and commit something, you tell git: Hey Git, here I give you some files I want to add, and I want to commit my changes. Please remember this state of my project. This is why Git made your code-ing experience better. There’s also a command git status, to see what’s your current status of git.
Oh my god, what’s that? So many texts to read. There are several informations you can get from the image. The first line shows that you are currently on a branch named “PBI-1-registrasi”. Then, the next line tells you that your branch is up to date with the remote. What is remote? Later you will understand. Then, git tells you the files that you have changed but haven’t commited. The last lines gives you the file that you haven’t added yet to Git.
Collaboration with Git
Then, you started using Git. You’re too happy that you found your “future best friend”, named Git, so you announce to your friend that you use it. One of your friend offered to help your project, so you can focus on your college works. Then your friend has some brilliant features to be added to your website. Let’s say that feature is Registration and Login, so not only you can use the site, but the whole world can. Okay, so you and your friend code together. You started to realize, if you code together, how can we share our code? There’s an online git-repository manager, such as GitLab and GitHub. You choose to use GitLab. So how can we use GitLab?
First, you and your friend need to create a Gitlab account. Then you can create a repository. Then you think “How can I upload my current local code to GitLab?”. Here, you will understand what does push and pull mean. Like it’s name, pushing will push your code to GitLab repository. When it’s done, your friend needs to clone your GitLab repo. The GitLab repository is called your remote. Then, when you want to download the most recent update on the repository, you use the pull command. So, you and your friend has already understand commit, push, and pull. That’s amazing! You can collaborate now.
Then, you start to code. You realize when you has already pushed a commit and your friend want to push, the Git will warn your friend. In this scenario, branch is needed for you. You can think of branch as a new working environment. One way the branch is differed is the features worked on that branch. The branch A has a different feature than branch B. But in reality, the branching is not as simple as that. For the sake of this easy-to-read-article, let’s make it easy. So, you created two branches, g-calendar-integration and registration-login. Wow, now your working space is differed, you can code freely!
Now you started working together in a different branch. Someday, you want to help your friend in their branch about a problem. How do you switch to their branch? You can use the checkout command. There’s a commonly used checkout branch parameter, git checkout -b. This is used when you want to create a new branch with the content of your current branch. Now, you can freely switch between branch.
Then you and your friends has finished each features. Now what? We need to combine our work. Merge will help you with that. Git merge will combine one branch with another branch into one. So, if you are satisfied with the result of each branch, you merge them. Okay, you start to merge them. Then, git tells you there’s a conflict. OH MY GOD, WHAT’S HAPPENING? WHAT HAVE I DONE WRONG? No, no, stay calm. It is very natural for that to happen. Conflict happens when you try to merge two branches, but there’s a code conflict. I will not give the details on how to fix your conflict in this article, you can search for that or my article will be longer than Harry Potter’s book.
Wow, now you understand several git commands/terms like push, pull, commit, add, merge, remote, branch, checkout, conflict, GitLab/GitHub, you can start to use Git in your future projects. There’s many information about Git that I haven’t told you, like what is the difference between clone and pull, how to remove an added file in a commit, how to revert a commit, Git Flow, and many more. Thank you for reading and have a nice day!
GIT Good: A Practical Introduction to GIT and GitHub (Hello GitHub)
This is a two part series on GIT and GitHub, → GIT-Good Part-A: Hello GitHub GIT-Good Part-B: In Git we trust! You…
Git is a free and open source distributed version control system designed to handle everything from small to very large…