Git Essential Commands: The Complete Guide Every Developer Needs
Git is one of the most important tools for software developers. Whether you are building a small personal project or working with a large development team, Git helps you track changes, manage versions, and collaborate with others.
This guide covers the Git commands that developers use every day. Each command includes a simple explanation and practical examples, making it easy for beginners and experienced developers alike.
By the end of this guide, you will have a complete reference that you can return to whenever you need a Git command.
Table of Contents
- What is Git?
- Install Git
- Check Git Version
- Configure Git
- Create a Repository
- Clone a Repository
- Check Repository Status
- Add Files
- Commit Changes
- View Commit History
- Branch Management
- Switch Branches
- Merge Branches
- Delete Branches
- Remote Repositories
- Push Changes
- Pull Changes
- Fetch Changes
- Undo Changes
- Git Stash
- Git Tags
- Compare Changes
- Reset Commands
- Clean Untracked Files
- Git Ignore
- Helpful Git Commands
- Daily Git Workflow
- Best Practices
- Git Cheat Sheet
- Final Thoughts
What is Git?
Git is a distributed version control system that helps developers manage source code.
With Git, you can:
- Track every change
- Restore previous versions
- Work on multiple features at the same time
- Collaborate with other developers
- Merge code safely
- Keep your project history organized
Install Git
Download Git for your operating system from the official Git website.
Windows
https://git-scm.com/download/win
macOS
brew install git
Ubuntu
sudo apt update
sudo apt install git
Check Git Version
git --version
Example output
git version 2.49.0
Configure Git
Set your username.
git config --global user.name "John Doe"
Set your email.
git config --global user.email "john@example.com"
View your configuration.
git config --list
Create a Repository
Create a new repository.
git init
This creates a hidden .git folder inside your project.
Clone a Repository
Clone an existing repository.
git clone https://github.com/username/project.git
Clone into a specific folder.
git clone https://github.com/username/project.git my-project
Check Repository Status
git status
This command shows:
- Modified files
- New files
- Deleted files
- Staged files
- Current branch
Most developers use this command many times every day.
Add Files
Add one file.
git add index.php
Add multiple files.
git add file1.php file2.php
Add all files.
git add .
Commit Changes
Commit staged files.
git commit -m "Add login page"
Stage and commit tracked files.
git commit -am "Fix login bug"
View Commit History
Show commit history.
git log
Compact view.
git log --oneline
Show graph.
git log --graph --oneline --all
Show last five commits.
git log -5
Branch Management
Create a branch.
git branch feature-login
List branches.
git branch
List all branches.
git branch -a
Rename current branch.
git branch -m new-name
Switch Branches
Switch branch.
git checkout feature-login
Create and switch.
git checkout -b feature-login
Using the newer Git command.
git switch feature-login
Create and switch.
git switch -c feature-login
Merge Branches
Switch to the target branch.
git checkout main
Merge another branch.
git merge feature-login
Delete Branches
Delete a merged branch.
git branch -d feature-login
Force delete.
git branch -D feature-login
Remote Repositories
View remote repositories.
git remote -v
Add a remote.
git remote add origin https://github.com/username/project.git
Change remote URL.
git remote set-url origin https://github.com/username/new-project.git
Remove a remote.
git remote remove origin
Push Changes
Push current branch.
git push
Push to origin.
git push origin main
Push and create upstream.
git push -u origin main
Pull Changes
Download and merge changes.
git pull
Pull from a branch.
git pull origin main
Fetch Changes
Download changes without merging.
git fetch
Fetch all remotes.
git fetch --all
Undo Changes
Discard changes in one file.
git checkout -- index.php
Using the newer command.
git restore index.php
Restore all files.
git restore .
Git Stash
Save current work.
git stash
View stash list.
git stash list
Restore latest stash.
git stash pop
Apply without deleting.
git stash apply
Delete a stash.
git stash drop
Clear all stashes.
git stash clear
Git Tags
Create a tag.
git tag v1.0.0
Annotated tag.
git tag -a v1.0.0 -m "First release"
List tags.
git tag
Push tags.
git push --tags
Compare Changes
Compare working directory.
git diff
Compare staged changes.
git diff --cached
Compare two branches.
git diff main feature-login
Reset Commands
Unstage files.
git reset
Reset one file.
git reset index.php
Soft reset.
git reset --soft HEAD~1
Mixed reset.
git reset HEAD~1
Hard reset.
git reset --hard HEAD~1
Warning: git reset --hard permanently removes uncommitted changes.
Clean Untracked Files
Preview files.
git clean -n
Delete untracked files.
git clean -f
Delete files and folders.
git clean -fd
Git Ignore
Create a .gitignore file.
Example
node_modules/
vendor/
.env
.env.local
*.log
.DS_Store
Thumbs.db
Git ignores these files during commits.
Helpful Git Commands
Show current branch.
git branch --show-current
Show repository root.
git rev-parse --show-toplevel
Show author information.
git show
View one commit.
git show COMMIT_ID
Find who changed a line.
git blame filename.php
Search commit messages.
git log --grep="login"
Search code history.
git log -S "function login"
Daily Git Workflow
A typical workday looks like this.
git pull
git checkout -b feature-profile
git status
git add .
git commit -m "Add user profile page"
git push -u origin feature-profile
After the pull request is approved:
git checkout main
git pull
git branch -d feature-profile
Best Practices
- Commit small changes.
- Write meaningful commit messages.
- Pull before starting work.
- Push your work regularly.
- Use feature branches.
- Keep the main branch stable.
- Review code before merging.
- Never commit passwords or API keys.
- Always use a
.gitignorefile. - Create tags for production releases.
Git Cheat Sheet
| Task | Command |
|---|---|
| Initialize repository | git init |
| Clone repository | git clone URL |
| Check status | git status |
| Stage files | git add . |
| Commit | git commit -m "message" |
| View history | git log --oneline |
| Create branch | git branch feature |
| Switch branch | git switch feature |
| Merge branch | git merge feature |
| Delete branch | git branch -d feature |
| Push | git push |
| Pull | git pull |
| Fetch | git fetch |
| Stash | git stash |
| Restore file | git restore file |
| Compare | git diff |
| Reset | git reset |
| Create tag | git tag v1.0.0 |
| Clean files | git clean -fd |
Final Thoughts
Git is an essential skill for every developer. You do not need to memorize every command. Instead, understand the purpose of each command and practice using them in your daily workflow.
Keep this guide as your Git reference. The more you use Git, the more natural these commands will become. With regular practice, you will be able to manage projects, collaborate with teams, and solve version control problems with confidence.