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.
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.
This makes sense as we can dereference the HEAD pointer to the branch name, as seen by
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!