A complete, beginner-friendly Git guide with essential commands, simple examples, and practical tips. Perfect for developers looking to master version control fast.
Git is the go-to tool for version control. It helps developers track code changes, work together without overwriting each other’s work, and roll back to earlier versions if needed. Whether you are new to coding or a seasoned pro, this guide breaks down Git into simple steps with real commands you can use right away.
1. Getting Started with Git
Install Git:
Windows/Mac: Download it from git-scm.com and run the installer.
Linux: Open the terminal and run:
sudo apt install git
Set up your info:
Let Git know who you are:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
This info is saved in your commits.
2. Creating and Cloning Repositories
Start a new project with Git:
git init
This turns your current folder into a Git repo.
Copy an existing project:
git clone <repo_url>
This downloads a project from GitHub or any remote Git server.
3. Core Git Commands You shall Use Every Day
Check the current status:
git status
See which files are new, modified, or staged.
Stage files for commit:
git add <file>
Use git add .
to stage everything at once.
Commit the changes:
git commit -m "Describe your changes"
Records your changes in the history.
View commit history:
git log
Shows all past commits.
4. Working with Branches
Create a new branch:
git branch <branch_name>
Keeps your feature or fix separate from the main code.
Switch branches:
git checkout <branch_name>
Move between different lines of development.
Create and switch in one command:
git checkout -b <branch_name>
Merge a branch into the current one:
git merge <branch_name>
Brings changes from one branch into another.
Delete a branch:
git branch -d <branch_name>
Use this after merging to clean up.
5. Remote Repositories (Working with GitHub, GitLab, etc.)
Connect to a remote repository:
git remote add origin <url>
Sets the remote project location.
Push changes to the remote:
git push origin <branch_name>
Uploads your commits to GitHub.
Pull updates from the remote:
git pull
Brings in new changes from others.
Fetch without merging:
git fetch
Pulls down updates but doesn’t change your code yet.
6. Fixing Mistakes
Unstage a file:
git reset <file>
Takes it out of the staging area.
Discard changes in a file:
git checkout -- <file>
Restores the last committed version.
Undo a commit:
git revert <commit_hash>
Makes a new commit that cancels the old one.
Go back to a specific commit:
git reset --hard <commit_hash>
Caution: this deletes later work. Use only if you’re sure.
7. Power User Moves
See file changes:
git diff
Shows what you have changed.
Save your work temporarily:
git stash
Useful if you need to switch tasks.
Get your stashed work back:
git stash apply
Add a tag (great for releases):
git tag v1.0
Marks that commit with a version number.
Final Thoughts
Git can seem complex at first, but once you get the hang of it, it’s a lifesaver. This guide gives you the most important commands in a clean, simple format. Keep this handy as your go-to Git cheat sheet, and share it with your team to help everyone stay in sync.