我是git的新手,我一直在努力理解为什么git在我运行git checkout以在分支之间切换时,在另一个分支的一个分支中继续显示我更改的内容首先我尝试不使用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")
Run Code Online (Sandbox Code Playgroud)
我认为,在使用分支时,无论你在一个分支中做什么,它对所有其他分支都是不可见的.这不是创建分支的原因吗?
我尝试使用"git add",但两个分支都可以看到更改.在切换分支之前是否需要运行"git commit"以避免这种情况?
Sea*_*ess 131
切换分支随身携带未提交的更改.首先提交,运行git checkout .
以撤消它们,或git stash
在切换之前运行.(你可以回复你的更改git stash apply
)
Cas*_*bel 29
简短的回答:是的,你需要提交.但请确保您在正确的分支上执行此操作!
分支是指向提交的指针.当您提交签出分支时,分支将前进到指向新提交.当你签出一个分支时,你正在检查它指向的提交.(您可以将提交视为工作树的快照.)
因此,如果您没有提交更改,那么它们将不会受到切换分支的影响.当然,如果切换分支与您的更改不兼容,git checkout
则只会拒绝执行此操作.
git add
是一个暂存更改的命令,然后您将提交.它不会将这些更改记录到存储库的历史记录中.它只是将它们放入一个临时区域(索引); git commit
然后使用该临时区域的内容来创建提交.
归档时间: |
|
查看次数: |
37237 次 |
最近记录: |