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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决