Mastering Git can transform your workflow and boost productivity. Many developers stick to basic commands like commit
, push
, and pull
, but Git’s command-line interface offers powerful features that are often overlooked. In this article, we’ll explore 20 Git tricks that will enhance your development experience and make you feel like a Git pro!
1. Interactive Staging with git add -p
Interactive staging allows you to stage specific changes within a file rather than the entire file. This is particularly helpful when you’re working on multiple tasks in the same file and want to commit only certain parts.
git add -p
Why it’s useful: Keeps commits clean by letting you selectively stage changes.
2. Undo the Last Commit with git reset --soft HEAD~1
Made a mistake on the last commit? With git reset
, you can remove the last commit but keep the changes staged.
git reset --soft HEAD~1
Why it’s useful: Allows you to revise your last commit without losing any changes.
3. Amend the Last Commit with git commit --amend
Instead of creating a new commit, you can amend the previous one if you forgot to add changes or want to edit the commit message.
git commit --amend
Why it’s useful: Ensures your commit history remains concise.
4. View Commit Log Graph with git log --oneline --graph
A visual graph of your Git history is invaluable for understanding branches and merges.
git log --oneline --graph
Why it’s useful: Offers a quick overview of your project’s branch history.
5. Reflog: Your Git Time Machine with git reflog
git reflog
is essential for finding old commits and recovering lost work.
git reflog
Why it’s useful: Helps track down deleted commits, making recovery easy.
6. Stash Uncommitted Changes with git stash
Use git stash
to temporarily save changes when you need to switch branches.
git stash
# To reapply, use:
git stash pop
Why it’s useful: Great for saving work-in-progress changes without committing them.
7. Stash Only Certain Files with git stash push -p
You can stash specific changes instead of the whole working directory by using interactive mode with -p
.
git stash push -p
Why it’s useful: Provides more control over what gets stashed.
8. Create and Switch to a New Branch with git switch -c
You can quickly create a new branch and switch to it in one command.
git switch -c new-branch
Why it’s useful: Streamlines branch creation and switching.
9. View All Branches with Last Commit Message with git branch -v
This command displays all local branches along with their last commit message.
git branch -v
Why it’s useful: Helps keep track of recent changes across branches.
If you found my content helpful or interesting, and you’d like to show your appreciation, why not buy me a coffee? It’s a small gesture that goes a long way in helping me keep creating more content for you.
Just click the button below to support:
10. Delete Local Branches with git branch -d
Use this command to delete branches locally once they’re no longer needed.
git branch -d old-branch
Why it’s useful: Keeps your workspace organized.
11. Rename the Current Branch with git branch -m
If you need to rename the current branch, -m
lets you do it without switching branches.
git branch -m new-name
Why it’s useful: Allows you to rename branches on the go.
12. Fetch and Prune with git fetch -p
fetch -p
removes local references to branches deleted from the remote.
git fetch -p
Why it’s useful: Keeps your branch list clean by removing deleted remote branches.
13. Resolve Conflicts with git mergetool
This command opens your default merge tool to help you resolve conflicts visually.
git mergetool
Why it’s useful: Makes handling merge conflicts easier.
14. Squash Commits with git rebase -i
When you want to consolidate multiple commits into one, interactive rebasing is the way to go.
git rebase -i HEAD~3
Why it’s useful: Keeps your history clean by combining multiple commits.
15. Show Detailed File Change History with git log -p filename
If you want to see how a file changed over time, -p
will show diffs for each commit.
git log -p filename
Why it’s useful: Great for auditing changes in a particular file.
16. Restore a Deleted File with git checkout
You can restore a deleted file from the last commit by checking it out from HEAD.
git checkout HEAD -- deleted-file
Why it’s useful: Saves you from redoing work if you accidentally delete a file.
17. Diff Between Branches with git diff branch1..branch2
This command shows the differences between two branches, which is useful for understanding what’s changed.
git diff branch1..branch2
Why it’s useful: Allows you to see what’s different between branches before merging.
18. Add Aliases for Frequent Commands with git config
You can simplify common commands by setting up aliases in Git.
git config --global alias.co checkout
git config --global alias.br branch
Why it’s useful: Saves you time by shortening commands you use frequently.
19. Find Author Contributions with git shortlog -s -n
This command shows each contributor’s number of commits in the repository.
git shortlog -s -n
Why it’s useful: Great for project summaries and acknowledging contributors.
20. Find the Creator of Each Line with git blame
git blame
tells you who last modified each line in a file, which can be helpful for debugging.
git blame filename
Why it’s useful: Quickly identify who last worked on specific parts of a file.
Conclusion
Mastering these Git command-line tricks will streamline your workflow, help you manage changes more effectively, and elevate your productivity. The more comfortable you are with Git, the more you can focus on what matters most: writing great code. Whether you're a beginner or an experienced developer, these commands are bound to take your Git skills to the next level.
That's all for today.
And also, share your favourite web dev resources to help the beginners here!
Connect with me:@ LinkedIn and checkout my Portfolio.
Explore my YouTube Channel! If you find it useful.
Please give my GitHub Projects a star ⭐️
Thanks for 32075! 🤗
This is a submission for the 2024 Hacktoberfest Writing challenge: Contributor Experience
Top comments (1)
Subscribe to my YouTube Channel if you find it helpful! Subscribing is free, and it will motivate me to stay active here. 😊