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.
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.
How can I create a new branch, sync my local changes and push it to remote?
Within your local branch develop_1 you could simply create the new branch:
$ 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.
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.
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.
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:
git push -u origin local_dev:develop_2
then raise the pull request against the main repo.
If you have direct access to remote repo then directly push the changes to the remote. No need to raise the pull request
来源:https://stackoverflow.com/questions/51317667/git-remote-branch-got-deleted-how-to-resync-local-changes-with-new-branch