How to Create a Git Branch from a Tag |Video upload date:  · Duration: PT3M26S  · Language: EN

Quick guide to create a Git branch from a tag Learn commands to branch locally and push the branch to remote

So you found a tag that represents a pristine release snapshot and now you want to build a tiny patch or a wild experiment without destroying the timeline of human progress. Good move. Branching from a tag gives you a stable starting point to work from and keeps the history neat enough that your team will thank you instead of sending passive aggressive GIFs.

Find the tag name

First locate the tag that points to the commit you care about. If the tag is already local run this command and bask in the list of tags.

git tag

If the tag lives on the central server but not in your clone fetch remote tags with this polite request.

git fetch --tags

Pick the tag name that matches the release or build you want, for example v1.2.3. Verify which commit the tag points at if you are the paranoid but careful sort.

git rev-parse v1.2.3

Create the branch from the tag

Now make the branch. You have two simple, supported ways to do this depending on your Git version and taste for verbs.

  • Create and switch with the classic command
git checkout -b my-hotfix v1.2.3
  • Or do the same with the newer switch command
git switch -c my-hotfix v1.2.3

Either command starts your branch at the tagged commit and checks it out so you can start making changes. This beats checking out the tag by itself which leaves you in a place where new commits vanish if you blink.

Use annotated tags for releases

If this is part of a release workflow prefer annotated tags for human friendly messages and easy auditing. Annotated tags also carry a signature if you like that sort of thing.

Push the branch to the remote and set upstream

Share your branch so other humans and CI systems can see it. To push the branch do this.

git push origin my-hotfix

To push and set the upstream in one tidy move use this.

git push -u origin my-hotfix

Now collaborators can fetch and check out the branch like any other feature branch. No mystery, no phantom commits.

Avoid a detached HEAD and confirm tracking

Checking out a tag directly without creating a branch results in a detached HEAD state. That means any commits you make do not belong to a named branch and can be accidentally discarded. If you see detached HEAD in git status you are in that awkward place.

git status

After creating a branch verify it is tracking the remote branch and points to the right commit.

git branch -vv
git show --no-patch --format=%H my-hotfix

Quick checklist and developer tips

  • List tags with git tag and fetch remote tags with git fetch --tags
  • Create a branch from the tag with git checkout -b name tagname or git switch -c name tagname
  • Push and set upstream with git push -u origin name
  • Verify the tag commit SHA with git rev-parse tagname or git show tagname
  • Use a clear branch prefix such as hotfix or patch to make intent obvious

Recap The process is short and glorious. Find the tag, branch from it, push the branch, and do not leave yourself in detached HEAD limbo. Now go fix that bug or break things in a branch where it is safe to do so. If things go wrong you can always blame the tag and call it a learning experience.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.