【问题标题】:Unable to do "git checkout main" [duplicate]无法执行“git checkout main”[重复]
【发布时间】:2021-11-11 02:54:34
【问题描述】:

每当我尝试跑步时:

git checkout main

我收到一个错误:

error: The following untracked working tree files would be overwritten by checkout:
    README.md
Please move or remove them before you switch branches.
Aborting

我已尝试执行以下步骤:

  1.  git rm --cached README.md
     git checkout main 
    

    没用。

  2.  git add -all
     git commit -m "initial commit"
     git checkout main 
    

    还是一样的输出。

【问题讨论】:

  • @Joe,谢谢!这个链接帮助了我!!

标签: git github git-bash git-commit git-checkout


【解决方案1】:

如果您不想保留 README.md 更改:

git add README.md
git reset HEAD --hard
git checkout main

如果您想保留 README.md 更改:

git add README.md
git commit -m "Your commit message"
git checkout main

或者你想稍后在主分支编辑它:

git add README.md
git stash
git checkout main
git stash pop

【讨论】:

  • git reset HEAD --hard 会删除未跟踪的README.md 文件吗?
  • @Joe 谢谢!您应该先添加它,然后重置将起作用。我将编辑答案。
  • 顺便说一句,如果你的 git 版本是 2.23 或更高版本,我推荐新命令:git switch <branch>
  • add/reset --hard 是实现rm 的一种相当混乱的方式。
  • 哦,我明白你的意思了。因为该文件是未跟踪的,如果我们不想保留它,我们可以直接rm它,对吧?
【解决方案2】:

试试:git checkout -f master

还要检查分支名称。 Git 中的默认分支名称是 master。

【讨论】:

  • 好的..但是具体如何检查呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
相关资源
最近更新 更多