Git
GitHub
Best Practices
Tutorial
Beginner
Ask anything about this article
Hi! I've read this article.
What would you like to know?
@farhan
Git is the most important tool in a developer's toolkit, yet most developers only know commit, push, and pull. Here are the commands that will make you a Git power user.
bash# Initialize a new repository
git init
# Clone a repository
git clone https://github.com/user/repo.git
# Check status
git status
# Stage all changes
git add .
# Stage specific files
git add src/component.tsx
# Commit with message
git commit -m "feat: add user authentication"
# Push to remote
git push origin main
bash# Create and switch to a new branch
git checkout -b feature/user-auth
# List all branches (local and remote)
git branch -a
# Switch branches
git checkout main
# Delete a branch (local)
git branch -d feature/user-auth
# Delete a branch (remote)
git push origin --delete feature/user-auth
bash# Undo last commit but keep changes staged
git reset --soft HEAD~1
# Undo last commit and unstage changes
git reset --mixed HEAD~1
# Completely undo last commit (DANGEROUS - loses changes)
git reset --hard HEAD~1
# Undo a specific commit (creates a new commit)
git revert abc1234
# Discard all local changes
git checkout -- .
# Unstage a file
git reset HEAD file.txt
bash# Save work in progress
git stash
# Save with a message
git stash save "WIP: working on login feature"
# List all stashes
git stash list
# Apply the latest stash
git stash pop
# Apply a specific stash
git stash apply stash@{2}
# Drop a stash
git stash drop stash@{0}
bash# Merge (creates a merge commit)
git checkout main
git merge feature/auth
# Rebase (replay commits on top of main)
git checkout feature/auth
git rebase main
# Interactive rebase (squash, edit, reorder commits)
git rebase -i HEAD~5
bash# Apply a specific commit from another branch
git cherry-pick abc1234
# Cherry-pick without committing
git cherry-pick --no-commit abc1234
bash# Pretty log
git log --oneline --graph --all --decorate
# Search commit messages
git log --grep="fix bug"
# See who changed each line
git blame src/app.tsx
# Show changes in a commit
git show abc1234
# Diff between branches
git diff main..feature/auth
feat: add user registration endpoint
fix: resolve null pointer in auth middleware
docs: update API documentation
refactor: extract validation logic
test: add unit tests for payment service
chore: update dependencies
Master these commands, and you'll never be scared of Git again.