Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching branches in git

Sometimes I'm in a feature branch, but I've made an unrelated change that I want to see in master. Often I can just do:

git checkout master
git commit -m "..." filename

But sometimes when I do the checkout I get a warning that there are local changes and thus I can't switch the branch.

Why does this only happen sometimes? Is there a workaround when I see this message? Maybe stash?

like image 303
Bill Avatar asked Sep 25 '09 01:09

Bill


1 Answers

As Devin Ceartas mentioned, this happens when switching branches would change some file that you've already changed locally. (Git won't complain when you have local changes on a file that would not be changed, or add new files that exist on neither the branch nor master.)

Two ways around this:

  1. "git stash" your changes, change to master, and "git stash apply". Then commit the change.

  2. Commit the changes you want on the branch, then "git stash" any other changes (if there are any), change to master, and cherry-pick the change on master.

like image 75
Phil Avatar answered Oct 23 '22 08:10

Phil