Introduction:
Have you ever found yourself stuck or frustrated while using Git, thinking, “There’s got to be a better way to do this?” Trust me, you’re not alone. Git is a powerful tool, but without the right knowledge, it can sometimes feel overwhelming. That’s why today, I’m sharing a few Git commands that I wish I’d known earlier — they’ve completely changed the way I work.
Git is more than a tool — it’s a collaborator in your development process. Mastering it not only helps you work smarter but also ensures smoother collaboration with your team. Whether you’re managing solo projects or contributing to complex repositories, knowing the right commands can turn a frustrating experience into a smooth and efficient workflow.
By the end of this article, you can handle scenarios like reverting changes safely, exploring your project’s history effectively, and keeping your branches clean and organized.
If you prefer visual learning, check out the video instead.
1. Update Your Last Commit
Have you ever forgotten to include something in a commit and just pushed a new one to fix it? Sure, it works, but later, when you look back at those messy commits, you might feel a bit awkward — or worse, imagine someone else finding them and wondering who made the mess!
Example of a messy commit history:
33d6560 (HEAD -> master) Sign up form
df36560 fixed Button action
efd6560 Minor changes
Here’s a simple solution: use git commit --amend --no-edit
. This command lets you add any staged changes to your last commit without changing the commit message. If you want to update the message, just leave out the --no-edit
flag, and Git will prompt you to edit it.
This command comes in very handy when you catch a small mistake right after committing.
2. Undo The Last Commit
If you want to undo commits, reset your working directory, or move the commit history back to a specific point. That’s where the git reset
command comes in handy.
# Undo the last commit but keep changes in the staging area
git reset --soft HEAD~1
# Undo the last commit and unstage the changes
git reset --mixed HEAD~1
# Undo the last commit and discard changes
git reset --hard HEAD~1
Beginners often find git reset
and git revert
confusing since both are used to undo changes, but they work in different ways. Unlike git reset
, which can rewrite history, git revert
creates a new commit that undoes the changes while keeping the original commit intact in the history.
git revert <commit-hash>
When to Use Each:
git revert
: When working on a shared branch or with a team, and you want to undo changes without affecting the commit history.
git reset
: When you’re working on a local branch and need to clean up commits or undo changes without preserving history.
3. Recover Your Git History
Making mistakes is part of being human. Imagine you accidentally deleted a branch or commit and later realize you need it back? That’s where git reflog
comes to the rescue. It keeps a record of changes to HEAD
, letting you recover commits even after commands like git reset
.
# Find its reference
git reflog
# Output
efd6560 HEAD@{0}: reset: moving to HEAD~1
df36560 HEAD@{1}: commit: fixed Button action
# Restore the previous commit
git checkout df36560
4. Save and Restore Your Work
Imagine you’re working on a feature when you suddenly need to switch branches. Most of us end up making a quick commit to save our progress and then amend
it it later — messy and unnecessary.
What if there’s a better way? With git stash
, You can temporarily save your changes, switch branches, and reapply them later. Simply run git stash
before switching branches, and when you're ready, use git stash pop
to restore your changes.
It’s also a lifesaver when you’ve made changes on the wrong branch. For example, if you realize you’ve been working on branch_2
instead of branch_1
, No problem:
git stash
git switch branch_1
git stash pop
5. Find Bugs and Fix Issues
When something suddenly breaks in your project, it can be frustrating to go through all the commits to find the bug. That’s where git bisect
comes in. It helps you identify which commit introduced the bug by using a binary search. It’s a powerful, yet simple process that works like this:
- Start Bisecting: Tell Git which commit is "good" (working fine) and which is "bad" (contains the bug).
- Git Tests Commits: Git will check out a commit between the good and bad commits and ask you if it’s good or bad.
- Repeat: Keep testing the commits Git shows you, marking them as "good" or "bad."
- Find the Bug: Git narrows it down to the exact commit that introduced the bug.
# Start the bisect process:
git bisect start
# Mark the current (broken) commit as bad:
git bisect bad HEAD
# Mark the last known good commit:
git bisect good <commit-hash>
Now, Git picks a commit between the good and bad ones and asks if it’s “good” or “bad.” It keeps narrowing things down, and before you know it, you’ll find the bug.
# Mark it as bad if it contains the bug
git bisect bad
# Mark it as good if it's working fine
git bisect good
Once found, you can fix the issue and use git bisect reset
to end the process. It’s a quick way to track down bugs without testing every commit manually.
6. Rewrite Commit History
While you’re working on a feature, someone else might push code that’s useful for your task. To include their changes, you might merge master
into your feature branch. But this can leave your commit history cluttered with multiple master
to feature branch merges. That’s where the git rebase
command comes in handy.
Before pushing your code upstream, run below command. This powerful command allows you to adjust the commits in your branch, keeping your history clean and organized.
git rebase -i origin/master
You can even edit commit messages or squash commits. The best part? Git opens this in your editor, making it as simple as selecting an option to remove a commit.
7. Apply Commits Selectively
What if you only need a specific commit from another branch, but don’t want to merge the entire branch? That’s where git cherry-pick
comes in. It lets you select a single commit from one branch and apply it to another. For example, you might want to apply a bug fix from feature-branch
to main
without merging the whole branch.
git cherry-pick <commit-hash>
This way, you can get just the changes you need, right where you need them!
8. Tagging Your Commits
you’ve just pushed a major feature or a stable release in your project and want to mark it in the commit history. git tag
allow you to do exactly that. It creates a marker at a specific commit, often used to signify releases or milestones.
# To create a tag:
git tag v1.0
# To push the tag:
git push origin v1.0
9. Track Commit Changes
You're working on a project and notice a bug you didn't see before. Unsure when it was introduced, you should run git log
to check the commit history. It provides a detailed list of commits, including the commit hash, author, date, and message.
# You'll get a list of commits
git log
# Single-line summary of each commit
git log --oneline
Before committing, it’s always a good idea to double-check your staged changes, especially when you’ve made several updates. That’s where git diff
comes in handy.
git diff --staged
10. Clearing Untracked Files
Sometimes, your project gets messy with untracked files like build artifacts or log files. You want to remove them without affecting your tracked files. That’s where git clean
comes in — it removes the untracked files while keeping your tracked files safe.
git clean -f
Conclusion:
You’ve learned some of the most useful Git commands that enhance your workflow and keep your repositories organized. From recovering lost commits with git reflog
to cleaning up histories with git rebase
and marking milestones with git tag
, you now have the skills to handle various scenarios with confidence.
Next steps? Practice these commands in a test repository, explore advanced Git workflows, or dive into Git’s documentation to uncover even more features. With these commands, you’re ready to handle any Git challenge and collaborate like a pro. Keep exploring, and happy coding!
Top comments (0)