Once you’ve mastered the essentials of Git and learned to manage branches and undo changes, it's time to level up. Git has a host of advanced commands and techniques that can make your workflow smoother, your project history cleaner, and your debugging process faster.
In this final part, we'll explore powerful commands like git cherry-pick, git rebase, and git bisect, as well as useful tips to optimize your Git usage. By the end, you'll have the skills to handle complex development tasks with confidence and finesse.
1. git cherry-pick
Sometimes you need to apply a specific commit from one branch to another without merging the entire branch. That’s where git cherry-pick
comes in. This command allows you to pick a commit and apply it to your current branch.
Example:
git cherry-pick abc1234 # Applies commit abc1234 to your current branch
2. git rebase
Rebasing helps you clean up your commit history by moving or combining commits. When you use git rebase, you’re reapplying your commits on top of another branch, making your history linear and easier to read.
Examples:
git rebase main # Reapplies your current branch's commits on top of 'main'
git rebase -i HEAD~3 # Opens an interactive rebase for the last 3 commits to modify, reorder, or squash
3. git reflog
git reflog is your safety net when you’ve lost track of a commit or need to find a recent change. It records all movements of HEAD, even those that don’t appear in git log.
Example:
git reflog # Shows a log of where HEAD has been
git checkout <commit-hash> # Restores a lost commit using its hash
4. git bisect
Debugging can be daunting, especially when you're trying to track down which commit introduced a bug. git bisect
helps you find the problematic commit by using a binary search through your commit history.
Example:
git bisect start # Starts the bisect process
git bisect bad # Marks the current commit as bad
git bisect good <commit-hash> # Marks a known good commit
Git will check out a commit halfway between the good and bad commits, helping you narrow down the issue until the exact commit is found.
5. git clean
Sometimes you need to clear untracked files from your working directory, whether to clean up build files or reset your environment. git clean
does this safely.
Examples:
git clean -n # Preview what will be deleted
git clean -f # Force deletes untracked files
git clean -fd # Deletes untracked files and directories
When working on a feature or fix, you might end up with multiple small commits that are better combined into one. Squashing commits can help maintain a cleaner commit history, making it easier for others (and yourself) to review changes.
What Does Squashing Do?
Squashing takes several commits and combines them into a single commit. This is useful when you want to merge multiple work-in-progress (WIP) commits into one meaningful commit before merging a branch into main or master.
In interactive rebase (squash mode), the order of commits in the editor is shown from oldest at the top to newest at the bottom. When you mark commits with squash (or s), they will be combined with the commit directly above them. This means that the final squashed commit will keep the order intact from oldest to newest, with all changes applied in sequence as they were originally made.
How to Squash Commits:
Start an Interactive Rebase:
Use git rebase -i
followed by the commit range you want to squash. For example, to squash the last 3 commits:
git rebase -i HEAD~3
Edit the Rebase Plan:
An editor window will open, showing the commits in the selected range. You'll see something like this:
pick abc1234 First commit
pick def5678 Second commit
pick ghi9101 Third commit
Change the lines from pick to squash (or s) for the commits you want to combine:
pick abc1234 First commit
squash def5678 Second commit
squash ghi9101 Third commit
Save and Close the Editor:
Save the changes and close the editor. Git will then combine the selected commits.
Edit the Commit Message:
Another editor window will pop up, allowing you to create a single commit message for the squashed commits. Adjust the message as needed, then save and close the editor.
Example Result:
You’ll now have a single, unified commit that combines the changes from the previous individual commits.
When you mark commits for squashing in an interactive rebase, the process works by combining each subsequent commit into the previous one.
In the rebase plan:
pick abc1234 First commit
squash def5678 Second commit
squash ghi9101 Third commit
How it works:
def5678
(Second commit) is merged into abc1234
(First commit).ghi9101
(Third commit) is then merged into the combined commit of abc1234 and def5678.This means that all changes from def5678 and ghi9101 are incorporated into abc1234, creating a new single commit with a new hash, such as mno7890. The original separate commits no longer exist in the history, and the result is a cleaner, unified commit that summarizes all combined changes.
This approach helps keep your commit history clean and easy to read, making collaboration and code reviews simpler.
When to Use Squashing:
Mastering advanced Git commands is only part of the equation. Here are tips to keep your workflow smooth and efficient:
1. Use Aliases for Common Commands
Typing long Git commands repeatedly can be tiring. Set up aliases for commonly used commands to save time.
Example:
git config --global alias.co checkout # Creates an alias 'git co' for 'git checkout'
git config --global alias.st status # Creates an alias 'git st' for 'git status'
2. Regularly Prune Local Branches
Old branches can clutter your repository. Use git branch -d
to delete merged branches and git fetch --prune
to clean up remote-tracking branches.
3. Keep Your Commits Clear
A clean commit history makes your project easier to navigate. Use meaningful commit messages and squash related commits before merging to reduce noise.
4. Choose Between Merge and Rebase Wisely
For a linear history, use git rebase. For keeping the full context of branch merges, use git merge. Just be cautious with rebasing shared branches, as it rewrites commit history.
By mastering these advanced Git commands and productivity tips, you’ll gain the confidence to handle even the most complex workflows. Whether you're cherry-picking specific changes, finding the source of a bug, or maintaining a clean commit history, these tools are invaluable.
Keep practicing and applying these techniques, and your Git skills will help you work faster and smarter — and may even rescue you when things go wrong.