git checkout 将未暂存的文件带到新分支 [英] git checkout carries unstaged files to the new branch

查看:33
本文介绍了git checkout 将未暂存的文件带到新分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 gitlab CE 设置存储库,作为设置的一部分创建了一个存储库并一直在使用它,当我遇到时,在我对文件进行一些修改并使用切换分支之后结帐时,即使我有未暂存的文件,我也可以切换,这与我之前的经验不同,即在我进行提交或存储之前我不允许结帐.

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 不会更改存储库中当前未跟踪的文件.在您的示例中,您只创建了一个未跟踪的文件 (newfile).

Git won't change files that are not currently tracked in the repository. In your example, you only created an untracked file (newfile).

所以 Git 的行为是绝对正常的.

So the Git's behavior is absolutely normal.

如果你 git add newfile 没有提交更改,Git 将不允许你切换到 master 分支.

If you git add newfile without commiting the changes, Git won't allow you to switch to master branch.

例如,这将由 Git 处理:

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'

深入解释:https://stackoverflow.com/a/8526610/882697

这篇关于git checkout 将未暂存的文件带到新分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