简体   繁体   中英

Undo git push --delete <branch>

I have done the following:

  1. Cloning a repo
  2. git push origin --delete Branch_Name

and branch is deleted.

Is there any way I can restore this? I did not checkout this branch before deletion so there is no local copy.


git reflog is useless I guess cause I did not checkout the branch and I also don't know the commit_id of last commit on this branch

Generally, the only way to recover from this is to find a repository—often one used by a colleague / friend / co-worker—who has the right commit(s), or to use the server's backups (your server does have backups made and tested regularly, right? Right?!? 😱). The reason for this is:

  • Server-side Git repositories have reflogs disabled by default.
  • Even if not, deleting the server's branch name with git push --delete deletes any relevant reflog the server had.
  • It deletes your own remote-tracking branch as well: your origin/ branch is gone, along with any reflog you had for that.

The branch-name deletion may not have triggered a git gc in the server (this is not predictable) and definitely did not trigger one in your own repository (this, fortunately, is ), so if you know the right hash ID , you can run:

git push origin <hash>:refs/heads/branch

but you usually don't know the right hash ID: that information was in the branch name on the server (gone now), your remote-tracking name (gone now), and any relevant reflogs in your and/or the server's repository (all of which are gone now).

That information does exist in any colleague's clone, provided that they (a) have run git fetch origin relatively recently and (b) have not run git fetch origin in such a way that their Git also deleted the information. This is the only reason I know of that one might not want to set fetch.prune to true : it causes your own Git repository to retain stale remote-tracking names, in case one of your colleagues executes this particular stunt.

Since responsible server sites make regular backups, though, you can always recover the information from a backup.

如果您在第 1 步之后不久执行了第 2 步,您可以尝试git push origin origin/[Branch_Name]:refs/heads/[Branch_Name]从最可能的提交中重新创建分支。

Is there any other developer who has a local copy of the branch? If any, ask her/him to push that branch.

Details:

The local repository keeps the latest commit object for that branch, but it's difficult to identify the commit because the information about the commit-branch relationship has been lost by git push origin --delete [Branch_Name] . However, if you can find the commit from the list of commit objects, you can checkout and regenerate the branch.

In many cases it's faster to ask other developers to push the branch from their repository, if any.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM