Git 101: A Guide to Help You Started
Make things easier to control using Git!
Definition
When I first step into the computer science world, I wondered how do programmers keep their code always updated with each other. I thought they always use USB or cloud storage to store them but I was wrong.
Instead, they use a platform called Git! Git makes programmers work a lot easier by creating a platform where programmers could do their work separately and then compiling it all together. Maybe you are wondering, what is Git actually?
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. — Git
Without further ado, let’s explore together on how to use Git! I will also be providing an example that you could follow.
PS: The commands that I’m about to show you can be found in their official documentation. It is more complete with the extensions for each commands.
Basic Git Commands
git init
This is the very first command that you would do when you want to initialize a git repository in your folder. This command will add a folder .git where it will hold all settings for your git repository.
git remote <commands>
After we have initialize our folder, we have to create a repository in Git so that we could put our files in the repository. We put the repository link in the command so that it would set the remote there. We could also set our repository to several remote.
git add <files>
Before we push the files to our repository, we have to add our files to the staging area. In this command, you can choose which files that you want to push. You can put the path to the file in the files section, or you can just put “.” if you want to push the whole repository.
git commit
When our files have been staged, we need to save our changes to the local repository first. We can also use the extension “-m” so that we can see our message in our commit later on.
git push origin <branch>
We are ready to push our files to the repository! If you are still wondering what does push mean in Git terminology, think of it as upload. With this command, we are pushing our local repository to our remote repository. Since we only have one branch by default, we put the branch name as “master” for now.
git clone <link>
If you accidentally deleted you local repository after you have pushed it, you can simply retrieve it back by using this command and put the link to the remote repository in the “link” section.
More Advanced Git Commands
We have learned the basic Git commands; now, we are going to use a more advanced commands so that we could work more efficiently with our teammates.
git branch
In Git, there is a terminology called branch. In order to simplify it, you can view Git branch as your workplace. Ideally, every teammates should create their own branch in order to prevent conflict. In this example, I’m going to create a new branch called “branch-test”. We can also use the command to detect which branch we are working on.
git checkout <branch>
With this command, we can switch our branch; or if you prefer, switch our workplace.
git pull origin <branch>
Let’s say our teammates have been working on something and we need it, we can simply retrieve it using this command. Keep it mind that it could create conflicts that we don’t want if there are differences in the same file that we and our teammates are working together.
git merge <branch>
We could merge our work with our teammates if we are working on separate files and we want to combine them together. It would be a lot more efficient rather than pull first and commit it.
git revert <commit SHA>
If we accidentally pushed something, we can easily revert it using this command without having to delete it manually.
git stash
This command is used to stash the changes we have created in the repository to go back to a clean one.
git rebase
This command is useful when we want to combine or move a sequence of commits to a new base commit. It is recommended to create and maintain a linear project history.
Git in Clicks
In Clicks, we have our own git repository to hold our project. In the repository, we have different branches. The branches are named according to the feature that is being build in the branch. There is also a master
branch, that is the branch where the other branches that have finished features are being merged into.
We also have set up the CI for our repository in a single file called .gitlab-ci.yml
. Since we are creating a React Native application, then we only have one stage, which is test
. We use SonarQube to test our project. We don’t have build
stage like most project are because we export the project with expo build
; in other words, we build them separately.
In order to make our commits more organized, we were advised to name our commits in a certain way. As a reference, we used Conventional Commits’ way to name our commit; feel free to check the website out for full explanation. The form would be <type>[optional scope]: <description>
. We have used this form for commit message and it has helped us greatly to understand what our teammates were doing in their branch. Here is an example of our commit message.
Conclusions
There are so many commands in Git that will suit your needs. It is also very flexible, even non-programmers can benefit from it. You can use it to store almost any kind of files.
It is very beneficial if you are building an application with your teammates. Git allows real-time collaboration so that it could be finished faster. It has helped us tremendously in Clicks. We could resolve conflicts and errors together without having to be physically present.