Keeping Git branches tidy

Open source GitHub

Following the GitHub SOP tidying branches needs to happen both on GitHub and locally and this details some of the ways to do that.

Zoë Turner true
2022-04-13

In the GitHub SOP we detailed how

Branches should be deleted as soon as possible

and this is good practice both locally and on GitHub. This short post details the ways to do this.

Delete after a merge

Deleting on GitHub is very easy and doesn’t require typing to the command line (although that is possible too). After merging a button will appear in purple saying Delete branch but it’s also possible to set this as the default for the repository. In this case the purple button will say Restore branch.

Go to the Settings for the repository and then scroll down to the Pull Requests section. Select the Automatically delete head branches.

More information on this can be found on GitHub help pages

Deleting stale branches in GitHub

Even if a pull request hasn’t occurred it’s possible to use GitHub to tidy the branches

Screenshot highlighting where to find the branches link on GitHub

It’s possible to delete branches, particularly “Stale” ones using the bin icon to the far right of the branch you want to delete.

Restoring a deleted branch

Note that the branch will appear with a Restore button when first deleted but this disappears after the page is refreshed or some time has passed.

Local branches

Once a branch is deleted remotely that doesn’t automatically mean the local branches have gone. Even after a pull request to another branch (like main) so to tidy up locally it’s advisable to use the Terminal if you want do to do this in RStudio and type:

git branch -d <name-of-branch>

Removing stale connections

If a branch is deleted remotely it will still appear in the local git branches

Screenshot of branch drop down menu from RStudio with the remote branches highlighted

To remove these stale connections type in the Terminal

git remote prune origin

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Citation

For attribution, please cite this work as

Turner (2022, April 13). CDU data science team blog: Keeping Git branches tidy. Retrieved from https://cdu-data-science-team.github.io/team-blog/posts/2022-04-13-keeping-git-branches-tidy/

BibTeX citation

@misc{turner2022keeping,
  author = {Turner, Zoë},
  title = {CDU data science team blog: Keeping Git branches tidy},
  url = {https://cdu-data-science-team.github.io/team-blog/posts/2022-04-13-keeping-git-branches-tidy/},
  year = {2022}
}