0

I have two branches, master and feature.

feature branch is derived from master branch and has 3 more commits. It has uncommitted changes as well.

At this point, I was going to update the master with these uncommitted changes.

So I did git checkout master but it throws me an error Your local changes to the following files would be overwritten by checkout.

What I can't understand is that sometimes I was able to switch the branch with uncommitted changes, and sometimes I wasn't.

  1. Could you anyone let me know when I can and can't switch the tab with uncommitted changes ?
  2. And in my above situation, how can I update the master branch for only those uncommitted changes?
Maradox
  • 620
  • 2
  • 13
  • You have to commit your changes to `feature` branch so that you could switch your branch. You could also try stashing those uncommitted changes in `feature` branch if you want to switch without committing. You can find more info [here](https://git-scm.com/docs/git-stash) – Ali Abbasov Jun 15 '21 at 07:55
  • Uncommited changes don't belong to any branch. They are just *there* on the workspace. Using stuff like autostash you can keep them around when you swap branches, but they don't **belong** to any one branch. If you want them to belong to a branch, then you must commit them. – Joachim Sauer Jun 15 '21 at 08:13
  • See also [Checkout another branch when there are uncommitted changes on the current branch](https://stackoverflow.com/q/22053757/1256452). – torek Jun 15 '21 at 12:38

1 Answers1

3

What I can't understand is that sometimes I was able to switch the branch with uncommitted changes, and sometimes I wasn't.

Because there are currently common files between master and features: switching branches would mean overriding the current modification on those common files, which Git actively prevents.

If there are no common files (like new private files in feature, not yet added/committed), then you can switch back and forth between branch.

Do add and commit first (or git stash), then switch branch (with git switch rather than git checkout (since Git 2.23).

If you want to update master only with the new changes (and not the previous 3 commits), I would recommend, especially if you are the only one working on feature, to do an interactive rebase of feature first, in which you reorder commits, and put the last one first:

 m--m--m                 (master, origin/master)
        \
         f1--f2--f3--f4  (feature)

 git rebase -i $(git merge-base master feature)

 m--m--m                 (master, origin/master)
        \
         f4'--f1'--f2'--f3' (feature rebased)

From there, you can merge f4 directly to master.

git branch tmp f4'
git switch master
git merge tmp

 m--m--m--f4'                (master)
            \
             f1'--f2'--f3'   (feature)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    @AndriiNaidenko Note: you will have to force push `feature` once the rebase is done (`git push --force`), which is not a big deal if you were the only one working on it. – VonC Jun 15 '21 at 09:33