Pragmatism in today's world

Fixing two minor git irritations in my OSS fork workflow

For OSS work, I work with two git remotes:

  • upstream: the canonical OSS project repository, where main is the default branch and is what gets released.
  • origin: a personal fork. All my feature branches live here.

I set up locally like this:

  1. Fork the project repo to my account.
  2. Clone my fork locally, so the origin remote is my fork.
  3. Set an upstream remote using git remote add upstream <ssh-url>.

and then when working on something, I tend to do these steps:

  1. Switch to the main branch with git switch main.
  2. git pull from upstream to pick up the latest code.
  3. Create a new feature branch from main using git switch -c <branch>.
  4. Do work, then commit and push the feature branch to origin.
  5. Open a PR from origin/<branch> against upstream/main (usually using gh).
  6. Once merged upstream, pull from upstream back into my local main branch and delete my feature branch.

The problems

I have a couple of little irritations with this workflow that I finally sat down to solve.

  • Firstly, when I git pull on main, it pulls from origin by default unless I remember to use git pull upstream main. If I don’t catch it, I don’t have the latest code when I branch for my new work.
  • Secondly, when I push my new branch, I get an error message fatal: The current branch foo has no upstream branch which is just irritating as I then sigh and re-run the push with -u origin.

The solutions

To remove these minor irritations, I discovered that I can run these useful git commands:

Firstly, set main to track upstream/main:

git branch --set-upstream-to upstream/main main

Usually I set which repo’s branch to track using git push -u, but that doesn’t make sense for the main branch as I never push it to the upstream repo. Using --set-upstream-to on the branch command does the same thing, without a push.

Secondly, to make the first git push automatically create the origin/<branch> and track it:

git config push.autoSetupRemote true

Now git pull on main always pulls from upstream, and git push on a new branch just works.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *