如何在离线时快进一个分支

How to fast forward a branch when being offline

当我 git fetch 在我在线时编辑,现在我离线并且想快进到我之前获取的状态时,我如何快进分支。

例如,我处于离线状态并执行以下操作:

$ git checkout develop
Switched to branch 'develop'
Your branch is behind 'origin/develop' by 37 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

尝试使用 git pull 时,我得到:

$ git pull
ssh: Could not resolve hostname <hostname>: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

在这种情况下我需要使用什么命令?

因为 git pull 基本上是 git fetch + git merge 并且你已经做了 fetch 并且离线时不能再做,你只需要做 git merge origin/develop.

或者你可以做 git reset --hard origin/develop 即使没有快进也能工作。

编辑:更多选项。 另一种方法是在切换到它之前执行 git branch -f develop origin/develop - 你不能改变当前分支,但你可以 "update" 任何分支到任何状态,只要它不是当前的。对于当前分支,您必须使用 git reset.

git pull is a shortcut for git fetch followed by git merge.

Only git fetch accesses to the remote repository (and requires a network connection if the remote repository is on a different computer). git merge 仅对您计算机上已存在的数据进行操作;它根本不关心你的网络连接。

既然你已经做了 git fetch all you need is to run the other half: git merge

作为上述答案的替代方案:

您可以直接签到您想去的分行:

git checkout origin/develop

然后删除旧的本地分支:

git branch -D develop

并在新提交时重新创建它,如下所示:

git checkout -b develop