git “无法同时更新路径和切换到分支”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22984262/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:59:25  来源:igfitidea点击:

"Cannot update paths and switch to branch at the same time"

gitgit-checkout

提问by marekful

I sometimes use the checkout -boption to create a new branch, check it out at the same time and set up tracking in one command.

我有时会使用该checkout -b选项来创建一个新分支,同时检查它并在一个命令中设置跟踪。

In a new environment, I get this error:

在新环境中,我收到此错误:

$ git checkout -b test --track origin/master
fatal: Cannot update paths and switch to branch 'test' at the same time.
Did you intend to checkout 'origin/master' which can not be resolved as commit?

Why does Git not like it? This used to work with the same repo.

为什么 Git 不喜欢它?这曾经与同一个 repo 一起使用。

回答by VonC

'origin/master' which can not be resolved as commit

' origin/master' 无法解析为提交

Strange: you need to check your remotes:

奇怪:你需要检查你的遥控器:

git remote -v

And make sure originis fetched:

并确保origin已获取:

git fetch origin

Then:

然后:

git branch -avv

(to see if you do have fetched an origin/masterbranch)

(看看你是否确实获取了一个origin/master分支)

Finally, use git switchinstead of the confusing git checkout, with Git 2.23+ (August 2019).

最后,使用Git 2.23+(2019 年 8 月)git switch代替令人困惑的git checkout

git switch -c test --track origin/master

回答by Ludder

FWIW: If you have a typo in your branchname you'll get this same error.

FWIW:如果你的分支名称有错别字,你会得到同样的错误。

回答by Bob Aman

You can get this error in the context of, e.g. a Travis build that, by default, checks code out with git clone --depth=50 --branch=master. To the best of my knowledge, you can control --depthvia .travis.ymlbut not the --branch. Since that results in only a single branch being tracked by the remote, you need to independently update the remote to track the desired remote's refs.

您可以在例如 Travis 构建的上下文中收到此错误,默认情况下,该构建使用git clone --depth=50 --branch=master. 据我所知,您可以控制--depthvia.travis.yml但不能控制--branch. 由于这导致远程仅跟踪单个分支,因此您需要独立更新远程以跟踪所需远程的引用。

Before:

前:

$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master

The fix:

修复:

$ git remote set-branches --add origin branch-1
$ git remote set-branches --add origin branch-2
$ git fetch

After:

后:

$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/branch-1
remotes/origin/branch-2
remotes/origin/master

回答by Ashwini Reddy

This simple thing worked for me!

这个简单的事情对我有用!

If it says it can't do 2 things at same time, separate them.

如果它说它不能同时做两件事,就把它们分开。

git branch branch_name origin/branch_name 

git checkout branch_name

回答by ssasi

You could follow these steps when you stumble upon this issue:

当您偶然发现此问题时,您可以按照以下步骤操作:

  1. Run the following command to list the branches known for your local repository.
  1. 运行以下命令以列出本地存储库已知的分支。

git remote show origin

git远程显示原点

which outputs this:

输出这个:

 remote origin
  Fetch URL: <your_git_path>
  Push  URL: <your_git_path>
  HEAD branch: development
  Remote branches:
    development                             tracked
    Feature2                                tracked
    master                                  tracked
    refs/remotes/origin/Feature1         stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    Feature2     merges with remote Feature2
    development  merges with remote development
    master       merges with remote master
  Local refs configured for 'git push':
    Feature2     pushes to Feature2     (up to date)
    development  pushes to development (up to date)
    master       pushes to master      (local out of date)
 remote origin
  Fetch URL: <your_git_path>
  Push  URL: <your_git_path>
  HEAD branch: development
  Remote branches:
    development                             tracked
    Feature2                                tracked
    master                                  tracked
    refs/remotes/origin/Feature1         stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    Feature2     merges with remote Feature2
    development  merges with remote development
    master       merges with remote master
  Local refs configured for 'git push':
    Feature2     pushes to Feature2     (up to date)
    development  pushes to development (up to date)
    master       pushes to master      (local out of date)
  1. After verifying the details like (fetch URL, etc), run this command to fetch any new branch(i.e. which you may want to checkout in your local repo) that exist in the remote but not in your local.
  1. 在验证(获取 URL 等)等详细信息后,运行此命令以获取远程存在但不在本地的任何新分支(即,您可能希望在本地存储库中检出该分支)。
? git remote update

Fetching origin
From gitlab.domain.local:ProjectGroupName/ProjectName
 * [new branch]      Feature3    -> Feature3
? git remote update

Fetching origin
From gitlab.domain.local:ProjectGroupName/ProjectName
 * [new branch]      Feature3    -> Feature3

As you can see the new branch has been fetched from remote.
3. Finally, checkout the branch with this command

如您所见,新分支已从远程获取。
3. 最后,用这个命令检出分支

? git checkout -b Feature3 origin/Feature3

Branch Feature3 set up to track remote branch Feature3 from origin.
Switched to a new branch 'Feature3'
? git checkout -b Feature3 origin/Feature3

Branch Feature3 set up to track remote branch Feature3 from origin.
Switched to a new branch 'Feature3'

It is not necessary to explicitly tell Git to track(using --track) the branch with remote.

没有必要明确告诉 Git 跟踪(使用--track)远程分支。

The above command will set the local branch to track the remote branch from origin.

上面的命令将设置本地分支从源跟踪远程分支。

回答by dansek

If you got white space in your branch then you will get this error.

如果您的分支中有空白,那么您将收到此错误。

回答by questionKing

It causes by that your local branch doesn't track remote branch. As ssasi said,you need use these commands:

这是因为您的本地分支不跟踪远程分支。正如 ssasi 所说,你需要使用这些命令:

git remote update
git fetch
git checkout -b branch_nameA origin/branch_nameB

I solved my problem just now....

我刚刚解决了我的问题......

回答by RayLoveless

For me I needed to add the remote:

对我来说,我需要添加遥控器:

git remote -add myRemoteName('origin' in your case) remoteGitURL

then I could fetch

然后我可以取

git fetch myRemoteName

回答by Raheel Hasan

First you need to Fetchthe remote (the specific branch), then you can create a local br and track it with that remote branch using your command (i.e. checkoutwith -b and --track).

首先您需要Fetch远程(特定分支),然后您可以创建一个本地 br 并使用您的命令(即checkout使用 -b 和 --track)使用该远程分支跟踪它。

回答by knight2016

You should go the submodule dir and run git status.

您应该转到子模块目录并运行git status.

You may see a lot of files were deleted. You may run

您可能会看到很多文件被删除。你可以跑

  1. git reset .

  2. git checkout .

  3. git fetch -p

  4. git rm --cached submodules//submoudles is your name

  5. git submoudle add ....

  1. git reset .

  2. git checkout .

  3. git fetch -p

  4. git rm --cached submodules//子模块是你的名字

  5. git submoudle add ....