Context switching in git

Filed under: Git, — Tags: Git — Thomas Sundberg — 2020-03-11

It happens that you are working on something and realize that you have to change context. Something urgent popped up and you need to get it into production as soon as possible. However, you have uncommitted changes that doesn't work properly. What do you do now? You can't commit your current changes. They are not done and should not end up in production. And you are smart so you avoid branching. Your current changes are on master and should be on master.

Hide your current changes

One way to deal with this, and be able to verify your change, is to hide your current changes and do the new changes needed. You stash your changes.

First, lets look at the local changes, you do this with the command

git status

Then stash the changes with the command

git stash

This will hide your changes. Your repository is now in the state it was before the last changes, i.e. you are back at your latest commit.

Do git status again to see that you don't have any local changes.

Switch context and do the changes needed

You can now do your new changes. You can build and test your application with the latest changes, not the ones that where half done.

When you are happy, you commit and push your work. I usually do

git fetch
git rebase
git push

Go back to your earlier state

It is now time to go back to your half done work that you stashed earlier. Popping the stash will bring them back. You do this with the command

git stash pop

Your earlier changes are now retrieved. Do git status to verify that you have your changed files.

Conclusion

It is possible to hide your work in git. Stashing them is one way. Other tools have other ways, some have the concept of shelving changes.

Acknowledgements

I would like to thank Malin Ekholm, Mika Kytöläinen, and Henrik Jörnvall for feedback.

Resources



(less...)

Pages

About
Events
Why

Categories

Agile
Automation
BDD
Clean code
Continuous delivery
Continuous deployment
Continuous integration
Cucumber
Culture
Design
DevOps
Executable specification
Git
Gradle
Guice
J2EE
JUnit
Java
Javascript
Kubernetes
Linux
Load testing
Maven
Mockito
New developers
Pair programming
PicoContainer
Presentation
Programming
Public speaking
Quality
React
Recruiting
Requirements
Scala
Selenium
Software craftsmanship
Software development
Spring
TDD
Teaching
Technical debt
Test automation
Tools
Web
Windows
eXtreme Programming

Authors

Thomas Sundberg
Adrian Bolboaca

Archives

Meta

rss RSS