How to tag a Git commit id by example

I recently had the need to tag an older Git commit in the course of my studies, and I was surprised that the command wasn’t as obvious as I thought it would be, and the prospect of doing a push of the tag back to the remote GitHub or GitLab repository wasn’t straight forward either.

Git Tag a Commit Id Example

To tag a specific commit id and push back to the remote repository, follow these two steps:

  1. Tag the commit with this command:
    git tag -a M1 e3afd034 -m "Tag Message"
  2. Specify the tag in the git push command::
    git push origin M1

When the second command completes, the tagged commit id that was pushed back will be identifiable on the server.

Tag an older commit id Git commands

The commands used in this example are as follows:

tag@commit-id /c/older/commit (main)
$ git tag -a M1 e3afd03 -m “Milestone One!”

tag@commit-id /c/older/commit (main)
$ git log –oneline
b56402e (HEAD -> main, origin/main, origin/HEAD) 3rd Commit carol File
e3afd03 (tag: M1) 2nd Commit baker File
90f4ccb 1st Commit adam File
5ec4fbc Initial commit

tag@commit-id /c/older/commit (main)
$ git push origin M1
Enumerating objects: 1, done.
* [new tag] M1 -> M1

App Architecture
Software Quality
Cloud Computing