24 Essential Git Command-Line Tips for Developers
This guide presents 24 practical Git command-line techniques—including global configuration, undoing commits, interactive rebasing, stash management, and reflog inspection—to help developers gain deeper control, improve workflow efficiency, and maintain a clean project history.
Git is an essential version control tool for developers. While graphical user interface (GUI) tools can simplify some tasks, mastering the Git command line provides deeper control, flexibility, and speed. Below are 24 Git command-line techniques every developer should know to optimize their workflow.
1. Set Global Configuration
Ensure your commits are tagged with the correct identity.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"💡 Tip: Use --local instead of --global for project‑specific configuration.
2. Undo the Last Commit (Keep Changes)
If you made a mistake in the most recent commit, you can undo it while preserving the changes.
git reset --soft HEAD~13. Amend the Last Commit
Forgot to include a change or want to update the commit message?
git add .
git commit --amend -m "Updated commit message"This updates the previous commit without creating a new one.
4. Stash Uncommitted Changes
Need to switch branches quickly without committing?
git stash💡 Later you can restore the stashed changes with:
git stash pop5. View Commit History Graphically
Visualizing the commit history makes understanding project state easier.
git log --graph --oneline --all6. Change Commit Author
Modify the author of the most recent commit.
git commit --amend --author="New Author
"7. Check Staged Changes Diff
Use git diff to compare file differences at various stages.
git diff --stagedThis shows changes that are staged but not yet committed.
8. Use bisect to Find Bugs
Employ git bisect to locate the commit that introduced a bug.
git bisect start
git bisect bad # current commit is bad
git bisect good
# a known good commit9. Keep History Clean with Interactive Rebase
Rebase rewrites your commit history for better clarity.
git rebase -i HEAD~3This lets you edit, squash, or reorder the last three commits.
10. Cherry‑Pick Specific Commits
Want to bring a particular commit from another branch?
git cherry-pick11. List All Branches (Local and Remote)
git branch -a12. Clean Untracked Files and Directories
Quickly delete files that Git is not tracking.
git clean -fd💡 Use -n for a dry run to preview what would be removed.
13. Track Upstream Branch
Keep a local branch in sync with its remote counterpart.
git branch --set-upstream-to=origin/main14. Merge Commits with Interactive Rebase
Combine multiple commits into a single one.
git rebase -i HEAD~n # replace 'n' with the number of commits15. View Files in a Specific Commit
git show
:path/to/file16. Edit .gitignore After a Commit
If you forgot to ignore certain files, update the .gitignore file.
echo "node_modules/" >> .gitignore
git rm -r --cached node_modules/
git commit -m "Update .gitignore"17. Revert a Pushed Commit
Undo the changes introduced by a specific commit without rewriting history.
git revert18. Fetch Metadata Only
Want to avoid pulling the entire repository?
git fetch --dry-runThis shows what would be fetched without actually downloading data.
19. Trace the Origin of a Line
Find out who authored a particular line in a file.
git blame path/to/file20. Reset a File to Its Previous Commit State
Discard local changes to a specific file.
git checkout -- path/to/file21. Reset to a Specific Commit
Use git reset --hard [commit-hash] to move the current branch to a given commit, discarding all later changes. Note that uncommitted work will be permanently lost.
git reset --hard [commit-hash]22. Add a Message to a Stash
Provide a descriptive message for a stash to make later identification easier.
git stash -m 'library update'23. Create a Tag for a Specific Commit
Tagging helps with version management and milestone tracking.
git tag your_tagThis creates a tag named your_tag on the current commit.
24. View the Reflog to Track All Operations
Run git reflog to see a log of all reference updates, including discarded commits, which aids in recovery.
git reflogThese 24 Git command-line tips can make your development process smoother, whether you work solo or in a team. While GUI tools offer convenience, mastering the Git CLI gives you greater control over your workflow. Try these commands to boost your Git proficiency!
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.