Learning to use your HEAD

Learning to use your HEAD

I have a saying called “Learning by teaching” which is just my way of phrasing the fact that you always learn a lot by teaching others. This happened again to me a few days ago when I was asked  a Git question. I have been extensively using and teaching Git the last years, so I feel quite confident in my abilities in this domain. I was asked about why you can’t push to your feature branch, when you’ve rebased your local branch on top of master. We had a good chat about force pushes (–with-lease), and protected branches.

But a topic that we also covered were the git push syntax. There are many good learning opportunities in this simple command, even though it is composed of simple parts. My usual starting point is git push <remote> <local-branch>:<remote-branch>, which can look intimidating to many Git novices, but can be decomposed in to understandable parts.

We first went into deleting your branch on the remote by pushing the “empty” branch to the remote branch you want to delete. For example to delete the branch my-branch on the remote origin , you can run the command git push origin :my-branch. This is using the empty string as the local branch.

But we have yet to cover my learning. I have always been a bit annoyed by the need to be verbose when no upstream branch has been set.

Running the command git push, when no upstream branch is configured gives a fatal error.
Running git push without an upstream branch configured gives an error.

The above example is reasonably easy, but many workflows involve feature/ and some form of issue id and task description as part of the branch name, and then it becomes a bother. But my colleague Christian that I had been discussing this with just did using HEAD instead of the branch name.

Using HEAD instead of branchname shortens the set-upstream flag to Git push

This makes sense as we can dereference the HEAD pointer to the branch name, as seen by cat .git/HEAD.

This way of setting the upstream branch is much easier for me to grok and I have now become more proficient in Git. Thank you!

2 thoughts on “Learning to use your HEAD”

  1. Hi Johan, it seems that the images are missing which makes it hard(er) to understand the text…


Leave a Reply

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s