Git is a version control system used by software developers, system administrators, web developers, and open-source contributors to track changes in files and projects. It allows you to save snapshots of your work, return to previous versions when something breaks, experiment safely with new ideas, and collaborate with other people without overwriting each other’s changes.
Think of Git as a time machine for your projects. Every time you create a commit, Git stores a snapshot of your files. If you make a mistake later, you can review previous versions and restore them if necessary.
Installing Git
On Ubuntu or Debian Linux, install Git with:
sudo apt update
sudo apt install git
On Fedora:
sudo dnf install git
On Arch Linux:
sudo pacman -S git
On macOS, install Git through Homebrew:
brew install git
Or install Apple’s Command Line Tools:
xcode-select –install
To verify the installation, run:
git –version
Git should display its version number.
Initial Configuration
Before using Git, configure your name and email address. These values are stored in each commit and identify who made the changes.
git config –global user.name “Your Name”
git config –global user.email “you@example.com“
To view your configuration:
git config –list
This setup only needs to be done once on a computer.
Creating Your First Repository
A repository, often called a “repo,” is a project directory managed by Git.
Create a project folder:
mkdir myproject
cd myproject
Initialize Git:
git init
Git creates a hidden .git directory that stores all version control information.
You can verify the repository status with:
git status
Initially, Git will report that there are no commits yet.
Creating and Tracking Files
Create a file:
touch hello.c
Edit the file and add:
#include <stdio.h>
int main(void)
{
printf(“Hello World\n”);
return 0;
}
Save the file.
Run:
git status
Git will show hello.c as an untracked file. Untracked means Git sees the file but is not yet monitoring changes to it.
To tell Git to track the file:
git add hello.c
The file is now staged for commit.
Check the status again:
git status
Git should indicate that the file is ready to be committed.
Creating Your First Commit
A commit is a saved snapshot of your project.
Create your first commit:
git commit -m “Initial commit”
The message should briefly describe what changed.
You now have a permanent record of the project’s current state.
To view commit history:
git log
For a shorter view:
git log –oneline
Each commit has a unique identifier called a hash.
Making Changes
Edit hello.c and change:
printf(“Hello World\n”);
to:
printf(“Hello Git\n”);
Save the file.
Run:
git status
Git reports that the file has been modified.
To see exactly what changed:
git diff
Git displays the differences between the current file and the last committed version.
To save the change:
git add hello.c
git commit -m “Changed greeting message”
You now have two commits in your repository history.
Understanding the Git Workflow
Most Git work follows the same cycle:
- Modify files.
- Stage changes with git add.
- Create a commit with git commit.
- Push changes to a remote repository if needed.
The commands are typically:
git status
git add .
git commit -m “Describe changes”
git push
This workflow is used by millions of developers every day.
Viewing Project History
To see all commits:
git log
For a condensed version:
git log –oneline
To compare your current work against the previous commit:
git diff HEAD~1 HEAD
Git’s ability to track history is one of its greatest strengths. It allows you to investigate when a bug was introduced and who made a particular change.
Restoring Files
If you modify a file and decide you do not want the changes:
git restore filename
Example:
git restore hello.c
This discards uncommitted changes and restores the file to its last committed state.
Use this command carefully because discarded changes cannot easily be recovered.
Branches
Branches allow you to work on new features without affecting the main project.
Create a branch:
git branch feature1
Switch to it:
git switch feature1
Older Git versions may use:
git checkout feature1
To see all branches:
git branch
The current branch is marked with an asterisk.
Many developers create a branch for every feature or bug fix they work on.
Merging Branches
After completing work on a branch, switch back to the main branch:
git switch main
Merge the branch:
git merge feature1
Git combines the changes into the main branch.
If the merge succeeds, the feature becomes part of the project.
Deleting Branches
After a branch has been merged:
git branch -d feature1
This keeps the repository organized.
Working with GitHub
Git stores code locally, but many developers also use online hosting services.
One of the most popular is GitHub.
After creating a repository on GitHub, connect your local repository:
git remote add origin https://github.com/username/repository.git
Verify the connection:
git remote -v
Push your commits:
git push -u origin main
After the first push, future pushes usually require only:
git push
To download updates from GitHub:
git pull
Cloning Existing Projects
Instead of creating a repository from scratch, you can download an existing project.
Example:
git clone https://github.com/torvalds/linux.git
This creates a local copy of the Linux kernel source code.
Move into the project:
cd linux
You can now browse, build, and modify the code.
How Git Is Used in Open Source
Most open-source development follows a simple process:
A developer discovers a bug or identifies an improvement.
They create a branch.
They modify the code.
They test the change.
They commit their work.
They submit the change for review.
Reviewers inspect the code, request improvements if needed, and eventually merge the contribution.
This is essentially how contributions are made to projects ranging from small utilities to the Linux kernel itself.
The Commands You Will Use Most Often
git status
Shows the current state of your repository.
git add .
Stages all modified files.
git commit -m “message”
Creates a new snapshot.
git log –oneline
Displays commit history.
git diff
Shows changes since the last commit.
git branch
Lists branches.
git switch branchname
Switches branches.
git merge branchname
Combines changes from another branch.
git pull
Downloads updates from a remote repository.
git push
Uploads your commits.
git clone URL
Downloads an existing repository.
Final Thoughts
Git can appear intimidating at first because of the number of available commands, but most developers use only a small subset on a daily basis. If you become comfortable with git status, git add, git commit, git log, git pull, and git push, you will already have enough knowledge to manage personal projects and begin contributing to open-source software.
As your skills grow, you can learn advanced topics such as rebasing, resolving merge conflicts, bisecting bugs, signing commits, creating pull requests, and generating patches for projects such as the Linux kernel. However, mastering the basic workflow first will provide a strong foundation for everything else you do with Git.