Useful Git Commands
Essential Git Commands for Developers
Here’s a collection of commonly used Git commands that every developer should know.
Basic Commands
Getting Started
git init # Initialize a new Git repository
git clone <url> # Clone a repository from remote
git status # Check status of working directory
Daily Workflow
git add . # Stage all changes
git commit -m "Your message" # Commit staged changes
git push origin main # Push to remote repository
Branching
git branch # List all branches
git branch <name> # Create new branch
git checkout <name> # Switch to branch
git merge <branch> # Merge branch into current branch
Advanced Commands
Stashing
git stash # Temporarily save changes
git stash pop # Apply saved changes`
History
git log # View commit history
git blame <file> # See who changed what in a file
Tips and Tricks
-
Undo Last Commit
git reset --soft HEAD~1
-
Amend Last Commit
git commit --amend
-
Force Push (use carefully!)
git push -f origin main
Remember to always be careful with commands that modify history or force operations!
Best Practices
- Always pull before pushing
- Write clear commit messages
- Create branches for new features
- Regular commits with smaller changes
Conclusion
These Git commands will help you manage your code effectively. Practice them regularly to become more comfortable with version control.