您可以手动以格式下载缺失的提交,并使用手动应用它们。例如:
github
(repository you linked to):
$ wget https://github.com/yurybond/stackowerflow-rocks/commit/a1c1540abd453773b3ce6445d01e51ad336bbe84.patch && git am a1c1540abd453773b3ce6445d01e51ad336bbe84.patch
gitlab
:
$ wget https://git.weboob.org/weboob/devel/commit/bba7e1b8ffb0743b57f202cf9cdb43fda209fa43.patch && git am bba7e1b8ffb0743b57f202cf9cdb43fda209fa43.patch
bitbucket
:
$ wget https://bitbucket.org/Kasreyn/linux-3-9-rc3-moxart/commits/434e8f69db2c3effdc8741139adb722a68dfcccd/raw -O 434e8f69db2c3effdc8741139adb722a68dfcccd.patch && git am 434e8f69db2c3effdc8741139adb722a68dfcccd.patch
请注意,
git am
不仅会应用文本补丁,还会在当前分支上重新创建一个完整的提交。
如果您从远程仓库中删除了一个分支,则没有办法从远程仓库中恢复它。然而,通常情况下,由于
git reflog
的存在,您仍然可以在本地恢复已删除的分支。请参考以下示例。
首先,创建一个新的非裸库:
$ git init
Initialized empty Git repository in /tmp/reflog-test/.git/
在 master
分支上添加 file
并创建一个新的提交:
$ touch file
$ git add .
$ git commit -m 'Initial commit'
[master (root-commit) 81fc76d] Initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file
切换到一个名为new-branch
的新分支:
$ git checkout -b new-branch
Switched to a new branch 'new-branch'
修改文件
并提交更改:
$ echo new-branch >> file
$ git commit -am 'commit on new-branch'
[new-branch 9c457c6] commit on new-branch
1 file changed, 1 insertion(+)
切换回 master
分支:
$ git checkout -
Switched to branch 'master'
$ git branch
* master
new-branch
删除
new-branch
。
$ git branch -D new-branch
Deleted branch new-branch (was 9c457c6).
已经消失:
$ git branch
* master
$ git log new-branch
fatal: ambiguous argument 'new-branch': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
然而,您仍然应该能够引用提交记录:
$ git show --stat 9c457c6
commit 9c457c69de8a54376a2614ca8cfc0f515c64676c
Author: Arkadiusz Drabczyk <adrabczyk@bigcorp.com>
Date: Tue Apr 3 10:43:49 2018 +0200
commit on new-branch
file | 1 +
1 file changed, 1 insertion(+)
即使在任何分支中都找不到,下面的命令也不会返回任何内容:
$ git branch --contains 9c457c6
Reflog
显示了在仓库中执行的所有操作:
$ git reflog
dbc721a HEAD@{0}: checkout: moving from new-branch to master
9c457c6 HEAD@{1}: commit: commit on new-branch
dbc721a HEAD@{2}: checkout: moving from master to new-branch
dbc721a HEAD@{3}: commit (initial): Initial commit
正如您所看到的,它还有我们在new-branch
上进行的9c457c6
提交。
只有当reflog
过期并运行垃圾收集器时,9c457c6
才会变得不可达:
$ git reflog expire --expire=all --all
$ git gc --prune=now
Counting objects: 3, done.
Writing objects: 100% (3/3), done.
Total 3 (delta 0), reused 0 (delta 0)
$ git show 9c457c6
fatal: ambiguous argument '9c457c6': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
默认情况下,reflog
条目在90天后过期,并且在某些命令之后自动运行git gc
,因此如果您最近删除了分支,则应该能够恢复它。
wget https://github.com/yurybond/stackowerflow-rocks/commit/a1c1540abd453773b3ce6445d01e51ad336bbe84.patch && git am a1c1540abd453773b3ce6445d01e51ad336bbe84.patch
。 - Arkadiusz Drabczykfatal: reference is not a tree
消息来自哪里。你使用submodules
吗? - Arkadiusz Drabczyk