Git Stash and Git Rebase

When to use them and how to handle merging your code to master

Sean LaFlam
Level Up Coding

--

When I was first learning to code, I would be the only engineer working on an app. Every once in a while I would have a project I would be working on with one other engineer, but a lot of the progress was made during pair programming sessions, where we would just push all the changes directly to master.

When I moved onto larger apps and began working at a company with a dozen engineers I began learning how to use branches, and how to use pull requests to merge my changes to the code base without affecting the other engineers code. Two of the most useful commands I have found are ‘git rebase’ and ‘git stash’. In conjunction with other commands such as git pull, git merge, etc. you start to realize truly how powerful of a tool git is.

Git Stash

As you start working on larger team, and especially one who’s product is currently live, there are times where you may begin working on a branch but will have to suddenly switch off of it to address some more urgent issues.

If you were to simply use git checkout to switch to the branch git will attempt to bring all of your changes on your current branch (Branch A) over to the branch you’re switching to (Branch B). Sometimes it will switch with no issue if there are no conflicts, but other times it will give you a warning that you need to commit your changes before checking out the new branch.

Either way, many times you do not want to bring the new code that you’ve been working on Branch A over to Branch B. What you want to do is save the code you’ve added, but when you switch to Branch B you would like it to revert back to the state it was in before you created Branch A. This is exactly what git stash was designed to do.

The git stash command helps you stash your work away and clear your working directory. Then when you checkout it will not carry over the new code with you.

Now let’s say you went and fixed the urgent error in Branch B, and you decided you want to import these changes that you made on Branch A. You can use the command git stash list to see a list of all the changes you’ve stored in the stash. From there you can apply these changes using the command git stash apply <stash_name> where stash_name is the name given by the list command (will looks something like stash@{0}: )

Finally, you can use the command git stash drop <stash_name> to remove an old or unwanted stash from your list.

Git Rebase

The command git rebase applies your commits on top of another branch. Here is an effective diagram I found online to explain it.

Let’s say you branch off of master and start adding a new feature to your app. While you’re working, a part of your app’s code that your new feature relies on what changed to fix a bug. Your code now is different from master in both your new feature, and some other bug fixes, so you are several commits both behind master and ahead of master.

Blue is the commits to fix a bug, and green represent your new feature commits.

When you go to merge your code there are a bunch of merge conflicts that you aren’t sure how to resolve. If only there was a way to pull the bug fixes on master into your branch, and THEN add the commits for your new feature onto the fixed up code.

You’re in luck, this is exactly what git rebase does! It will move your new feature to the tail end of the new commits on master, creating one continuous flow of commits that should be conflict free, and replaces the need to merge.

These 2 commands should really enhance your ability to avoid conflicts and move around from branch to branch on a larger team!

--

--