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 t
This is actually a solution for one of your problems but you can add a message to each stash in order to know which stash belongs to which branch. Doing it so you will allways easily know which branches have a stash and which don't.
For doing this just use this command:
git stash save -u branch1
where 'branch1' is the message or name that you give the stash
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.
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
You can avoid it using git stash to stash your changes. Then you can change branch, then restore the branch and getting back your changes:
$ git stash
$ git checkout other_branch
$ git checkout original_branch
$ git stash pop