Git 常见问题
本文档旨在帮助你解决 Git 中经常遇到的一些奇怪的技术故障场景(WTFS)。它会随着问题的出现而更新。
fatal:拒绝合并不相关的历史
这通常发生在你在从中拉取后有人更改了骨架代码时。使用
--allow-unrelated-histories
拉取,即
$ git pull skeleton main --allow-unrelated-histories --no-rebase
你可能需要 解决一些由此产生的合并冲突 。
或者,如果你从你自己的 s*** student repo 拉取(例如,在不同的计算机上),
$ git pull origin main --allow-unrelated-histories --no-rebase
HEAD 分离
注意:
从课程的 FA23 版本开始,由于首选
git restore
,这种故障场景应该会少得多。
$ git status
HEAD detached at 1193e06
Untracked files:
(use "git add <file>..." to include in what will be commited)
../seitan/
nothing added to commit but untracked files preset (use "git add" to track)
很可能,你使用了
git checkout
命令而没有指定文件(或目录)。没关系!如果你没有做任何更改,你可以使用命令
git switch main
来修复。如果一切正常,你应该看到类似的消息:
$ git switch main
之前的 HEAD 位置是 b405852... 添加了 tofu 食谱
已切换到分支 'main'
如果你做了一些更改(即使用命令
git status
告诉你修改了一些文件,如下图),需要采取更多步骤。
$ git status
HEAD detached at 1193e06
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: kung_pao_tofu.txt
Untracked files:
(use "git add <file>..." to include in what will be commited)
../seitan/
no changes added to commit (use "git add" and/or "git commit -a")
首先,使用命令
git stash
。你的修改可能奇迹般地消失了!别担心——我们稍后能够恢复它们!
$ git stash
Saved working directory and index state WIP on (no branch): 1193e06 added tofu
recipes HEAD 是 now at 1193e06 added tofu recipes
$ git status
HEAD detached at 1193e06
Untracked files:
(use "git add <file>..." to include in what will be commited)
../seitan/
nothing added to commit but untracked files preset (use "git add" to track)
从这里,使用命令
git switch main
。你应该会看到之前的所有清除消息:
$ git switch main
之前的 HEAD 位置是 b405852... 添加了 tofu 食谱
已切换到分支 'main'
快完成了!让我们取回我们的更改。使用
git stash pop
。但等等,我们有冲突了!(这可能并不总是发生。如果你没有遇到冲突,你应该可以继续了。)
$ git stash pop
Auto-merging tofu/kung_pao_tofu.txt
CONFLICT (content): 合并 conflict in tofu/kung_pao_tofu.txt
$ git status
On branch main
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: kung_pao_tofu.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
../seitan/
no changes added to commit (use "git add" and/or "git commit -a")
现在使用
git stash drop
:
$ git stash drop
Dropped refs/stash@{0} (57f0ac5c5480964cdf29a94ed6b87e38da823488)<粘贴>
现在我们必须解决这个合并冲突。要了解如何操作,请看 这里 !
错误:推送某些引用失败??
有时在与其他人协作时,push 时会收到这样的消息:
$ git push origin main
To https://github.com/gilbertghang/recipes.git
! [rejected] main -> main (non-fast-forward)
错误: failed to push some refs to 'https://github.com/gilbertghang/recipes.git"
hint: Updates were rejected because the tip of your current branch 是 behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the '注意 about fast-forwards' in 'git push --help' for details.
这里发生的是你的远程(即你的在线 Github 仓库)包含你的本地仓库没有的提交。幸运的是,Git 非常擅长告诉你如何修复这些错误:如果你仔细阅读错误消息,你会看到它建议你
git pull
。做那个,解决任何
合并冲突
,然后 push。完成!
我代码里这些符号是什么??
有时当你从仓库拉取时,拉取时会看到这样的消息:
$ git pull origin main
From github.com:伯克利-CS61B/course-materials-sp16
* branch main -> FETCH_HEAD
Auto-merging proj/proj0/solution/canonical/Planet.java
CONFLICT (content): 合并 conflict in proj/proj0/solution/canonical/Planet.java
Automatic merge failed; fix conflicts and then commit the result.
问题是你的计算机上的代码与你要拉取的远程仓库中的代码存在冲突,Git 无法弄清楚如何解决。由于它不确定,Git 拒绝覆盖你的本地代码。
但是,当你打开你的 Planet.java 时,你会看到一些奇怪的垃圾,比如:
public Planet(Planet p) {
<<<<<<< HEAD
th是.xPos = p.xPos;
th是.yPos = p.yPos;
=======
th是.xxPos = p.xxPos;
th是.yyPos = p.yyPos;
>>>>>>> 27ddd0c71515e5cfc7f58a43bcf0e2144c127aed
这是一件好事!
<<<<<<< HEAD
和
=======
之间的所有内容都是你计算机上的内容,
=======
和
27ddd0c71515e5cfc7f58a43bcf0e2144c127aed
之间的所有内容是远程服务器上的内容。
你的工作是查找这些内容并自己解决合并冲突。在这种情况下,远程仓库是正确的,所以我们只需删除
<<<<<<< HEAD
和
=======
之间的所有内容,也删除
>>>>>>> 27ddd0c71515e5cfc7f58a43bcf0e2144c127aed
标记,留下:
public Planet(Planet p) {
th是.xxPos = p.xxPos;
th是.yyPos = p.yyPos;
一旦你解决了所有合并冲突,添加你手动编辑的所有文件,然后像平常一样提交它们,例如
git add Planet.java
git commit -m "resolved merge conflict"
git push origin main
错误:你尚未完成合并(MERGE_HEAD 存在)。
如果你看到这样的消息:
error: You have not concluded your merge (MERGE_HEAD exists).
hint: Please, commit your changes before merging.
fatal: Exiting because of unfinished merge.
添加并提交你当前的更改,然后再次拉取。
如果再次拉取后,你的终端显示"请输入提交消息来解释为什么需要此合并"的消息,请参阅下一节。
请输入提交消息来解释为什么需要此合并
如果你看到这样的消息:
Merge branch 'main' of https://github.com/Berkeley-CS61B/skeleton-sp24
# Please enter a commit message to explain why th是 merge 是 necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Git 为你打开了一个终端文本编辑器来输入提交消息。你可以保留默认提交消息并退出文本编辑器。
如果你在终端底部看到类似这样的内容:
^G Get Help ^O WriteOut ^R Read File ^Y Prev Pg ^C Cur Pos
这表明你正在使用 Nano 文本编辑器。要退出,请按 Ctrl+X(两个键同时按)。
如果你在终端左侧看到一堆波浪号(
~
符号),并且在终端底部看到类似这样的内容:
".git/COMMIT_EDITMSG" 9L, 273C 13,1 All
这表明你正在使用 Vim 文本编辑器。要保存并退出,输入
:wq
——冒号键,然后是字母
w
,然后是字母
q
,一个接一个(不是同时)。
fatal: 'skeleton' 似乎不是一个 Git 仓库
如果你看到这样的消息:
fatal: 'skeleton' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
Git 可能找不到 skeleton repo 的位置。
尝试运行
git remote -v
。如果你的 repo 设置正确,你应该看到:
origin git@github.com:Berkeley-CS61B/sp24-s*** (fetch)
origin git@github.com:Berkeley-CS61B/sp24-s*** (push)
skeleton https://github.com/Berkeley-CS61B/skeleton-sp24.git (fetch)
skeleton https://github.com/Berkeley-CS61B/skeleton-sp24.git (push)
如果你只看到对应于
origin
的两行,而没有看到对应于
skeleton
的两行,那么 Git 不知道在哪里找到 skeleton repo。
要修复此问题,请运行:
git remote add skeleton https://github.com/Berkeley-CS61B/skeleton-sp24.git
然后,再次运行
git remote -v
,确保你看到两行对应于
origin
和两行对应于
skeleton
。