问题
Possible Duplicates:
git: Switch branch and ignore any changes without committing.
Git branches behaving strangely
I know that the general recommendation is to have a clean status before switching branches in git. ( stash or park-commit ). I'm trying to understand when will I get "You have local changes cannot switch branches" , I can't follow the logic :
I have a repo, with a file called version.txt, contains the text "1" :
git checkout -b new
echo 2 >> version.txt (working dir is not dirty, modified the file)
git checkout master ( how come this works ? I have not stages\commited my changes on new )
same happens if I delete the file content in my new branch, or stage the file first.
Can someone help me to understand when will i get the "You have local changes cannot switch branches." ?
Thanks, Ran
回答1:
It's quite simple, if you have changes in a file which will be modified if you change to a specific branch.
Example:
$ git init
Initialized empty Git repository in /tmp/asadfasd/.git/
$ echo 1 > bar
$ git commit -am "commit 1 master"
[master (root-commit) 55da003] commit 1
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 bar
$ git co -b testbranch
Switched to a new branch 'testbranch'
$ git co master
Switched to branch 'master'
$ echo 2 >> bar
$ git commit -am "commit 2 master"
[master c6dc6d9] commit 2 master
1 files changed, 1 insertions(+), 0 deletions(-)
$ git co testbranch
Switched to branch 'testbranch'
$ echo 3 >> bar
$ git co master
error: Your local changes to the following files would be overwritten by checkout:
bar
Please, commit your changes or stash them before you can switch branches.
Aborting
If you would rebase testbranch on master before modifying the file it would work because the file wouldn't be modified.
Another way to put it, if the branches are diverged and you modify the diverged files it won't work.
回答2:
When master and test HEAD are same, local changes in test are applied to master when you switch back.
If however they are not, say you had an extra commit in master after you created test or an extra commit in test, you will get the warning.
( just remembered that I had answered this before - pasting my old answer here and voting to close this question as duplicate)
When you are switching branch and the files are only locally modified, Git will not give you the warning / message ( and will merge the changes into the other branch). For example, you have your repo on master, create a branch temp, have a local modification on the file. Now, when you switch to master you won't get the message. If on the other hand you makes changes in temp and commit them ( that is temp diverges from master ) and then you have local modifications, it will give you that message when you switch to master
Git branches behaving strangely
回答3:
When switching branches, Git only updates the parts of the index and working directory that differ from the revision you are switching away from. If a file doesn't differ between the two revisions, git checkout
doesn't even look to see if it was modified.
In your example, the contents of version.txt
is the same in each branch. Thus, Git happily ignores any uncommitted modifications to that file when switching between the two.
来源:https://stackoverflow.com/questions/6638937/switching-branches-in-git-when-will-i-get-you-have-local-changes-cannot-switc