为什么无论我是否运行 git add ,当我切换分支(修改、添加、删除文件)时,git 总是显示我的更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5531362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why git keeps showing my changes when I switch branches (modified,added, deleted files) no matter if I run git add or not?
提问by JPZ
I'm really new to git and I've been trying to understand why git keeps showing whatever I changed in one branch in another branch when I run git checkout to switch between branches First I tried not using git add and didn't work. However, I tried then using git add, but didn't fix the problem. I'm not using git commit yet.
我对 git 真的很陌生,我一直在试图理解为什么当我运行 git checkout 以在分支之间切换时,git 不断显示我在另一个分支中的一个分支中所做的更改首先我尝试不使用 git add 并且没有工作。但是,我尝试使用 git add,但没有解决问题。我还没有使用 git commit。
This is basically what I'm doing:
这基本上就是我在做什么:
$ 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")
I thought that, while using branches, whatever you do in one branch, it's invisible to all the other branches. Is not that the reason of creating branches?
我认为,在使用分支时,无论您在一个分支中做什么,其他所有分支都是不可见的。这不是创建分支的原因吗?
I tried using "git add" but the changes are visible in both branches. Do I need to run "git commit" before switching between branches to avoid this?
我尝试使用“git add”,但更改在两个分支中都可见。我是否需要在分支之间切换之前运行“git commit”以避免这种情况?
回答by Sean Clark Hess
Switching branches carries uncommitted changes with you. Either commit first, run git checkout .
to undo them, or run git stash
before switching. (You can get your changes back with git stash apply
)
切换分支会随身携带未提交的更改。首先提交,运行git checkout .
以撤消它们,或者git stash
在切换之前运行。(您可以使用 取回您的更改git stash apply
)
回答by Cascabel
Short answer: yes, you need to commit. Make sure you do it on the right branch, though!
简短回答:是的,您需要提交。但是,请确保在正确的分支上执行此操作!
A branch is a pointer to a commit. When you commit with a branch checked out, the branch advances to point to that new commit. When you check out a branch, you're checking out the commit it points to. (You can think of commits as snapshots of your work tree.)
分支是指向提交的指针。当您在签出分支的情况下提交时,该分支会前进以指向该新提交。当您检出一个分支时,您正在检出它指向的提交。(您可以将提交视为工作树的快照。)
So, if you have changes you haven't committed, they're going to be unaffected by switching branches. Of course, if switching branches is incompatible with your changes, git checkout
will simply refuse to do it.
因此,如果您有尚未提交的更改,它们将不会受到切换分支的影响。当然,如果切换分支与您的更改不兼容,git checkout
将干脆拒绝这样做。
git add
is a command for staging changes, which you will then commit. It does not record those changes into the history of the repository. It simply places them into a staging area (the index); git commit
then uses the contents of that staging area to create commits.
git add
是用于暂存更改的命令,然后您将提交该命令。它不会将这些更改记录到存储库的历史记录中。它只是将它们放入一个暂存区(索引);git commit
然后使用该暂存区的内容来创建提交。