Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to stash / commit my changes before switching branches?

Tags:

git

I find this to be annoying because I would like to quickly switch branches and do something and then switch back to what I was working on before. I realize I can stash and then retrieve stash, but then I have to type those lines every time :/

Is there any way to avoid having to do this?

I also find it annoying because I keep about 5 or 10 feature branches and it gets hard to manage which ones have a stash that needs to be retrieved and which ones are good to keep working on as they are.

like image 860
sherlock Avatar asked Nov 05 '13 16:11

sherlock


People also ask

Should you commit changes before switching branches?

You must commit or stash those changes first before switching branches. You can think of stash as a drawer to store uncommitted changes temporarily. Stashing allows you to put aside the “dirty” changes in your working tree and continue working on other things in a different branch on a clean slate.

What happens if you switch branches without committing?

Originally Answered: If you do not commit and you switch branches (or pull), do you lose your tracked but uncommitted modified files (whether staged or not)? No. By default git checkout and git pull will refuse to perform an update that touches any files with uncommitted modifications (tracked or not).

Should I commit or stash?

A commit creates a new save point on a branch; a stash reverts to a previous save point. A new commit leaves files in the working tree unchanged; a stash resets files in the working tree to the previous commit point. A commit is a public record of file changes; a stash is local.

What happens when you switch branches?

When you switch a branch, all files that are under control of Git will be replaced with the state of the new branch. That includes changes to files as well as additions and deletions. In your case this means that you have some files in your current 'local' branch that simply do not exist in the master.

Can you switch branches after commit?

If changing a branch would overwrite your uncommitted changes, Git will not let you switch. When you have work in progress and want to switch branches you should put it in the stash using git stash .


2 Answers

One way to make this easier is to use something like Legit. One of the commands I like from Legit is git switch:

$ git switch <branch>
# Switches to branch. Stashes and restores unstaged changes.

Legit stores the stash with a description that follows a particular naming convention. That way, when you use git switch to return to the first branch, it can automatically unstash the changes saved for that branch.

like image 157
Greg Hewgill Avatar answered Oct 10 '22 22:10

Greg Hewgill


You could use git checkout's --merge(-m) option.
Using it causes a three-way merge to be done when switching branches. You may need to resolve merge conflicts if these occur.

As to why this occurs, the manual states

When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context.

See the git checkout manual page for more details

like image 3
Hasturkun Avatar answered Oct 10 '22 20:10

Hasturkun