Git Commands Cheat Sheet: 15 Essential Git Commands Every Developer Must Know (Beginner to Advanced Guide)
Git Commands Cheat Sheet: 15 Essential Git Commands Every Developer Must Know
Git is one of the most powerful version control systems used by developers worldwide. Whether you’re working solo or collaborating in a team, mastering Git commands is essential for efficient code management.
In this guide, youโll learn 15 essential Git commands with examples, helping you streamline your development workflow and avoid common mistakes.
๐น 1. git clone (Copy a Repository)
The git clone command is used to download a remote repository to your local machine.
git clone https://github.com/user/project.git
๐ This creates a complete local copy of the project, including all files and commit history.
๐น 2. git status (Check Repository Status)
This command shows the current state of your working directory.
git status
๐ It helps you identify:
- Modified files
- Staged files
- Untracked files
๐น 3. git add (Stage Changes)
Before committing, you need to stage your changes.
git add file.txt
To add all files:
git add .
๐ Moves changes to the staging area.
๐น 4. git commit (Save Changes)
Commits save your staged changes with a message.
git commit -m "Added login feature"
๐ Think of it as creating a checkpoint in your project.
๐น 5. git push (Upload Code to Remote)
Push your local commits to a remote repository.
git push origin main
๐
origin= remote repositorymain= branch name
๐น 6. git pull (Get Latest Code)
Fetches and merges the latest changes from the remote repository.
git pull origin main
๐ Best practice: Always pull before starting work to avoid conflicts.
๐น 7. git fetch (Download Changes Only)
Downloads changes without merging.
git fetch
๐ Useful for safely reviewing updates before applying them.
๐น 8. git branch (Manage Branches)
List all branches:
git branch
Create a new branch:
git branch feature-login
๐ Helps in working on features independently.
๐น 9. git checkout (Switch Branches)
Switch to another branch:
git checkout feature-login
Create and switch:
git checkout -b feature-login
๐ Used for navigation and restoring files.
๐น 10. git merge (Combine Branches)
Merge one branch into another.
git checkout main
git merge feature-login
๐ Integrates feature code into the main branch.
๐น 11. git log (View Commit History)
Shows commit history.
git log
Short version:
git log --oneline
๐ Helps track changes and history.
๐น 12. git diff (See Code Changes)
Displays differences between files.
git diff
๐ Useful for reviewing changes before committing.
๐น 13. git reset (Undo Staging)
Remove a file from staging area:
git reset file.txt
๐ Does not delete changes, only unstages them.
๐น 14. git stash (Save Work Temporarily)
Save unfinished work:
git stash
Restore it later:
git stash pop
๐ Perfect when switching branches without committing.
๐น 15. git remote (Manage Remote Repositories)
View remote repositories:
git remote -v
Add a new remote:
git remote add origin https://github.com/user/project.git
โ ๏ธ Important Git Workflow Tip (Real-World Practice)
๐ Before pushing code:
- Always pull from
pencil_dev_env - Only push to
basant_dev - Add proper tags when required
This ensures:
- No merge conflicts
- Clean collaboration
- Proper version tracking
๐ Best Practices for Using Git
- โ Commit frequently with meaningful messages
- โ Pull before starting work
- โ Use branches for new features
- โ Avoid pushing directly to main
- โ
Use
git fetchfor safe updates - โ Keep your repository clean
๐ฏ Conclusion
Mastering these Git commands will significantly improve your development workflow. Whether you’re a beginner or an experienced developer, these commands form the foundation of efficient version control.
Start practicing today and make Git your best development companion ๐