Git rid of old branches :-)

Toss ’em out!

Every so often, I clean up old working branches I don’t need any more.

After I’ve written or revised content, and the pull request has been merged from my fork of the repo into the main branch of the organization’s repo, it’s time to get rid of the old working branches.

TLDR/Copy and paste

Here’s a summary of the commands for you to copy and paste.

cd <repo-directory>/
git checkout <main branch>
git fetch upstream <main branch>
git branch --merged
> Make sure the branch is dead
git branch -D <branch-name>
git push origin :<branch-name>

Verbose

Sections:

  • List the merged branches
  • Make sure the working branches are really dead
  • Delete the dead branches
List the merged branches

I start by listing the merged branches in my local repo:

$ cd openshift-docs/       # Change to the project/repo directory
$ git checkout main        # Check out the main or master branch
Already on 'main'
Your branch is up to date with 'upstream/main'.
$ git fetch upstream main  # Fetch information about branches from your upstream repo 
remote: Enumerating objects: 46, done.
remote: Counting objects: 100% (36/36), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 19 (delta 15), reused 12 (delta 10), pack-reused 0
Unpacking objects: 100% (19/19), 3.11 KiB | 176.00 KiB/s, done.
From github.com:openshift/openshift-docs
 * branch                main     -> FETCH_HEAD
   54901a001..b051f3c46  main     -> upstream/main
$ git branch --merged      # List local branches that are merged in the upstream repo
  RHDEVDOCS-2465-replace-docker
  RHDEVDOCS-2514
  RHDEVDOCS-2609
  RHDEVDOCS-2618
  RHDEVDOCS-2740-rn
  bz#1873372
* main
Make sure the working branches are really dead

Early on, when I start a new project, I use the ID of the Jira or GitHub issue to name the working branch and PRs. This makes it simple to know which issue, branch, and PR belong to each other.

When I’ve finished using a working branch, to make sure I don’t need it any more, I search my closed pull requests for the issue ID. I review the pull request to make sure that the PR that was merged into the main branch was also cherry-picked into all relevant release branches. I also look at the issue to make sure its state is “closed.”

When I’ve confirmed that a branch is not only merely dead, but really most sincerely dead, it’s time to…

Delete the dead branches

I return to my terminal and delete the branch by entering:

$ git branch -D <branch-name>

Then I push the deletion to origin by entering:

$ git push origin :<branch-name>

Notes

  • Many repos still use master as the name of their primary branch. In this post, I have changed the name to main and will continue to do so in future posts.
  • Please share your comments, questions, and suggestions for improving this post, below!

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s