Version control is the foundation of every modern development workflow — and Git is the most popular and powerful tool for it.
Whether you’re a beginner learning your first commands or a Full Stack engineer managing multiple branches, mastering Git will make your life easier and your projects cleaner.
In this article, we’ll cover 21 essential Git commands every developer should know — with examples and when to use them.
1. Initialize a New Repository
git init
Creates a new local Git repository in your project directory.
A hidden .git folder is created to track your code history.
2. Clone an Existing Repository
git clone <repository_url>
Copies a remote repository (like from GitHub or GitLab) to your local system.
3. Check Repository Status
git status
Displays the current working state of your project — modified, staged, or untracked files.
4. Add Files to Staging Area
git add <file_name>
Stage specific files, or use:
git add .
to stage all modified files.
5. Commit Changes
git commit -m "Add user authentication feature"
Saves your staged changes with a descriptive message.
6. View Commit History
git log
Shows commit history.
Use this for a concise view:
git log --oneline
7. Create a New Branch
git branch feature/login
Creates a new branch without switching to it.
8. Switch Between Branches
git checkout feature/login
Switches to the specified branch.
9. Merge Branches
git merge <branch_name>
10. Push Changes to Remote
git push origin <branch_name>
Uploads your commits to a remote repository branch.
11. Pull Changes from Remote
git pull
Fetches and integrates changes from a remote branch to your local branch.
12. Compare Changes
git diff
Shows differences between working directory and staging area.
13. Remove Files from Repository
`git rm
git commit -m "Remove old configuration file"
`
Deletes a tracked file and commits the removal.
14. Undo Changes
Undo unstaged changes:
git checkout -- <file_name>
Undo staged changes:
git reset <file_name>
Undo last commit but keep changes:
git reset --soft HEAD~1
15. View Remote Repositories
git remote -v
Lists connected remote repositories and their URLs.
16. Stash Temporary Changes
git stash
Saves uncommitted changes temporarily.
To retrieve later:
git stash pop
List all stashes:
git stash list
17. Tag a Commit (For Releases)
git tag v1.0.0
 git push origin v1.0.0
Tags mark specific commits — often used for versioning releases.
18. Rename a Branch
git branch -m old_branch_name new_branch_name
Renames your local branch.
19. Delete a Branch
Delete locally:
git branch -d feature/login
Delete remotely:
git push origin --delete feature/login
20. Revert a Commit
git revert <commit_id>
Creates a new commit that undoes the changes of a previous commit without altering history.
21. Configure User Information
git config --global user.name "github_username"
 git config --global user.email "github_mail_Id"
Sets up your Git username and email (used for commit metadata).
Final Thoughts
Git isn’t just a version control tool — it’s a developer’s time machine.
It helps you experiment fearlessly, collaborate effectively, and always recover from mistakes.
 

 
    
Top comments (0)