Git Best Practices for Senior Engineers: Rebase, Cherry-pick, Amend and More

Day 2 of #30DaysOfDevOps — Post 2 Once you know basic Git, the next step is learning how to use it like a pro. These are the commands that separate junior engineers from senior ones. 1. Git Rebase Merge creates a new commit tying two branches together. Rebase moves your entire branch on top of another — keeping history straight and clean. git checkout feature/api-refactor git rebase main Your commits get replayed one by one on top of main. No messy merge commits cluttering the log. 2. Interactive Rebase Before merging, clean up your commit history so reviewers don't see every "fix typo" and "oops" commit. git rebase -i HEAD~4 In the editor: - squash — combine commits into one - reword — rename a commit message - drop — remove a commit entirely A clean history is a sign of a professional engineer. 3. Git Cherry-pick Need just one commit from another branch? Don't merge the whole thing. git checkout release/v2.1 git cherry-pick 7d3f91b That single commit is now applied to your current branch. Perfect for backporting a hotfix. 4. Amending Commits Caught a mistake right after committing? Fix it before pushing. Wrong message: git commit --amend -m "Add rate limiting to auth endpoint" Forgot a file: git add middleware/ratelimit.js git commit --amend --no-edit Rule: never amend a commit that's already on a shared branch. 5. Reset vs Revert Two ways to undo — very different consequences. git reset --soft HEAD~1 — undo commit, keep changes staged git reset --mixed HEAD~1 — undo commit, keep changes unstaged git reset --hard HEAD~1 — undo commit and wipe all changes git revert HEAD — create a new commit that undoes the previous one On shared branches, always use revert. Reset rewrites history — dangerous for teammates. 6. Git Stash Need to switch branches but not ready to commit? Stash your work. git stash git checkout main Come back later and restore it: git stash pop You can have multiple stashes and name them too: git stash save "half-done login validation" 7. Git Hooks Hooks run scripts automatically at key Git events. A pre-commit hook can enforce code quality before anything gets committed. cd .git/hooks nano pre-commit #!/bin/bash echo "Running lint check..." npm run lint if [ $? -ne 0 ]; then  echo "Lint failed. Commit blocked."  exit 1 fi chmod +x pre-commit Now no one on your team can commit broken code. 8. Challenges for Today 1. Rebase a feature branch on top of main and verify the log looks linear. 2. Use interactive rebase to squash 4 commits into a single meaningful commit. 3. Cherry-pick a bug fix commit from one branch and apply it to another. 4. Practice all three reset modes and observe how your working directory changes. Drop your solutions or questions in the comments. #DevOps #Git #GitHub #30DaysOfDevOps #LearningInPublic #DevOpsEngineer #CloudComputing

To view or add a comment, sign in

Explore content categories