DEV Community

Saurabh Kumar
Saurabh Kumar

Posted on • Updated on

Git Tags .FTW

How they make life simpler

Alt Text

Tagging

is an additional mechanism used to create a snapshot of a Git repo. Typically, it is used to create semantic version number identifier tags like release-20.0.7 ;) that correspond to software release cycles.
Basically = A fixed point in git commit history

Eg: I want to migrate my existing financial statements microservice project from h2 -> mongodb, how can I have the current fully functional h2 version for future


Solution : make a tag from the current version of my project

Type of Tags

  1. Lightweight version and mostly good for private projects like mine (they don’t have any message or info like who did what)
    ➜ git tag v0.2

  2. Annotated version has message like commit and can have more info, simply put — metadata
    ➜ git tag -a v0.2 -m “working h2 version of app”

If you don’t use -m, a text editor will open up where you can enter your required info

By default git tag takes the current head as fixed point in time, but you can have your own commit as the point like this

➜ git tag -a v1.2 15027957951b64cf874c3557a0f3547bd83b3ff6

Also, it's possible to overwrite the tag just like world burning push -f command

➜ git tag -a -f v1.2 15027957951b64cf874c3557a0f3547bd83b3ff6

preferred : annotated

How to push tags

➜ git push origin v0.1
Enter fullscreen mode Exit fullscreen mode

How to fetch tags

➜ git pull
Enter fullscreen mode Exit fullscreen mode

Or if the remote tags are not available try

➜ git fetch --all --tags
Enter fullscreen mode Exit fullscreen mode

How to checkout tags to our own branch

➜ git checkout -b my-version tags/v0.1
Enter fullscreen mode Exit fullscreen mode
  • points to this tag in my new branch 'my-version

List all tags in your repo

➜ git tag
Enter fullscreen mode Exit fullscreen mode

Also you can filter on it with

➜ git tag -l `*basic`
Enter fullscreen mode Exit fullscreen mode
  • all tags having basic in them

Finally, delete a tag

➜ git tag -d v0.1 (locally)
Enter fullscreen mode Exit fullscreen mode

or

➜ git push --delete origin v0.1 (from remote)`
Enter fullscreen mode Exit fullscreen mode

A Complete Example:

`
➜ git:(master) git tag -a v0.1
➜ git:(master) git push origin v0.1
Counting objects: 1, done.
Writing objects: 100% (1/1), 270 bytes | 270.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/xxxxxx/xxxxxx.git

  • [new tag] v0.1 -> v0.1 ➜ git:(master) `

Bonus

1. You retrieve multiple tags from your remote repository. Then, retrieve the latest tag available by using the “git describe” command.


➜ tag=$(git describe — tags
git rev-list — tags — max-count=1)
➜ echo $tag
v0.2

2. Inspect the state of your branch

➜ git log — oneline — graph

`

  • b5c0345 (HEAD -> my-version, tag: v1.0-rc, tag: v0.2, tag: v0.1, origin/master, origin/HEAD, master) queryDSL example
  • 043a12c TextIndexed updated for Spring 2.3+
  • f9e007a projection and aggregation
  • 48d59eb mongo query dsl — refer README
  • a59e6ca controllers with mongo template and mongorepository
  • 9e6a36d init
  • bfa47c0 Initial commit (END) `

If you found this concise and helpful, do support with like 😊

Reference:
https://www.atlassian.com/git/tutorials/inspecting-a-repository/git-tag


Saurabh Kumar

Coder. Blogger. Mentor. Speaker.

Find more article from me @

Or checkout my photography @

Top comments (0)