Git远程分支被删除,如何使用新分支重新同步本地更改

时间:2021-05-08 23:45:29

I got a remote branch as develop_1 which I was using. All my local code changes were committed to it but my lead has accidentally deleted that remote branch.

我有一个远程分支作为我正在使用的develop_1。我的所有本地代码更改都已提交给它,但我的导致意外删除了该远程分支。

I have all those code changes in my local system. Now I want to push all those changes back to that same Git repository in a new remote branch, say develop_2.

我在本地系统中进行了所有这些代码更改。现在我想将所有这些更改推回到新远程分支中的同一个Git存储库,例如develop_2。

How can I create a new branch, sync my local changes and push it to remote?

如何创建新分支,同步本地更改并将其推送到远程?

3 个解决方案

#1


1  

Within your local branch develop_1 you could simply create the new branch:

在您的本地分支develop_1中,您可以简单地创建新分支:

$ git checkout -b develop_2

Push your changes and then to keep your local repo in sync by running:

推送您的更改,然后通过运行以保持本地存储库同步:

$ git fetch -p

The -p is for prune deleting local branches, in this case, the old develop_1 that don't exist in the remote anymore.

-p用于修剪删除本地分支,在这种情况下,是远程不存在的旧develop_1。

#2


0  

but my lead has accidentally deleted that remote branch

但我的领导不小心删除了那个远程分支

If this is on GitHub, you can get back the SHA1 of the remote branch with the "poor man's reflog", aka the push events (GitHub Events API).
See "Does github remember commit IDs?": look for any recent push events on the master branch: you can then fetch that commit (and its associated history) back to your local repo.
If not, the GitHub support will have a look in order to restore your previous content.

如果这是在GitHub上,您可以使用“穷人的reflog”(即推送事件(GitHub Events API))取回远程分支的SHA1。请参阅“github是否记得提交ID?”:在主分支上查找最近的推送事件:然后,您可以将该提交(及其关联的历史记录)提取回本地存储库。如果没有,GitHub支持将查看以恢复以前的内容。

A forced push is then needed to restore the same history on the remote side.

然后需要强制推送以在远程端恢复相同的历史记录。

But even simpler, if your local changes were done on top of what was already pushed, you don't have to create a new branch: push your existing branch back.

但更简单的是,如果您的本地更改是在已推送的基础上完成的,则无需创建新分支:将现有分支推回。

#3


0  

Have you forked the remote repo and then cloned it on your local?

您是否已将远程仓库分叉,然后将其克隆到您的本地?

If you have code changes on your local branch say local_dev and you want to push to develop_2 then command be like:

如果您在本地分支上有代码更改,请说local_dev并且您想要推送到develop_2,那么命令如下:

git push -u origin local_dev:develop_2 then raise the pull request against the main repo.

git push -u origin local_dev:develop_2然后针对主仓库提出拉取请求。

If you have direct access to remote repo then directly push the changes to the remote. No need to raise the pull request

如果您可以直接访问远程仓库,则直接将更改推送到远程。无需提出拉取请求

#1


1  

Within your local branch develop_1 you could simply create the new branch:

在您的本地分支develop_1中,您可以简单地创建新分支:

$ git checkout -b develop_2

Push your changes and then to keep your local repo in sync by running:

推送您的更改,然后通过运行以保持本地存储库同步:

$ git fetch -p

The -p is for prune deleting local branches, in this case, the old develop_1 that don't exist in the remote anymore.

-p用于修剪删除本地分支,在这种情况下,是远程不存在的旧develop_1。

#2


0  

but my lead has accidentally deleted that remote branch

但我的领导不小心删除了那个远程分支

If this is on GitHub, you can get back the SHA1 of the remote branch with the "poor man's reflog", aka the push events (GitHub Events API).
See "Does github remember commit IDs?": look for any recent push events on the master branch: you can then fetch that commit (and its associated history) back to your local repo.
If not, the GitHub support will have a look in order to restore your previous content.

如果这是在GitHub上,您可以使用“穷人的reflog”(即推送事件(GitHub Events API))取回远程分支的SHA1。请参阅“github是否记得提交ID?”:在主分支上查找最近的推送事件:然后,您可以将该提交(及其关联的历史记录)提取回本地存储库。如果没有,GitHub支持将查看以恢复以前的内容。

A forced push is then needed to restore the same history on the remote side.

然后需要强制推送以在远程端恢复相同的历史记录。

But even simpler, if your local changes were done on top of what was already pushed, you don't have to create a new branch: push your existing branch back.

但更简单的是,如果您的本地更改是在已推送的基础上完成的,则无需创建新分支:将现有分支推回。

#3


0  

Have you forked the remote repo and then cloned it on your local?

您是否已将远程仓库分叉,然后将其克隆到您的本地?

If you have code changes on your local branch say local_dev and you want to push to develop_2 then command be like:

如果您在本地分支上有代码更改,请说local_dev并且您想要推送到develop_2,那么命令如下:

git push -u origin local_dev:develop_2 then raise the pull request against the main repo.

git push -u origin local_dev:develop_2然后针对主仓库提出拉取请求。

If you have direct access to remote repo then directly push the changes to the remote. No need to raise the pull request

如果您可以直接访问远程仓库,则直接将更改推送到远程。无需提出拉取请求