Learning to use Git effectively isn’t always straightforward—especially for many of us developers. Git has a vast set of commands, and honestly, no one knows them all. However, some essential commands like git pull
and git push
are used regularly.
While Git proficiency comes with experience, learning key commands and best practices early can significantly improve your workflow and make you a better developer. In this guide, I’ll share some Git commands I use regularly, starting with the basics and moving to more advanced ones. I hope it helps anyone reading this.
🧱 Basic Git Commands
git init
Initialize a new Git repository in your current directory.
git status
Shows the current state of your working directory and staging area.
Tip: Run this often to check what’s staged, modified, or untracked.
git clone <repository>
Downloads a remote repository to your local machine.
Example: git clone https://github.com/user/repo.git
git add .
Stage all changes in your working directory.
git add <filepath>
Stage a specific file for commit.
git commit -m "message"
Commit staged changes with a descriptive message.
git log
View the commit history.
git checkout <branch>
Switch to an existing branch.
Tip: Consider using git switch <branch>
for a more intuitive experience.
git checkout -b <branch>
Create and switch to a new branch.
git branch
List all branches.
git branch -D <branch>
Delete a branch.
git branch -M <old> <new>
Rename a branch from <old>
to <new>
.
git pull
Fetch and merge changes from the remote branch.
Tip: Use git pull --rebase
to avoid unnecessary merge commits.
git push
Push your local commits to the remote repository.
git fetch
Retrieve changes from the remote without merging.
git merge <branch>
Merge another branch into your current branch.
🚀 Advanced Git Commands
These are often extensions of basic commands with extra arguments or specific use cases.
git add -p <filepath>
Stage parts of a file instead of the whole file. It opens an interactive mode where you select specific "hunks" (sections of code) to stage.
Common options:
-
y
: stage this hunk -
n
: don’t stage this hunk -
q
: quit (don’t stage this or remaining hunks) -
a
: stage this hunk and all later hunks -
d
: don’t stage this hunk or later ones -
s
: split the hunk into smaller chunks -
e
: manually edit the hunk -
?
: show help
Mini story: When I first started, I pushed large commits with unrelated changes. Using
git add -p
helped me group changes logically, making code review easier.
git commit --amend
or git commit -a --no-edit
Add new staged changes to the previous commit, especially if related.
Warning: Only use this for commits not yet pushed to remote repositories.
git log branchA..branchB
Shows commits in branchB
not in branchA
. Combine with --oneline
for readability:
git log --oneline branchA..branchB
git diff branchA..branchB
Shows code changes in branchB
that aren’t in branchA
.
git diff --name-only branchA..branchB
Shows filenames only that differ between two branches.
git cherry-pick <commit1> <commit2>
Apply specific commits from another branch onto your current branch. Useful when releasing select features or fixes.
git stash
Temporarily saves uncommitted changes to work on something else.
git stash push -m "WIP on feature X"
git stash pop
git rebase -i
Interactive rebase lets you clean up commit history—squash, reorder, or remove commits.
✅ Best Practices and Advice
1. Commit Often, but Meaningfully
Keep commits small and focused on a single purpose.
2. Write Clear Commit Messages
Explain the “why,” not just the “what.”
Good: Fix login error when password is empty
Bad: fix bug
3. Never Rebase Shared Branches
Rebasing rewrites history; only do it on local branches not pushed to others.
4. Use Branches for Features and Fixes
Keep your main branch clean. Create new branches for features or bug fixes.
5. Pull Often, Push When Ready
Regularly run git pull --rebase
to minimize conflicts. Push only when your code is ready.
6. Clean Up Before Merging
Use git rebase -i
or squash commits to tidy history before merging.
Tip: GitHub's Squash and merge feature can do this for you automatically during pull request merges.
7. Learn by Breaking (Locally)
Experiment freely. You can recover lost commits using git reflog
.
8. Use .gitignore
Wisely
Exclude environment files, dependencies, or secrets to keep your repo clean.
Useful Resources
💬 Got Questions or Tips?
I wrote this guide to share how I use Git day-to-day and to help others who might be feeling overwhelmed by all the commands. If you have questions, feedback, or your own Git tips, feel free to drop them in the comments. I'd love to hear how you use Git or what helped you learn it!
Final Thoughts
This is not an exhaustive list of Git commands, but it includes the ones I use the most. Git has a steep learning curve at first, but understanding just a handful of useful commands and when to use them can greatly improve your workflow and confidence.
Top comments (0)