With Hacktoberfest in full swing, it's the perfect opportunity to take your git skills to the next level. In this article, we'll dive deeper into some advanced git commands that will help you debug, clean, and manage your repositories more efficiently.
1. Git Bisect: Pinpointing the Bug
Main article: Git Binary Search
One of the most powerful yet underused features in git is git bisect
. When faced with a bug in your code, git bisect
helps identify the commit that introduced the issue by performing a binary search through your commit history.
Starting Git Bisect
To begin the bisecting process:
git bisect start
Mark the commit where the bug is present as bad
and the last known working commit as good
:
git bisect bad <commit-hash>
git bisect good <commit-hash>
Git will check out a commit halfway between the two, and you’ll need to test if the issue is present. Depending on the outcome, mark the commit:
git bisect bad # if the issue is present
git bisect good # if the issue is resolved
Continue this process until you find the exact commit that introduced the bug.
Ending the Bisect Process
Once you’ve identified the problematic commit, you can end the process with:
git bisect reset
2. Git Clean: Clearing Your Working Directory
Sometimes your working directory becomes cluttered with untracked files that aren't part of your git repository. This is where git clean
comes in handy. It removes untracked files and directories, allowing you to keep your workspace clean.
Check What Would Be Removed
Before removing anything, it’s a good idea to see what files would be deleted:
git clean -n
This will give you a dry run and list the files and directories that will be removed.
Remove Untracked Files
To remove the untracked files, use the -f
(force) flag:
git clean -f
If you want to remove directories as well, use the -d
flag:
git clean -fd
3. Rebasing: Clean Up Your History
Rebasing is putting your branch on top of the current master branch. It allows you to do a fast-forward merge.
git rebase master
If you want to rebase onto the latest version of the remote master branch:
git pull --rebase origin master
To learn more about different merging strategies, take a look at the following articles:
- Git: Merge vs Rebase: The Three Types of Merge
- Git: Merge vs Rebase and Where to Use Them
- Git Merge vs Rebase vs Squash: On Squash Merge and When to Us It
4. Cherry-Picking: Apply Specific Commits
Cherry-picking is perfect when you want to apply the changes from a specific commit to your current branch without merging an entire branch.
git cherry-pick <commit-hash>
This command picks the specified commit and applies it on top of your current branch. To get the commit hash, you can do a git log
on the branch containing the commit.
5. Git Reset vs Revert: Fixing Mistakes
Both git reset
and git revert
are used to undo changes, but they behave differently.
Git Reset
git reset
allows you to move your HEAD
pointer back to a previous commit. You can choose to keep your working directory changes or discard them:
-
--soft
: Keeps changes in the staging area. -
--hard
: Removes everything, including uncommitted changes.
Example of a soft reset:
git reset --soft <commit-hash>
Example of a hard reset:
git reset --hard <commit-hash>
If you want to move one commit back, i.e. undo the last commit, you can do:
git reset --hard HEAD~1
After doing a git reset
, you need to push with --force-with-lease
, as it has changed history:
git push --force-with-lease
You can do a git reset
on your feature branch, but never do it on the master branch, instead, use git revert
.
Git Revert
git revert
, creates a new commit that undoes the changes made by a previous commit. It is safer when working in a shared repository, as it doesn't rewrite the history.
git revert <commit-hash>
Final Words
These advanced git commands—git bisect
, git clean
, rebase
, and cherry-pick
—will help you maintain a clean project history and improve your workflow during Hacktoberfest. Don't be afraid to experiment with them and integrate them into your daily git usage.
For more on advanced git practices, check out these additional resources:
Happy hacking!
Top comments (0)