Git is an indispensable tool in modern software development. While knowing the basics may seem sufficient, advanced commands provide significant time savings and substantially improve workflow in complex projects. In this guide, we explore advanced Git commands that will strengthen your daily workflow.
Git Rebase: For a Clean History
Rebase is one of the most powerful ways to organize commit history:
Basic Rebase
git checkout feature-branch
git rebase main
This command reapplies your feature branch commits on top of the latest state of the main branch, resulting in a linear and clean history.
Interactive Rebase
git rebase -i HEAD~5
This allows you to edit the last 5 commits:
- pick: Keep the commit as is
- squash: Merge with the previous commit
- reword: Change the commit message
- edit: Modify the commit contents
- drop: Delete the commit
Rebase should be used carefully on shared branches. Rebasing published commits can break your teammates' work.
Git Cherry-Pick: Selective Commit Transfer
Cherry-pick is used to move a specific commit to another branch:
git checkout main
git cherry-pick abc1234
Common Use Cases
- Moving an urgent bug fix to the release branch
- Retrieving a commit made on the wrong branch
- Selectively including specific features in different versions
Handling Conflicts
git cherry-pick --continue # After resolving the conflict
git cherry-pick --abort # To cancel the operation
Git Bisect: Your Bug-Hunting Assistant
Bisect uses a binary search algorithm to find which commit introduced a bug:
git bisect start
git bisect bad # Current commit is broken
git bisect good v2.0 # v2.0 tag was working
Git automatically checks the commits in the middle. By saying "good" or "bad" at each step, you reach the commit that introduced the bug.
Automated Bisect
git bisect start HEAD v2.0
git bisect run ./test-script.sh
You can fully automate the process with a test script.
Git Stash: Temporary Storage
Stash is used to temporarily save changes you are working on:
Basic Usage
git stash # Save changes
git stash pop # Restore and remove from list
git stash apply # Restore but keep in list
Advanced Stash
- Named stash:
git stash push -m "WIP: login form" - Stashing specific files:
git stash push -p - Stash list:
git stash list - Applying a specific stash:
git stash apply stash@{2}
Git Reflog: Recovering Lost Commits
Reflog records all movements of HEAD and is a lifesaver for recovering seemingly "lost" commits:
git reflog
Recovery Scenarios
- Restoring an accidentally deleted branch
- Returning to a previous state after a mistaken reset
- Finding lost commits
git checkout -b recovery-branch abc1234
Reflog retains data for 90 days by default. Check the reflog before panicking.
Bonus Tips
Git Worktree
To work on multiple branches simultaneously:
git worktree add ../feature-branch feature-branch
Code Archaeology with Git Blame
To see who changed a line and when:
git blame file.cs
git blame -L 10,20 file.cs # Only lines 10-20
Useful Git Aliases
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.st "status -sb"
git config --global alias.co "checkout"
Best Practices
- Make small, meaningful commits: Each commit should contain a single logical change
- Write descriptive commit messages: Instead of "Fix bug," use "Fix null reference error in user login"
- Use a feature branch strategy: Separate branches for each feature or fix
- Rebase regularly: Keep your feature branch up to date with main
- Avoid force push: Never use
--forceon shared branches
Conclusion
Learning Git's advanced commands significantly increases your developer productivity. At BUZ Yazilim, we actively use these commands in our teams and offer Git workflow consulting to our clients. Feel free to reach out with any questions.