Alxblsk.com Logo
RE•DONE
Blog by Aliaksei Belski

How to Switch Among Different Scopes in Git?

Published: August 20th, 2020gitstash-changes

How to Switch Among Different Scopes in Git?

Imagine, you've been working on a feature, and need to switch and fix a critical bug. Did you ever experience such a situation? It may sound complex for several reasons:

  1. A new branch may contain conflicting changes;
  2. You may want to keep changes, but start development anew.

One simple way is to make a commit in an old branch, but it's not always convenient. For example, your work is still in progress, and code doesn't work as expected, or lints / tests weren't fixed.

For such scenarios, there is a handy way to save your code in a buffer and use it later:

git stash

After executing this command, all the staged changes will be extracted and placed to a virtual stack. To apply changes back, simply run:

git stash apply

Once you've done with changes and no longer need them, you can drop the changes by running the appropriate command:

git stash drop

That's it! Now you're ready to a to switch between branches fast.