
Knowing how to use Git and understanding its basic commands allows you to ensure that every change to your project is well-documented.
So, what preparations do you need to make before using Git? And what are the steps to get started? Here is a Git tutorial you can follow!
Table of Contents
Preparations Before Using Git
Before we dive into how to use Git, there are a few things you need to prepare, from understanding how Git works and installing it, to familiarizing yourself with its basic commands.
1. Understand What Git Is
Before using Git, you first need to understand what it is. What is Git actually used for? Simply put, Git is used to record changes to code in your files.
With Git, a software development team can collaborate effectively, save different versions of files, and easily merge changes from various sources.
2. Install Git
Once you understand how Git works, the next step is to install it on your computer. You can download the installation file for free directly from the official Git website.
3. Understand Command Prompt (CMD) Commands
After installing Git, you should also have a basic understanding of how to use the command prompt (CMD). Here is a list of fundamental CMD commands you’ll use when working with Git:
cd
: Changes the current directory.dir
: Displays a list of files and directories in the current directory.mkdir
: Creates a new directory.q
: Quits or exits a program’s display view.rmdir
: Deletes a directory.del
: Deletes one or more files.echo
: Displays a message or sets the value of an environment variable.cls
: Clears the screen.copy
: Copies a file from one location to another.move
: Moves or renames files and directories.exit
: Exits the CMD or closes the CMD window.
4. Understand Basic Git Commands for Beginners
Git has many commands, but which ones are essential for beginners? Below is a summary of the Git commands we will be using in this tutorial:
git init
: Initializes a new directory as a Git repository.git add
: Adds file changes to the staging area.git log
: Displays the commit history.git commit
: Records changes from the staging area into the repository.git checkout
: Switches to a specific commit or branch.git show
: Shows the details of changes from a specific commit.git status
: Shows the status of changes in the current repository.
For a more comprehensive list, you can refer to a Git cheat sheet.
How to Use Git for Beginners
For newcomers, using Git can be broken down into six simple steps, from creating a local Git repository on your computer to viewing previous versions of your code files.
Step 1: Create a Local Git Repository
The first step in using Git is to create a local repository. Here’s how:
- Open your terminal or command prompt (CMD) to execute Git commands. We’ll be using CMD in this guide.
- Navigate to the directory where the files you want to track are located. For example, we are using a directory named 00-belajar-coding where we will modify our code files.
- Once you are in the correct directory, type the command
git init
and press Enter. The git init command initializes, or creates, a Git repository. This repository is where all version changes will be stored. If successful, a .git repository folder will appear.
Step 2: Add Files to the Staging Area
The next step is to add the files you plan to modify to the staging area. Think of the staging area as a workbench where you place files that are ready for their changes to be saved.
There are three ways to add files to the staging area: adding (1) all files, (2) a single file, or (3) specific files. Here are the commands for each method:
git add .
: This command adds all modified and new files to the staging area. Don’t forget the space and the period.git add <nama-file.html>
: This command adds a single specific file to the staging area. Example:git add learning-git-2.html
.git add <nama-file.html> <nama-file-1.html>
: This command adds multiple specific files to the staging area. Example:git add learning-git-1.html learning-git-2.html
.
Since we plan to modify a single file, we will use the second method: git add <nama-file>
. In our example, the command is git add belajar-html-1.html
.
To verify that the command worked correctly, you can use the git status
command. If you see one of the following three pieces of information, your git add process was successful:
- Changes to be committed: This indicates that changes have been added to the staging area and are ready to be committed. The files listed under this section are the ones you have successfully added.
- Changes not staged for commit: This shows files that have been modified since the last commit but have not yet been added to the staging area. If you run
git add
and no longer see your file here, it means it has been successfully staged. - Untracked files: This indicates files that have never been tracked by Git. If you run
git add
for a file and it no longer appears in this section, it has been successfully added to the staging area.
Step 3: Editing the File
Once your files are in the staging area, you can edit them using your preferred text editor. Here are two examples of how to open a file for editing:
- Method 1: In the CMD terminal, type
notepad <nama-file>
. Example:notepad learning-git-2.html
. This command is useful if you want to edit the file using Notepad.
- Method 2: In the CMD terminal,
code <nama-file>
. Example:code learning-git-2.html
. This command is useful if you want to edit the file using VS Code.
After editing the code in your file, click Save, and then run the git add
command again. The reason you need to do this is that git add
moves the most recent changes from your working directory to the staging area, preparing them for the next commit.
Step 4: Commit the File Changes
After completing the third step, the next is to use the git commit
command to commit your changes. A commit in Git is an action that saves the changes you’ve made to the Git repository.
When you run git commit
, you must include a commit message. This message should explain the changes you made. Here are a few ways to write your commit message:
- Method 1, Directly in the Command Line: You can add the commit message directly after the
git commit
command using the-m
option. Format:git commit -m "Your Commit Message"
. Example:git commit -m "change log"
.
- Method 2, Using the Default Text Editor: If you just run the git commit command without the
-m
option, Git will open the default configured text editor (like Vim, Nano, or another) for you to write your commit message. - Method 3, Using a GUI (Graphical User Interface): If you are using a GUI tool for Git, such as GitHub Desktop, GitKraken, or others, there will usually be a dedicated text box for you to write your commit message.
Commit messages are vital for providing context and understanding the history of changes in a repository, making it easy for your team or other developers (including your future self) to understand the reason behind each change.
Step 5: View the Commit History
Once your changes are saved, you can view previous versions. Use the git log
command to see a detailed history of your commits.
With the git log command, all change information will appear. If you look closely, the details provided are as follows:
- Commit ID (SHA/hash): a unique code that represents the commit.
- Author: who made the commit.
- Date and time of the commit.
- Commit message.
In addition to the standard git log
command, you can also customize the information displayed with command modifications like these:
git log --oneline
: to see a more concise history view.git log -p
: to display the differences in each commit, allowing you to see exactly what code changed.git log -n <number>
: to display a specific number of recent commits. Example:git log -n 5
displays the last 5 commits.
The information displayed is useful for subsequent actions. For example, the commit ID is used to view the details of a file version from that specific commit.
After running git log, you can exit the information display by pressing q
, which stands for quit. As you can see, using Git is closely tied to CMD commands.
Step 6: Viewing Previous File Versions
If you need to view or revert to a previous version of a file, use the git checkout
command along with the commit ID you obtained earlier. Here’s how to do it:
To restore a file to a previous version:
- Enter the command
git checkout <commit_id> -- <path_to_file>
. For example:git checkout 2739bde614f6dd67b73a8a020ee3e8f99dbb865c -- belajar-html-1.html
as shown below:
- Next, open the file in a text editor like VS Code or Notepad. The contents of the file will now match the version from that commit ID.
To view the contents of a file from a previous version without changing your current file:
You can use the git show
command. This command displays the file’s information directly in the terminal.
- Enter the command
git show <commit_id>:<path_to_file>
. For example:git show 2739bde614f6dd67b73a8a020ee3e8f99dbb865c:belajar-html-1.html
. - The code from that past version will be printed directly to your terminal. This is a great way to inspect old code without overwriting your current work.
Are You Ready to Use Git?
That concludes our beginner’s tutorial on how to use Git and its basic commands. By using Git, you can manage your project’s source code more efficiently, monitor changes, and collaborate easily with your development team.
As a web developer, Tonjoo also leverages Git to develop websites with our team. This allows us to work together efficiently and ensure optimal product quality.
Our team has successfully handled a variety of projects, from government websites to startups, including Universitas Gadjah Mada and Cakap.com. Let’s discuss the website you envision. Contact Tonjoo today, and we will help bring it to life!
Read similar articles by Moch. Nasikhun Amin on the Tonjoo blog about WordPress, WooCommerce, plugins, and other web development topics.
Updated on July 29, 2025 by Moch. Nasikhun Amin