我真的是 git 的新手,我一直在努力理解为什么当我运行 git checkout 在分支之间切换时,git 一直在显示我在一个分支中所做的任何更改,在另一个分支中首先我尝试不使用 git add 并且没有'不工作。但是,我尝试使用 git add,但没有解决问题。我还没有使用 git commit。
这基本上就是我正在做的:
$ git clone <a_repository>
$ git branch
* master
$ git branch testing
$ git checkout testing
...edit a file, add a new one, delete...
$ git status
# On branch testing
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: file1.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file2.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git branch
master
* testing
$ git checkout master
D file1.txt
Switched to branch 'master'
$ git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: file1.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file2.txt
no changes added to commit (use "git add" and/or "git commit -a")
我认为,在使用分支时,无论您在一个分支中做什么,所有其他分支都是不可见的。这不就是创建分支的原因吗?
我尝试使用“git add”,但两个分支中的更改都可见。 在分支之间切换之前是否需要运行“git commit”以避免这种情况?
最佳答案
切换分支会带来未提交的更改。要么先提交,然后运行 git checkout .
撤消它们,要么在切换之前运行 git stash
。 (您可以使用 git stash apply
取回您的更改)
关于git - 为什么无论我是否运行 git add,当我切换分支(修改、添加、删除文件)时,git 一直显示我的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5531362/