Skip to content
Felipe Reducino
All posts

Reviewing a teammate's branch without losing your own work

How `git stash` lets you pause what you're doing, test a pull request locally, and come back to exactly where you left off.

Felipe ReducinoUpdated

I believe that sharing practical, real examples from my day-to-day work as a developer is a great way to help people who are facing the same problem I did, or who will almost certainly run into something similar. To keep the quality of a real-world codebase high, I've come to see a well-done code review as crucial. But I mean a code review that goes beyond reading the diff in the pull request your teammate opened. To go further, I usually check out my teammate's branch and run the code locally, so I can quickly test the feature, fix or whatever they're working on.

The problem

  • I'm also working on a task of my own, and I have a bunch of modified files.

  • I don't want to commit and push something I haven't finished yet.

  • Git won't let me switch branches if the files I changed would be overwritten by the versions on the other branch (such as main, develop or feat/teammate-branch). So how do you do the kind of code review I described above? The answer is: Git.

The solution

On your branch, feat/my-branch, run:

$bash git stash push -m "WIP: work in progress"

Make the message meaningful for the work you're in the middle of. Your future self will thank you.

Now you can move freely between branches, and review and test your teammate's work locally. When you're done, go back to your branch:

$bash git checkout feat/my-branch

And this is where the magic happens:

$bash git stash pop

All your modified files come back, with nothing lost, so you can safely pick up right where you left off.

One caveat

This isn't the one true way. I know there are other options. How would you handle this process?