Changes on remote branch made by last two PR were lost by a accidental push from the local branch that was behind the corresponding remote by several commits. The branch was not updated before that push. Is it possible to restore missed commits from closed and previously merged PRs? For example: by restoring old branches in them and by making new PRs (that will be exactly the same as previous ones) based on these branches.
CodePudding user response:
Changes on remote branch made by last two PR were lost by a accidental push from the local branch that was behind the corresponding remote by several commits.
Well, it wasn't entirely accidental. Someone had to force push in order to do that. Force pushing a shared branch is frowned upon, and should generally be avoided. Fortunately it's extremely easy to put it back to how it was before.
Note that a branch is simply a pointer to a commit. Suppose the branch was previously pointing to commit X
. Someone who had an older version of the branch, say, pointing to commit Q
, force pushed the branch and effectively removed all commits after Q
up through X
. The solution is to simply repoint the branch to X
again and push it out. This time it won't even have to be a force push because X
is ahead of Q
. So first, figure out what the commit ID of X
is (either by looking on someone else's machine) or at the last PR that was completed- it should show the merge commit ID when the PR was completed. Then from anyone's repo:
git fetch
git switch main # change main to the branch name to fix
git reset --hard <previous-commit-id>
git push
If you want to be fancy, you could combine the switch and reset commands into one, with:
git fetch
git switch -C main <previous-commit-id> # change main to the branch name to fix
git push
The switch
command can be used to create a new branch with the -c
option, or -C
if the branch already exists, and it can be set to start at any commit ID (or branch name).