本文介绍了Git:恢复已删除的(远程)分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要恢复在推送过程中以某种方式删除的两个 Git 分支.

I need to recover two Git branches that I somehow deleted during a push.

这两个分支是在不同的系统上创建的,然后推送到我的共享"(github)存储库.

These two branches were created on a different system and then pushed to my "shared" (github) repository.

在我的系统上,我(显然)在获取期间检索了分支:

On my system, I (apparently) retrieved the branches during a fetch:

~/myfolder> git fetch
remote: Counting objects: 105, done.
remote: Compressing objects: 100% (58/58), done.
remote: Total 62 (delta 29), reused 0 (delta 0)
Unpacking objects: 100% (62/62), done.
From github.com:mygiturl
 * [new branch]      contact_page -> origin/contact_page
   731d1bb..e8b68cc  homepage   -> origin/homepage
 * [new branch]      new_pictures -> origin/new_pictures

在那之后,我推动将我的本地更改发送到中央存储库.出于某种原因,这些分支已从我的本地系统和中央存储库中删除:

Right after that I did a push to send my local changes up to the central repo. For some reason, these branches were deleted from both my local system and the central repo:

~/myfolder> git push
Counting objects: 71, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (43/43), done.
Writing objects: 100% (49/49), 4.99 KiB, done.
Total 49 (delta 33), reused 0 (delta 0)
To [email protected]:mygiturl.git
 - [deleted]         contact_page
 + e8b68cc...731d1bb homepage -> homepage (forced update)
   bb7e9f2..e0d061c  master -> master
 - [deleted]         new_pictures
   e38ac2e..bb7e9f2  origin/HEAD -> origin/HEAD
   731d1bb..e8b68cc  origin/homepage -> origin/homepage
   e38ac2e..bb7e9f2  origin/master -> origin/master
 * [new branch]      origin/contact_page -> origin/contact_page
 * [new branch]      origin/new_pictures -> origin/new_pictures

从他们出生地的机器上取下树枝并不容易,所以如果可能的话,我想尝试从我的本地恢复它们.

It's not terribly easy to get the branches off of their birthplace machine, so I'd like to try and recover them from my local if possible.

我在谷歌上搜索过的所有 git撤消"信息都与恢复丢失的提交有关.我认为这不适用于这里,因为我没有为这些分支提交 UID.

All of the git "undo" information I've googled has to with recovering lost commits. I don't think that applies here, since I don't have commit UIDs for these branches.

我想知道如何取回这些.我还想知道它们最初是如何被删除的,以及我将来如何避免这种情况发生.

I'd like to know how I can get these back. I'd also like to know how they were deleted in the first place and how I can avoid this in the future.

根据要求,这是我的 repo 配置

by request, here's my repo configuration

user.name=Craig Walker
[email protected]
alias.unadd=reset HEAD
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
[email protected]:MyGitURL.git
remote.origin.mirror=true
branch.master.remote=origin
branch.master.merge=refs/heads/master
alias.undo=reset --hard
alias.test=push -f ci HEAD:master
alias.st=status
alias.ci=commit
alias.br=branch
alias.co=checkout
alias.ch=checkout
alias.df=diff
alias.lg=log -p
alias.who=shortlog -s --
remote.ci.url=ContinuousIntegrationGitURL
remote.ci.fetch=+refs/heads/*:refs/remotes/ci/*
branch.photo.remote=origin
branch.photo.merge=refs/heads/photos
remote.foo.url=FooGitURL
remote.foo.fetch=+refs/heads/*:refs/remotes/cynthia/*
branch.homepage.remote=origin
branch.homepage.merge=refs/heads/homepage

推荐答案

我不是专家.不过你可以试试

I'm not an expert. But you can try

git fsck --full --no-reflogs | grep commit

找到已删除分支的 HEAD 提交并取回.

to find the HEAD commit of deleted branch and get them back.

这篇关于Git:恢复已删除的(远程)分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:04