git本地仓库,分支切换的问题
在本地创建的git仓库,在分支1中修改某文件,不提交,切换到分支2,结果在分支2中也能看到分支1中的 修改,并且可以在分支2中提交该修改。。。 不明白为啥会这样,分支之间的修改,应该是互相不可见的才对吧。 其实在切换分支之前,提交一下其实也没什么,但这样的特性总让人感觉不爽。 从下面的文章中找到了一个解决方法,那就是在分支1中,切换之前先git stash一下,它会把你的修改隐藏 起来,这样切换分支时就不会有问题。在其他分支修改完之后,回来可以通过git stash list看到你的隐藏记录, 并且通过git stash apply stash@{0}这样的方式把修改拿回来。 http://www.cppblog.com/deercoder/archive/2011/11/13/160007.html 感觉以上链接的博主。 下面是一个实验过程: mt@Mars:~/aaa$ ls file mt@Mars:~/aaa$ cat file This is a file. ##创建两个新的分支,test1和test2 mt@Mars:~/aaa$ git branch test1 mt@Mars:~/aaa$ git branch test2 mt@Mars:~/aaa$ git branch * master test1 test2 mt@Mars:~/aaa$ git checkout test1 Switched to branch 'test1' #在分支1中修改文件 mt@Mars:~/aaa$ echo "Test1" >> file mt@Mars:~/aaa$ cat file This is a file. Test1 mt@Mars:~/aaa$ git status # On branch test1 # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: file # no changes added to commit (use "git add" and/or "git commit -a") #切换到test2分支 mt@Mars:~/aaa$ git checkout test2 M file Switched to branch 'test2' #此时发现file也是被修改的状态!!!!!照我的理解,分支之间,这种修改应该是不可见的。 mt@Mars:~/aaa$ git status # On branch test2 # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: file # no changes added to commit (use "git add" and/or "git commit -a") #在test2分支中直接提交修改,就把在test1中做的修改给提交了。。FT。。 mt@Mars:~/aaa$ git commit -a [test2 2b0b04f] test222 Committer: mt <mt@Mars.(none)> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+) mt@Mars:~/aaa$ cat file This is a file. Test1 #再切回test1 mt@Mars:~/aaa$ git checkout test1 Switched to branch 'test1' #发现此时test1中的文件反而是没有修改之前的内容 mt@Mars:~/aaa$ cat file This is a file. mt@Mars:~/aaa$ git status # On branch test1 nothing to commit (working directory clean) mt@Mars:~/aaa$ #在test1中再次修改 mt@Mars:~/aaa$ echo "NewTTT" >> file #使用stash隐藏修改 mt@Mars:~/aaa$ git stash Saved working directory and index state WIP on test1: 9ff6019 file modify HEAD is now at 9ff6019 file modify #切到test2分支,不会察觉到文件被修改过 mt@Mars:~/aaa$ git checkout test2 Switched to branch 'test2' mt@Mars:~/aaa$ cat file This is a file. Test1 mt@Mars:~/aaa$ git checkout test1 Switched to branch 'test1' #回到test1,用stash查看栈中的内容 mt@Mars:~/aaa$ git stash list stash@{0}: WIP on test1: 9ff6019 file modify mt@Mars:~/aaa$ cat file This is a file. #找回隐藏的修改 mt@Mars:~/aaa$ git stash apply stash@{0} # On branch test1 # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: file # no changes added to commit (use "git add" and/or "git commit -a") #看,文件恢复到了之前修改的状态 mt@Mars:~/aaa$ cat file This is a file. NewTTT mt@Mars:~/aaa$ git status # On branch test1 # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: file # no changes added to commit (use "git add" and/or "git commit -a") #在test1中提交它 mt@Mars:~/aaa$ git commit -a [test1 e6448ad] Newtt. Committer: mt <mt@Mars.(none)> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+) mt@Mars:~/aaa$ git status # On branch test1 nothing to commit (working directory clean) mt@Mars:~/aaa$ cat file This is a file. NewTTT mt@Mars:~/aaa$ git checkout test2 Switched to branch 'test2' mt@Mars:~/aaa$ cat file This is a file. Test1