Introduction: Why Do We Need Version Control?

Every developer has probably experienced situations like these: "Yesterday's code was better...", "I wish I could go back to before adding this feature", or "My changes conflicted with my colleague's changes, and now everything's a mess." Have you ever added dates to filenames or suffixes like "final", "final_v2", or "final_REALLY_final"?

This is exactly the problem that Version Control Systems (VCS) were designed to solve. And the version control system most widely used by developers worldwide today is Git, which we'll be learning about.

Git was created in 2005 by Linus Torvalds for Linux kernel development. Initially used only within the Linux development team, its excellent performance and flexibility have made it the standard tool for software development. From startups to large enterprises, from solo developers to open-source projects with thousands of contributors, modern software development is hard to imagine without Git.

In this article, we'll explore Git from basic concepts to commands you can use in real-world projects. Even if you're new to Git, following this guide will help you get started with version control.

1. Installing Git

To use Git, you first need to install it on your computer. Installation methods vary slightly by operating system, so follow the instructions for your environment.

1.1 Installing on Windows

For Windows users, downloading the installer from the official Git website is the easiest option.

  1. Visit git-scm.com/download/win.
  2. The download should start automatically. If not, click "Click here to download manually".
  3. Run the downloaded installer.
  4. Most installation options can be left at their default values.
  5. However, at the "Adjusting your PATH environment" step, we recommend selecting "Git from the command line and also from 3rd-party software".

Once installation is complete, search for "Git Bash" in the Start menu and launch it. You can use Git commands in this terminal.

1.2 Installing on macOS

There are several ways to install Git on macOS.

Method 1: Using Xcode Command Line Tools

Open Terminal and enter the following command:

xcode-select --install

When the popup appears, click "Install". Development tools including Git will be installed.

Method 2: Using Homebrew

If you have Homebrew installed, it's even simpler:

brew install git

1.3 Installing on Linux

Use your distribution's package manager.

Ubuntu/Debian:

sudo apt update
sudo apt install git

Fedora/CentOS/RHEL:

sudo dnf install git
# Or for older versions
sudo yum install git

1.4 Verifying Installation

Regardless of your operating system, verify the installation by running this command in your terminal:

git --version

If you see output like git version 2.x.x, installation is complete.

2. Initial Git Configuration

After installing Git for the first time, you need to configure some basic settings. You only need to do this once, and you can change these settings anytime.

2.1 Setting User Information

Git records who wrote each commit. So you first need to set your name and email.

# Set your name
git config --global user.name "John Doe"

# Set your email
git config --global user.email "john@example.com"

The --global option applies these settings to all Git repositories on this computer. If you want to use different information for a specific project, run the same commands without --global from that project's folder.

2.2 Setting the Default Editor

Git opens a text editor when you write commit messages. The default is Vim, but if you're not familiar with Vim, you can change it to another editor.

# Use VS Code
git config --global core.editor "code --wait"

# Use Notepad++ (Windows)
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

# Use nano (Linux/macOS)
git config --global core.editor "nano"

2.3 Checking Configuration

To check your current settings, use these commands:

# Check all settings
git config --list

# Check specific settings
git config user.name
git config user.email

3. Understanding Git's Core Concepts

To use Git properly, you need to understand several core concepts. While they might seem confusing at first, understanding them makes all Git commands logically connected.

3.1 Three Areas: Working Directory, Staging Area, Repository

Git manages files in three areas.

Working Directory

This is where you actually edit files. It's the folder where you write and modify code. Files in this area can be either "tracked" or "untracked" by Git.

Staging Area (Index)

This is where you prepare files to be included in the next commit. It's like marking files saying "I want to save the current state of these files." You add files to this area using the git add command.

Repository

This is where Git stores all version information. The .git folder inside your project folder is this repository. The git commit command permanently records staged files here.

Think of it like creating a photo album. The Working Directory is like all photos in your camera roll, the Staging Area is the photos you've selected "for this album," and the Repository is the photos you've actually printed and put in the album.

3.2 File States

In Git, files can have the following states:

  • Untracked: New files that Git isn't tracking yet
  • Staged: Files added to the staging area
  • Modified: Files that have been changed but not yet staged
  • Committed (Unmodified): Files safely saved in the repository

4. Mastering Basic Commands

Now let's actually use Git. We'll look at the most frequently used basic commands one by one.

4.1 git init - Creating a Repository

This is the first command you run when starting a new project.

# Create a new folder and initialize Git repository
mkdir my-project
cd my-project
git init

Running this command creates a hidden folder called .git in the current directory. Git stores all version control information in this folder.

4.2 git status - Checking Status

This checks the current state of your working directory and staging area. It's the command you'll run most frequently when using Git.

git status

The output shows which files have been modified, which are staged, and which are untracked, all at a glance.

4.3 git add - Staging

This adds files to the staging area.

# Stage a specific file
git add index.html

# Stage multiple files
git add index.html style.css script.js

# Stage all changes in current folder
git add .

# Stage files with specific extension
git add *.js

Tip: While git add . is convenient, you might accidentally stage unnecessary files. Always check staged files with git status before committing.

4.4 git commit - Saving Changes

This records staged files to the repository.

# Commit with a message
git commit -m "Implement login feature"

# Stage and commit in one step (only for already tracked files)
git commit -am "Fix bug"

Writing Good Commit Messages

