2

I created a test branch, made some changes to it, then went back to the master branch and all the changes I made on the test branch are also in the master branch. I can't switch branches before committing?

JesseSousa
  • 25
  • 2
  • 4

2 Answers2

5

Before you commit your changes reside on disk (the "working copy") or after you git add the "staging area". Neither of these belong to a particular branch. When you switch branches, uncommitted changes will go with you. 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. See Stashing and Cleaning in Pro Git.

Alternatively you can commit your work in progress and then later use git commit --amend to add to the commit. See Rewriting History in Pro Git.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 1
    Those are two of the common options; they aren't the only ones, and each possible approach has pros and cons that may work for any given user's workflow; so I would stop short of saying that's what OP "should" do. – Mark Adelsberger Aug 19 '20 at 23:40
2

I can't switch branches before committing?

You can[1]; and if you do, the uncommitted changes come with you. This may not be what you expected, but it's often very useful. Utility aside, it's really the only sensible default behavior for git. Your changes are in the worktree and maybe the index at this point, but by default you only have one of those (not one per branch); so switching branches without bringing along your changes would mean discarding the changes.

If you want to switch branches without bringing your local changes with you, you have several options.

1 - You can stash your changes. Then after you're done looking at the other branch, you can come back to the branch where the changes are and unstash the changes.

git stash
// checkout other branches, do whatever
git checkout branch
git stash pop

https://git-scm.com/docs/git-stash

2 - You can create a temporary branch for your changes. Usually (i.e. unless your changes include deletion of files) you can use the <pathspec> form of checkout to pull the changes back later.

git checkout -b temp
git commit -m "wip"
// checkout other branches, do whatever
git checkout branch
git checkout temp -- .
git branch -D temp

If you do have deletions, you can still make it work, but rather than work around that it probably just makes more sense to use one of the other methods I've listed at least in those cases.

3 - You can go ahead and commit your changes. As long as you don't push them to a shared repository, it's easy to undo the commit anyway.

git commit -m "wip"
// checkout other branches, do whatever
git checkout branch
git reset --soft HEAD^

4 - You can check out the other branch to a new worktree and index. (Recall that only having one worktree and index is what necessitates git's default behavior; so if you don't want that, you can make more worktrees.) https://git-scm.com/docs/git-worktree

5 - You can discard your changes. This may rarely be the option you'd want, but it's easy enough to do if it is what you want.

git reset --hard HEAD

[1] - Well, sometimes you can - such as in the case you describe. If your changes would be overwritten by the attempt to switch branches, then git won't just allow you to switch branches because you would lose data.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52