Git for Beginners: Basics and Essential Commands
Source: Dev.to
What is Git?
Git is an open-source Distributed Version Control System (DVCS). Created in 2005 by Linus Torvalds (the creator of Linux), it was built to handle massive projects with high speed and efficiency.
In simple terms, Git is a “time machine” for your project. Developers use it to:
- Track Changes – See exactly what was changed, when, and by whom.
- Collaborate – Work on the same codebase with multiple people simultaneously.
- Manage History – Revert back to a previous version if something breaks.
Whether you are working alone or in a big team, Git ensures your project history is safe and organized.
Core Concepts
- Working Directory – Your local folder where you actively edit files.
- Staging Area (Index) – The “waiting room.” Files go here when they are ready to be saved.
- Local Repository – A hidden
.gitfolder on your machine that stores your project’s history. - Remote Repository – A version of your project hosted online (e.g., GitHub) for sharing and backup.
- Commit – A “snapshot” of your project. Each commit has a unique ID and a message explaining the changes.
- Branch – A parallel workspace. It lets you develop features without breaking the main code.
- Merge – Combining changes from one branch into another (e.g., merging a “feature” branch into “main”).
- HEAD – A pointer that marks your current location in the project history. Think of it as a “You Are Here” sign.
Essential Git Commands
Configuration and Repository Setup
# Set your name for all your commits
git config --global user.name "Your Name"
# Set your email address
git config --global user.email "yourname@example.com"
# Turn a local folder into a Git repository
git init
# Download an existing project from a URL (like GitHub)
git clone [url]
Working with Changes
# Show modified files in your working directory and staged files for your next commit
git status
# Add a file to the staging area for the next commit
git add [file]
# Add all untracked files to the staging area
git add .
# Remove a file from the staging area while retaining changes in the working area
git reset [file]
# Show the difference of what is changed but not staged
git diff
# Show the difference of what is staged but not yet committed
git diff --staged
# Commit your staged content as a new snapshot
git commit -m "[descripted message]"
# Shortcut: stage and commit tracked files
git commit -am "[descripted message]"
Branch Management
# List all local branches
git branch
# Create a new branch
git branch [branch-name]
# Switch to a specific branch
git checkout [branch-name]
# Combine another branch's history into your current one
git merge [branch-name]
Viewing History
# Show the commit history of the current branch
git log
# Show the commits on branchA that are not on branchB
git log branchB..branchA
# Show the commits that change a file, even across renaming
git log --follow [file]
# Show the difference of what is in branchA that is not in branchB
git diff branchB...branchA
# Show any object in Git in human‑readable format
git show [SHA]
Working with Remotes
# Add a remote repository (alias)
git remote add [alias] [url]
# Fetch all branches from the remote
git fetch [alias]
# Merge the remote branch into your current branch
git merge [alias]/[branch]
# Push local commits to the remote repository
git push [alias] [branch]
# Fetch and merge any commit from the tracking remote branch
git pull
Practical Example
-
Create a new project folder (Working Directory)
mkdir my-first-git-project cd my-first-git-project git init # Initialize Git (run once per project) -
Create a file
echo "Hello Git!" > hello.txt -
Check the status
git statusYou will see
hello.txtlisted as “untracked.” -
Stage and commit
git add hello.txt git commit -m "Initial commit: Create hello.txt" -
Make further changes
echo "Learning Git is fun!" >> hello.txt git diff # See exactly what changed before saving -
Stage and commit the updates
git add . git commit -m "Update hello.txt with a new sentence" -
View the project history
git log --oneline
Each line in the log represents a point in time you can return to if something goes wrong. You’ve just successfully tracked your first project!
Version control isn’t just about saving code; it’s about the freedom to experiment without fear of breaking your project.