Commit messages are crucial when reviewing change history later. Follow these guidelines:

  • Clearly describe what was changed
  • Write in imperative mood (e.g., "Add feature" not "Added feature")
  • Keep the first line under 50 characters
  • Add detailed explanation after a blank line if needed

4.5 git log - Viewing Commit History

This shows the history of commits.

# View basic log
git log

# View in single-line format
git log --oneline

# View branch structure as graph
git log --oneline --graph

# View only last 5 commits
git log -5

# View history for specific file
git log -- index.html

4.6 git diff - Viewing Changes

This shows the specific changes made to files.

# Compare working directory with staging area
git diff

# Compare staging area with last commit
git diff --staged

# Compare two commits
git diff abc123 def456

# Compare specific file only
git diff index.html

5. Configuring .gitignore

Every project has files that shouldn't be managed with Git. Examples include build artifacts, log files, configuration files with API keys, and dependency folders like node_modules. The .gitignore file tells Git to ignore these files.

5.1 Creating a .gitignore File

Create a .gitignore file in your project's root folder and list patterns for files or folders to ignore.

# OS-related files
.DS_Store
Thumbs.db

# Editor/IDE settings
.vscode/
.idea/
*.swp

# Dependency folders
node_modules/
vendor/

# Build artifacts
dist/
build/
*.min.js
*.min.css

# Log files
*.log
logs/

# Environment config files (sensitive info)
.env
.env.local
config/secrets.yml

# Cache
.cache/
__pycache__/
*.pyc

5.2 .gitignore Pattern Syntax

  • # at the start of a line indicates a comment
  • * matches zero or more characters
  • ? matches exactly one character
  • / at the start means current folder only
  • / at the end indicates a folder
  • ! at the start creates an exception (don't ignore)
# Ignore all .txt files
*.txt

# But track important.txt
!important.txt

# Ignore TODO file only in root (track TODO in subfolders)
/TODO

# Ignore entire build folder
build/

# Ignore .txt files only in doc folder
doc/**/*.txt

5.3 Ignoring Already Tracked Files

If a file continues to be tracked even after adding it to .gitignore, it's because Git already started tracking it. In this case, you need to remove it from the cache.

# Stop tracking specific file (file is not deleted)
git rm --cached config.json

# Stop tracking folder
git rm -r --cached node_modules/

# Commit the changes
git commit -m "Add gitignore and remove tracked files"

6. Undoing Mistakes

One of Git's most powerful features is the ability to undo mistakes. Different commands are used depending on the situation.

6.1 Unstaging Files

If you accidentally staged a file, you can unstage it with these commands:

# Unstage specific file
git restore --staged index.html

# Unstage all files
git restore --staged .

6.2 Discarding File Changes

To discard changes in the working directory and revert to the last commit:

# Restore specific file to last commit state
git restore index.html

# Warning: This command completely deletes changes!
# Unsaved work cannot be recovered.

6.3 git reset - Undoing Commits

git reset is a command for undoing commits. It has three modes:

# --soft: Undo commit only, keep changes staged
git reset --soft HEAD~1

# --mixed (default): Undo commit, keep changes in working directory
git reset HEAD~1

# --hard: Undo commit and completely delete changes (caution!)
git reset --hard HEAD~1

HEAD~1 means "one commit before current." HEAD~3 means three commits before.

Warning: git reset --hard completely deletes changes. Resetting commits that have been shared with others can cause serious problems, so use it only locally.

6.4 git revert - Safely Undoing

git revert creates a new commit that undoes the changes of a specific commit. It can be safely used to undo commits that have already been shared.

# Revert specific commit
git revert abc123

# Revert most recent commit
git revert HEAD

Unlike reset, revert doesn't delete history but adds a new commit, making it safer in collaborative environments.

7. Practical Examples and Tips

7.1 Typical Git Workflow

Let's see how Git is used in actual development with an example.

# 1. Start new project
mkdir my-website
cd my-website
git init

# 2. Create files and first commit
echo "<h1>Hello World</h1>" > index.html
git add index.html
git commit -m "Initialize project: create index.html"

# 3. Develop features
# (edit files...)
git status  # Check changes
git diff    # Check specific changes
git add .
git commit -m "Add header navigation"

# 4. When you make a mistake
git log --oneline  # Check history
git reset --soft HEAD~1  # Undo commit and rework
# (after fixing)
git add .
git commit -m "Add header navigation (bug fixed)"

7.2 Setting Useful Git Aliases

You can shorten frequently used long commands.

# Set aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all"

# Usage examples
git st      # Same as git status
git lg      # Pretty log graph

7.3 Common Mistakes and Solutions

When you wrote the wrong commit message:

git commit --amend -m "Corrected commit message"

When you forgot to include a file in your commit:

git add forgotten-file.txt
git commit --amend --no-edit

When you worked on the wrong branch:

# Temporarily save current changes
git stash
# Move to correct branch
git checkout correct-branch
# Restore changes
git stash pop

Conclusion

In this article, we've covered Git basics. From Git installation to basic commands, .gitignore configuration, and ways to undo mistakes. The many commands might seem confusing at first, but they'll become second nature with actual use.

With what you've learned today, you can manage version control for your own projects. But Git's true power is revealed in collaboration. In the next article, we'll explore remote repository management and collaboration methods using GitHub.

Git might feel difficult at first, but once you get comfortable with it, it becomes an essential tool that greatly improves development productivity. Apply the commands you've learned today to your actual projects, and if there's anything you don't understand, try using git help [command]. I hope you enjoy your development journey with Git!