Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: Do I need to commit my branch before checking out another branch, what about stashing?

I'm new to Git and a bit confused. I have a Master branch and have created a second feature branch.

If I make changes in my feature branch and then switch to Master, will my changes be lost if I don't commit?

Where does stash come into play, is it something you do before you switch branches (but don't want to commit) or is it to simply revert some changes so you can get back to previous code temporarily?

like image 951
Tom DeMille Avatar asked Jan 08 '10 22:01

Tom DeMille


People also ask

Can I checkout to another branch without commit?

Generally, Git won't let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren't committed. You have three options to handle your changes: 1) trash them, 2) commit them, or 3) stash them.

What happens to uncommitted changes when you switch branches?

If you have uncommitted changes when you switch branches, they will be lost.

What does stashing changes mean in git?

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.


2 Answers

You can't change to another branch unless you clean your tree. This is done by committing your changes, reverting them or saving them to the stash.

like image 66
Htbaa Avatar answered Oct 25 '22 20:10

Htbaa


You probably don't want to use stash for this purpose.

If you really want to be developing on the master and a feature branch at the same time, I suggest cloning your repository, working in the clone and the master, and then using push and pull to move changes between them.

If you are switching frequently between them, I suggest checking in before you switch; there's nothing wrong with checking in garbage; git makes it easy to sort this out later on.

like image 29
Alex Brown Avatar answered Oct 25 '22 18:10

Alex Brown