5

Hello While merging develop branch into stage branch I've forgot that on Gitlab I have option remove source branch checked. So right now I have local outdated develop branch, and last feature branch (locally) which I've merged into develop, before the merging (and deleting) develop to stage. What can I do to completely restore deleted version of develop ?

Verthon
  • 2,937
  • 3
  • 20
  • 32
  • Not really sure I understand the question. If you have the latest commit of the feature branch, then there is nothing to restore. Are you asking how to get the commit from your local repo onto gitlab? – William Pursell Dec 18 '20 at 18:16
  • Here is the flow, `feature` branch was created from local version of develop then I've created PR `feature` => `develop` merged it, then as a next step another PR `develop` => `stage` and here I removed accidentally the `develop` branch from remote. – Verthon Dec 18 '20 at 18:22
  • Does this answer your question? [Can I recover a branch after its deletion in Git?](https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git) – matt Dec 18 '20 at 18:57
  • Hmm when I'm trying to `git checkout -b shanumber` The sha number is from gitlab I'm receiving the error `Git normally never creates a ref that ends with 40 hex characters because it will be ignored when you just specify 40-hex. These refs may be created by mistake. For example, git switch -c $br $(git rev-parse ...) where "$br" is somehow empty and a 40-hex ref is created. Please examine these refs and maybe delete them. Turn this message off by running "git config advice.objectNameWarning false" fatal: A branch named 'develop' already exists.` – Verthon Dec 18 '20 at 20:46
  • Please correct me if I'm wrong, but I think if I can do same PR again, so for this, it will be do again PR from `feature` to `develop` merge it, and the code of develop should be the same as before (before deleting develop on mergin into stage branch), however I'm not sure if it corrupt history or will provoke some conflict in future merging develop into stage branch? – Verthon Dec 18 '20 at 21:27

2 Answers2

3

I did the same. Deleted someone else's branch from a Gitlab repo that I did not have locally, so "git reflog" was no help. I then found another answer regarding recovering a Github deleted branch, Git: Recover deleted (remote) branch which suggested if you used JIRA or something like it that would have a link to the commit and sure enough the link was there and clicking on it brought me right to the commit. The link contains the SHA1 id of the commit to use in @diego-baranowski answer.

git checkout -b <branchname> <sha1>

where "branchname" can be any name you want, the same as the deleted branch name or a new name and "sha1" is the long SHA1 id of the commit.

Alternatively, you can use curl (or better yet) Postman to list the events on the GitLab server and find the "deleted" event that you did which will also have the SHA1 id in it (the "commit from" value).

curl --location --request GET 'https://gitlab.com/api/v4/events' --header 'Authorization: Bearer <your Personal Access Token here>

Returns JSON like this:

   {
    "id": xxxx,
    "project_id": xxxx,
    "action_name": "deleted",
    "target_id": null,
    "target_iid": null,
    "target_type": null,
    "author_id": xxxx,
    "target_title": null,
    "created_at": "2021-11-08T20:27:56.299Z",
    "author": xxxx,
    "push_data": {
        "commit_count": 0,
        "action": "removed",
        "ref_type": "branch",
        "commit_from": "<YOUR SHA1 ID IS HERE>",
        "commit_to": null,
        "ref": "<DELETED NAME HERE>",
        "commit_title": null,
        "ref_count": null
    },
    "author_username": "xxxx"
},
rwhirn
  • 61
  • 1
  • 6
  • Also one can found target sha using menu: Project Information->Activity (which would move you to https://...//activity ) Then scroll down to the actual commit you're searching – Pavel Nov 23 '21 at 14:08
2

Extract the commit SHA1 your branch was on with reflog. Then use the checkout command.

git reflog
git checkout -b <branchname> <sha1> 
Diego B
  • 1,256
  • 11
  • 19