If a pull request is not accepted…
As I am not very experienced with R and coding, my knowledge of using Git and GitHub is limited. Usually, I use the pr_*()
family of {usethis} functions as pull request helpers. They work very well – as long as the workflow follows the standard (successful) workflow:
- Initiate the pull request with
pr_init(<branch name>)
- Submit the changes in the new branch with
pr_push()
. - Click the “Create pull request”-button to make the PR.
- After the code is accepted and merged, conclude the process with
pr_finish()
.
This sequence does not fly if the workflow deviates from the above model. The standard procedure describes the scenario of a discussion between contributor and reviewer that finally results in the acceptance of the PR. But what about a rejection of the PR?
… deletion of the remote branch with GitHub is not enough
This situation happened to me after I proposed fixing a typo for a repo that had been frozen because of a necessary general overhaul. I thought to delete the submitted remote branch at the GitHub site would bring me back to my initial position.
But back in RStudio, it turned out that the deleted branch was still there.
Ok, I understood that my remote action did not delete the local branch too. But why was the remote branch still there? After all, I had it just removed. (I double-checked the deletion, and the remote branch at the remote GitHub site was, in fact, completely gone.
There are three, not two branches
After some research, I found an appropriate question-answer dialog (How do I delete a Git branch locally and remotely?) at StackOverflow with more than 11 million views! It seems that I am not the only one surprised by the situation.
After reading this and other answers at SO, I learned that there are three different branches:
When you’re dealing with deleting branches both locally and remotely, keep in mind that there are three different branches involved:
- The local branch X.
- The remote origin branch X.
- The local remote-tracking branch origin/X that tracks the remote branch X. Quote from the top second answer
I deleted the remote origin branch X manually at the GitHub site. The local branch but also the local remote-tracking branch were still functional!
For every one of these three branches, one needs different commands to delete them. And here – as far as I understood – we have met the limits of RStudio and/or {usethis}. For the following commands, you have to change from the console to the terminal.
Useful commands for deleting branches
If you are using the Z shell (zsh) you have to set setopt interactivecomments
for interactive use either in your ~/.zshrc
file or write this command into the terminal for temporary use during the terminal session. In contrast to ksh
(Korn shell), sh
(Bourne shell), and bash
(Bourne-again shell) the #
is for the zsh
(Z shell) only set as the comment character for non-interactive use (scripts).
Deleting a remote branch
Terminal
setopt interactivecomments
git push origin -d <branch>
Deleting a local branch
Terminal
git checkout master # change branch, you can't delete a branch you are working on
git branch -d <branch>
git branch -D <branch> # force-delete un-merged branches
Deleting a local remote-tracking branch
Terminal
git branch -dr <remote>/<branch> # 'dr' stands for "delete remotes"
git branch -dr origin/dev # example
# Delete multiple obsolete remote-tracking branches
git fetch <remote> -p # 'p' stands for "prune"
git fetch origin -p # example
Don’t forget to refresh the RStudio listing to verify that you have deleted the branches.