Git Branches and Tags

Introduction

This page contains a quick reference to create and work with branches and tags in Git. This is part 3 of the Git quick reference series.

Git Branches

Creating, Deleting and Switching Branches

Create a new Branch from current place in Repository

Create a new Branch from another Branch, Tag or Commit

Switching Branches

Create a Branch and Switch to it

Create Local Branch and Push to Remote Repository

The following creates a new branch called branch_name , and pushes it to the remote directory. The -u  option is needed to link the local branch to the remote branch.

Delete a Branch

It is common for Git projects to delete branches once no longer needed. Since tags and branches point at the same commit, you can always re-create a branch from a tag at a later point.

The git branch tool deletes a branch with the -d flag.

If you have not merged the branch with the current branch, git branch -d will fail. You can override this using the -D flag.

Delete a remote branch

Once the branch has been deleted locally, you also need to remote it from the local repo:

Alternatively, you can also use the :branch_name convention. It is like pushing an empty local branch into a remote branch, hence removing it.

Prune old local branches

A confusing “feature” of git is that it does not remove local branches once they have been removed from the remote repo. This might lead to a local repo with many old and untracked branches.

You can run this command to remove local branches that no longer have a remote counterpart:

or

Viewing Branches

List Local Branches

List Remote Branches

List All Branches

List all Merged / Non-Merged Branches

List Branches Matching a Name

View Branch Commit Info

View Branch Commit Info and Upstream Branch

View Latest Commit Info from All Branches (Local/Remote)

Git Tags

The git tag tool creates a read-only marker in the repository. Tags are similar to branch names, with the difference that you cannot check them out or commit to them. You can however create branches from a tag.

There are two types of tags:

  • Annotated tags – for release tags
  • Lightweight tags – for private or temporary labels

Create a Lightweight Tag

Create an Annotated Tag

Please note that -m flag implies an annotated flag.

List Tags

List Tags with their Message

The -n flag can be used to display the tag message. The commit message is displayed if it is a lightweight tag or an annotated tag without a message.

Filter Tags

Delete a Tag

Create a Branch from a Tag

Pushing Tags

You can push tags to a remote repository either one by one:

or all tags to a remote repository.

Fetching Tags

Tags are usually fetched automatically.  If a tag is not fetched, you can use the –tags flag.

Bibliography

  • Git Reference: http://git-scm.com/doc
  • Pro Git. Scott Chanon and Ben Straub. APress. http://git-scm.com/book/en/v2
  • Pragmatic Guide to Git. Travis Swicegood. Pragmatic Bookshelf, 2010.
  • Version Control with Git. Jon Loeliger and Mattew McCullough. O’Reilly, 2012

Git Tools

The following two tabs change content below.

Eduard Manas

Eduard is a senior IT consultant with over 15 years in the financial sector. He is an experienced developer in Java, C#, Python, Wordpress, Tibco EMS/RV, Oracle, Sybase and MySQL.Outside of work, he likes spending time with family, friends, and watching football.

Latest posts by Eduard Manas (see all)

Leave a Reply