简体   繁体   中英

Git in Xcode 6.3: Master branch showing new files from other branches in red and won't compile

I am using Xcode's integrated Source Control with Git and I have the following problem:

I have a perfectly working master branch and I want to work on two new features. So I create two new branches, where I add one new file at each branch.

Now when I switch back to the master branch or the other branch, after committing the changes and without merging (I don't want to merge yet), the files from ALL the branches appear in the project navigator (the ones that don't belong to the current branch are in red colour) and prevent my code from compiling as the compiler complains that these files don't exist.

My master at least should compile regardless of what I've done in other branches right?

Am I missing something trivial here?

Untracked files and unstaged changes do not belong to any branch. They only live in your working tree. When you switch branches, those files/changes are left untouched. If you want them to exist only in a certain branch, you have to add and commit all of them.

This should help understanding:

$ git status
On branch master
nothing to commit, working directory clean

$ >>file echo 'added line'

$ touch new_file

$ git status
On branch master
Changes not staged for commit:

  modified: file

Untracked files:
  new_file

$ git checkout -b new_branch
M       file
Switch to a new branch 'new_branch'

$ git status
On branch new_branch
Changes not staged for commit:

  modified: file

Untracked files:
  new_file

So, as you see, unstaged changes are carried over when switching branches (this is by design). When checking out another branch, Git tells you which files contain changes (the line starting with M odified). This also holds for untracked files (Git cannot delete them, since you might need them), but their names are not explicitely output when checking out a branch.

To have Git delete files when switching a branch, you have to add (and commit.) them to a branch first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM