So this is a bit silly, but yesterday I merged a branch where the target branch seems to have been deleted at some point and the source branch has been configured to be removed after a merge. I am a bit confused why Gitlab lets me do this in the first place, but now I am wondering whether the changes are lost.
Since I still have the changes locally, I think I could relatively easily branch off the branch I should have merged into to begin with, however out of curiosity - and because it would save me some embarassment, frankly, I'm still wondering whether there is no other solution here.
If it helps, I still have the wrong, now non-existent target branch locally on my machine. Pulling it yields:
Your configuration specifies to merge with the ref 'refs/heads/wrong-target-branch'
from the remote, but no such ref was fetched.
The merged branch also still exists locally on my local machine.
For the source branch, there are two different workflows that can be followed. In the first workflow, a user creates a branch for pending changes, makes commits, and then creates a merge request from this branch to the target branch. Once these pending changes are reviewed and merged, the branch is deleted as it has served its purpose. When the user plans to make new changes, they can simply create another branch for the new pending changes.
On the other hand, in the second workflow, a user creates a long-lasting branch for pending changes. Even after a merge request is accepted, the branch remains. The user then updates the branch by pulling the target branch, makes new commits, and creates new merge requests.
If you prefer the second workflow, you can go to the repository Settings -> General -> Merge requests, and uncheck the option "Enable 'Delete source branch' option by default", so that the source branch will not be deleted unexpectedly. The option is checked by default when the repository is created.
If any branch has been unexpectedly deleted and you still have the necessary data in the local repository, you can recreate it by using the command git push origin $commit:$branchname
. The commit is the head at which the branch was pointing. If you want to revert a merge, you can also use a similar command git push origin -f $commit:$branchname
or git push origin --force-with-lease $commit:$branchname
. The commit is the one you expect the branch to point at. If you don't want to rewrite the history, you can also try git revert -m.