美文网首页
Git问题:切换分支后本地代码会改变吗?

Git问题:切换分支后本地代码会改变吗?

作者: _百草_ | 来源:发表于2025-06-20 15:26 被阅读0次

1、问题

29447@GW64 /d/myProject (temp_branch)
$ git status -s
 M temp.txt
?? stash_file

29447@GW64 /d/myProject (temp_branch)
$ git checkout -
M       temp.txt
Switched to branch 'main'

29447@GW64 /d/myProject (main)
$ git status -s   # 切换main分支后,文件及状态一致
 M temp.txt
?? stash_file

29447@GW64 /d/myProject (main)
$ git add .

29447@GW64 /d/myProject (main)
$ git status -s
A  stash_file
M  temp.txt

29447@GW64 /d/myProject (main)
$ git checkout -
A       stash_file
M       temp.txt
Switched to branch 'temp_branch'

29447@GW64 /d/myProject (temp_branch)
$ git status -s   # 切换test_branch分支后,文件及状态一致
A  stash_file
M  temp.txt

2、原因

一个工作区和一个暂存区,但是存在多个分支的提交区
1、没有commit时,无论有无add,进行切换分支操作时,原分支修改的内容在新分支也有
2、已经add且commit时,切换分支操作,新分支上就看不到原分支上的修改了

29447@GW64 /d/myProject (temp_branch)
$ git status -s
A  stash_file
M  temp.txt

29447@GW64 /d/myProject (temp_branch)
$ git commit stash_file -m "test_branch:提交stash_file"
[temp_branch 4f6eb6f] test_branch:提交stash_file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 stash_file

29447@GW64 /d/myProject (temp_branch)
$ git status -s
M  temp.txt

29447@GW64 /d/myProject (temp_branch)
$ ls
3ac_202506171610.txt  stash_file  temp.txt


29447@GW64 /d/myProject (temp_branch)
$ git checkout -
M       temp.txt
Switched to branch 'main'

29447@GW64 /d/myProject (main)
$ git status -s
M  temp.txt

29447@GW64 /d/myProject (main)
$ ls
3ac_202506171610.txt  temp.txt


3、参考

1、git切换分支后,本地代码会改变吗

相关文章

  • git 自己使用心得(2)切换线程 mac

    切换分支 git checkout -b develop(分支名字) 在本地更改完分支的代码后,add并commi...

  • git常用命令

    分支管理 git 切换分支 git 查看远程分支 git 查看本地分支 git 创建本地分支 git 删除本地分支...

  • git常用命令

    查看分支:git branch -a 切换本地分支:git checkout <分支名> 创建+切换本地分支:gi...

  • git 相关命令学习

    本地分支指向新的远程分支 切换到新的远程分支拉取最新代码 切换到本地分支 git branch --unset-u...

  • 提交本地代码到新分支

    将从分支A下载代码,然后提交到分支B添加本地需要提交代码: 提交本地代码: push 到git仓库: 切换新分支:

  • 提交本地代码到新分支

    将从分支A下载代码,然后提交到分支B添加本地需要提交代码: 提交本地代码: push 到git仓库: 切换新分支:

  • git 本地分支与远程分支相关

    本地分支 查看本地分支:git branch -v创建本地分支:git branch name切换本地分支:git...

  • git 常用命令行

    创建一个分支 切换到分支 experiment 是以上两条命令行的缩写 问题: git远程删除分支后,本地git ...

  • git 常用命令

    新建本地分支并切换 git checkout -b <本地分支> <远程分支> 删除本地分支 git branc...

  • git创建分支与合并分支(本地)

    git创建分支与合并分支(本地) 本地创建分支 切换分支 删除本地分支 $ git branch -d [name...

网友评论

      本文标题:Git问题:切换分支后本地代码会改变吗?

      本文链接:https://www.haomeiwen.com/subject/frecgjtx.html