git checkout将未分档的文件传送到新分支 [英] git checkout carries unstaged files to the new branch
问题描述
我一直在尝试使用gitlab CE来设置存储库,作为设置的一部分创建了一个repo并且一直在玩它,当我遇到时,在对文件进行一些修改并使用switch branch结账时,即使我有未分期存档的文件,我也可以进行切换,这与我之前的经验有所不同,我不能在结账或存储之前结账。
这种体验胜过无缝切换分支的目的,而不必担心无意中潜入。
在下面突出显示。
somasundaram.s@user / d / projects / repositories / newrepo(master)
$ ls -ltr
total 1
-rw-r - r-- 1 somasundaram.s 1049089 13 Apr 4 16:28自述文件
-rw-r - r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
somasundaram.s@user / d / projects / repositories / newrepo(master)
$ git分支新分支
somasundaram.s @ user / d / projects / repositories / newrepo(master)
$ git checkout new-branch
转换到分支'new-branch'
somasundaram.s@user / d /项目/存储库/ newrepo(新分支)
$ touch newfile
somasundaram.s@user / d / projects / repositories / newrepo(新分支)
$ ls - ltr
total 1
-rw-r - r-- 1 somasundaram.s 1049089 13 Apr 4 16:28自述文件
-rw-r - r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
-rw-r - r-- 1 somasundaram.s 1049089 0 Apr 4 16:37 newfile
somasundaram.s@user / d / projects / repositories / newrepo(新分支)
$ git checkout master
转换到分支'master'
somasundaram.s@user / d / projects / repositories / newrepo(master)
$ ls -ltr
total 1
-rw- r - r-- 1 somasundaram.s 1049089 13 Apr 4 16:28自述文件
-rw -r - r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
-rw- r - r - 1 somasundaram.s 1049089 0 Apr 4 16:37 newfile
Git不会更改当前未在存储库中进行跟踪的文件。
在你的例子中,你只创建了一个未跟踪的文件( newfile
)。
如果您 git添加新文件
而不提交更改,Git将不允许您切换例如,这将由Git处理:
$ git branch new-branch
$ git checkout new-branch
转换到分支'new-branch'
$ echotest> newfile
$ git add newfile
$ git checkout master
一个新文件
转换到分支'master'
深入解释:
https://stackoverflow.com/a / 8526610/882697
I have been trying to set-up repository with gitlab CE, as a part of the set-up created a repo and been playing around with it, when I encountered, that after I make some modification to files and switch branch using checkout, I'm allowed to switch even though I have unstaged files, which was different from my previous experience that I was not allowed to checkout until I either did a commit or stash.
This experience beats the purpose of seamlessly switching branches without having to worry about unintended creeping in.
The steps are highlighted below.
somasundaram.s@user /d/projects/repositories/newrepo (master)
$ ls -ltr
total 1
-rw-r--r-- 1 somasundaram.s 1049089 13 Apr 4 16:28 README
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
somasundaram.s@user /d/projects/repositories/newrepo (master)
$ git branch new-branch
somasundaram.s@user /d/projects/repositories/newrepo (master)
$ git checkout new-branch
Switched to branch 'new-branch'
somasundaram.s@user /d/projects/repositories/newrepo (new-branch)
$ touch newfile
somasundaram.s@user /d/projects/repositories/newrepo (new-branch)
$ ls -ltr
total 1
-rw-r--r-- 1 somasundaram.s 1049089 13 Apr 4 16:28 README
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:37 newfile
somasundaram.s@user /d/projects/repositories/newrepo (new-branch)
$ git checkout master
Switched to branch 'master'
somasundaram.s@user /d/projects/repositories/newrepo (master)
$ ls -ltr
total 1
-rw-r--r-- 1 somasundaram.s 1049089 13 Apr 4 16:28 README
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:37 newfile
Git won't change files that are not currently tracked in the repository.
In your example, you only created an untracked file (newfile
).
So the Git's behavior is absolutely normal.
If you git add newfile
without commiting the changes, Git won't allow you to switch to master branch.
For example, this will be handled by Git:
$ git branch new-branch
$ git checkout new-branch
Switched to branch 'new-branch'
$ echo "test" > newfile
$ git add newfile
$ git checkout master
A newfile
Switched to branch 'master'
For in depth explanations: https://stackoverflow.com/a/8526610/882697
这篇关于git checkout将未分档的文件传送到新分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!