Git — A basic tool for Developers!
Let’s get Git your projects!
What is Git?
Git is a Distributed Version Control Systems that created on 2005 by Linus Torvalds (and Linux development community). The development of Git was started since the revokement of BitKeeper’s free-of-charge status and it was made for Linux Kernel maintenance.
How to install Git?
To install Git, you can jump into its documentation and choose the right installation method based on your Operating System.
Adventure of Git — Basic Commands
In this section, we will learn basic commands of git by using it right away. But, before doing that, I suggest you to register for an account on an online Git Repository such as GitHub or GitLab. Then, make sure that you’ve installed Git on your PC/Laptop.
Quick Setup
- Create a repository on your chosen online Git Repository. If you use GitHub, then you can go to this link. Insert repository name field with ‘lets-get-git’ (actually, you can name it anything you want 😄)
- Create a new directory for learning git (you can name it anything too).
- Open up your terminal or command prompt at the directory that you’ve created.
git config
First of all, to make things easy, we will setup our identity before using Git because every Git commit will use this information. This can be done using ‘git config’ command.
Actually, if you don’t want to setting up your identity over and over again every time you ‘init’ or ‘clone’ a repository, you can use ‘global’ option after ‘git config’ command. See the example below.
After setting up username and email, we will setting up our default branch into ‘main’ by using the command below.
git init
This command is used to initializing a repository in an existing directory that is not under a version control.
As you can see, this command is initializing an empty Git repository by creating a new subdirectory named ‘.git’ that contains all necessary repository files. Currently, nothing in this directory is tracked yet.
git add
To begin tracking files in this directory, we should have some files in the directory (you don’t say, lmao 😅). So, let’s create a ‘README.md’ file.
After creating a ‘README.md’ file that contained “It’s README” inside it, we should track this file using ‘git add’ command.
Then, to make sure the file tracked by git, we will use ‘git status’ command.
git status
This command is used for checking status of files on the current directory. There are two states to determining the status of files, ‘Untracked files’ and ‘Changes to be committed’. Let’s use this command.
From the image above, we can see the changes of state on files in the current directory. The ‘README.md’ goes to ‘Changes to be committed’ state from ‘Untracked files’ state after added by using ‘git add’ command.
git commit
After adding files to be tracked, we would like to commit our changes. We can do that by using ‘git commit’ command. Let’s commit our changes!
You can write any commit message that you want, but let’s just write ‘Initial Commit’ for our first commit.
git remote
To be able to manage our remote repository, we should configure our remote by using ‘git remote’ command.
Change the ‘InputYourUsername’ into your GitHub/GitLab username. Then, we will check again that our remote already configured.
‘origin’ is the shortname of the remote handler that we’ve specified, while the URLs is the where the remote is located.
git push
After setting up the remote, we will push our changes into the remote server so that our changes will be recorded on the remote server.
If you want to make sure that your changes and files already pushed into the remote server, simply check it on your remote repository. Here’s the example.
git clone
Suppose that we accidentally remove the directory that we were working on but we already pushed all of our changes into the remote server. Well, don’t worry because we can get a copy our existing Git remote repository using ‘git clone’ command. Let’s clone it!
By using the command above, you will get a copy of your existing Git remote repository. It will present as a directory with name of the repository. Let’s check files inside the directory.
git pull
To understand how to use this command, let’s create a new file using the ‘Create new file’ button in your repository at GitHub/GitLab website.
Create a file with ‘Test.txt’ as the filename and “It’s Test” as the content. Then, click “Commit new file” button at the bottom of the page.
After that, we’ll get back to our beloved terminal and run ‘git pull’ command.
All changes in the remote repository will be pulled into our local repository and we can continue our journey without any problem.
Woah! What a fun journey !?
I hope that this tutorial is useful for you and you will be using git on your next projects! Thanks for reading & following my tutorial! 😃
See you!
References:
- Scott C, Ben S. Pro Git