Git Commands from Beginner to Advanced: Complete Git Cheat Sheet for Developers
Learn Git commands from beginner to advanced level with practical examples. Master repository creation, branching, merging, rebasing, stashing, remote repositories, and advanced Git workflows.
Git Commands from Beginner to Advanced
Git is the most widely used version control system that helps developers track code changes, collaborate with teams, and manage software projects efficiently. This guide covers essential Git commands from beginner to advanced levels.
Beginner Git Commands
1. Check Git Version
git --version2. Configure Username
git config --global user.name "Your Name"3. Configure Email
git config --global user.email "you@example.com"4. Initialize a Repository
git init5. Clone a Repository
git clone <repository-url>6. Check Repository Status
git status7. Add Files to Staging Area
git add file.txtAdd all files:
git add .8. Commit Changes
git commit -m "Initial commit"9. View Commit History
git log10. Create a New Branch
git branch feature-branch11. Switch Branch
git checkout feature-branchOr:
git switch feature-branch12. Create and Switch Branch
git checkout -b feature-branch13. View Branches
git branch14. Merge Branches
git checkout main
git merge feature-branch15. Delete Branch
git branch -d feature-branch16. Add Remote Repository
git remote add origin <repository-url>17. Push Changes
git push origin main18. Pull Latest Changes
git pull origin main19. Fetch Updates
git fetch20. View Remote Repositories
git remote -vAdvanced Git Commands
21. Rebase Branch
git rebase main22. Interactive Rebase
git rebase -i HEAD~523. Cherry-Pick Commit
git cherry-pick <commit-hash>24. Stash Changes
git stash25. View Stashes
git stash list26. Apply Stash
git stash apply27. Pop Stash
git stash pop28. Reset Commit (Soft)
git reset --soft HEAD~129. Reset Commit (Hard)
git reset --hard HEAD~130. Revert Commit
git revert <commit-hash>31. View Differences
git diff32. View Staged Changes
git diff --staged33. Tag a Release
git tag v1.0.0Push tags:
git push origin --tags34. Show Reference Log
git reflog35. Clean Untracked Files
git clean -fdExpert-Level Git Commands
36. Bisect for Bug Detection
git bisect start
git bisect bad
git bisect good <commit-hash>37. Squash Commits
git rebase -i HEAD~538. Amend Last Commit
git commit --amend39. View Commit Graph
git log --oneline --graph --all40. Force Push (Use Carefully)
git push --force-with-lease41. Track Remote Branch
git checkout --track origin/feature-branch42. Find Author Information
git blame file.txt43. Search Commit History
git log --grep="bug fix"44. Archive Repository
git archive --format zip --output project.zip HEAD45. Remove File from Git Tracking
git rm --cached file.txtGit Fetch vs Git Pull
| Feature | Git Fetch | Git Pull |
|---|---|---|
| Purpose | Downloads latest changes from the remote repository | Downloads changes and merges them into the current branch |
| Changes to Local Branch | Does not modify your working branch | Automatically updates your current branch |
| Safety | Safer because you can review changes before merging | Faster but may create merge conflicts immediately |
Git Merge vs Git Rebase
| Feature | Git Merge | Git Rebase |
|---|---|---|
| Commit History | Preserves complete branch history | Creates a cleaner, linear history |
| New Commit | Creates a merge commit | Does not create a merge commit |
| Team Collaboration | Safer for shared branches | Better for personal feature branches |
Git Reset vs Git Revert
Feature Git Reset Git Revert History Impact Removes commits from history Creates a new commit that undoes changes Safe for Shared Branches No Yes Use Case Local undo operations Undo changes in public repositories
Git Workflow Summary
- Initialize or clone a repository.
- Create a feature branch.
- Make changes and stage files.
- Commit changes with meaningful messages.
- Push branch to remote repository.
- Create a pull request.
- Merge approved changes into the main branch.
- Pull latest updates regularly.
Frequently Asked Questions
What is Git and why is it used?
Git is a distributed version control system that helps developers track code changes, collaborate with teams, manage project history, and safely revert changes when needed.
What is the difference between Git Fetch and Git Pull?
git fetch downloads changes from a remote repository without merging them, while git pull downloads and automatically merges those changes into the current branch.
When should I use Git Rebase instead of Merge?
Use Git Rebase when you want a cleaner and linear commit history. Use Merge when you want to preserve the complete branch history and collaboration context.
How can I undo my last Git commit?
You can undo the last commit while keeping changes using: git reset --soft HEAD~1 To completely remove the commit and changes: git reset --hard HEAD~1
What is Git Stash used for?
Git Stash temporarily saves uncommitted changes so you can switch branches or work on another task without committing unfinished work.