Understand basic git commands
Published on
By : Mohammed KAIDI
✅ What is Git?
Git is a distributed version control system that lets you track code changes, revert to previous versions, and collaborate with others. It stores project history in snapshots called commits, allowing you to safely experiment without fear of losing work.
Official website: https://git-scm.com/
⚒️ How to Install Git
Windows / Mac / Linux
- Download from https://git-scm.com/downloads
- Follow the installer instructions
- After installation, check with:
📦 Basic Git Workflow
1. Configure Git (First Time Only)
Check configuration:
2. Initialize a Git Repository
Creates a .git
folder to track changes.
3. Add Files and Make a Commit
Staging means preparing files to be part of the next commit.
4. View Commit History
Shows a list of commits with author, date, and message.
5. Connect to a Remote Repository (e.g., GitHub)
Note: Older tutorials use master
; new convention is main
.
6. Clone an Existing Repository
7. Pull Latest Changes
Fetches and merges changes from the remote repo.
8. Branching Basics
Create a new branch:
Switch branches:
Push new branch to remote:
9. Merge Changes
First, switch to main:
If no conflicts, changes from feature-branch
are merged into main
.
10. Resolving Conflicts
If two people change the same lines, Git will ask you to manually fix conflicts. After fixing:
🛠️ Useful Git Commands
| Command | Description |
|-----------------------------|---------------------------------------|
| git status
| Show current state of repo |
| git diff
| Show unstaged changes |
| git stash
| Temporarily save changes |
| git stash pop
| Reapply stashed changes |
| git reset --hard
| Discard all changes (careful!) |
| git log --oneline --graph
| Compact visual commit history |
| git branch -d branch-name
| Delete a local branch |
| git remote -v
| Show configured remote repositories |
🌍 Recommended Git Platforms
- GitHub — Most popular for open-source projects
- GitLab — Great for private repos and CI/CD
- Bitbucket — Supports Git & Mercurial
🧠 Key Concepts Recap
- Repository (Repo): Project folder tracked by Git
- Commit: Snapshot of project state
- Branch: Isolated line of development
- Merge: Combine changes from one branch into another
- Remote: Online copy of repo (GitHub, GitLab, etc.)
- Clone: Copy a remote repo locally
🎯 Final Tips
- Commit often with meaningful messages
- Use branches for features or fixes
- Always pull latest changes before pushing
- Learn how to resolve merge conflicts
- Use
.gitignore
to exclude files from tracking
📚 Useful Links
- Official Git Documentation
- GitHub Learning Lab
- Visual Git Cheat Sheet (PDF)
- Learn Git Branching (Interactive)
Git gives you control over your code. Learn it well — it's essential for any serious developer. 🚀