1275

We are new to git, and I want to set a tag at the beginning of our repository. Our production code is the same as the beginning repository, but we've made commits since then. A tag at the beginning would allow us to "roll back" production to a known, stable state.

So how to add a tag to an arbitrary, older commit?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
hogsolo
  • 13,565
  • 6
  • 24
  • 25
  • 1
    I came here looking for help deleting an old tag locally and on a remote and eventually found this useful, maybe it'll help somebody else too: https://gist.github.com/mobilemind/7883996 – Aleksander Lidtke Jun 15 '18 at 08:27
  • 2
    And don't forget [How to push a tag to a remote repository using Git?](https://stackoverflow.com/q/5195859/608639) because Git won't push a tag without doing something special. – jww Jul 25 '18 at 17:12

8 Answers8

1918

Example:

git tag -a v1.2 9fceb02 -m "Message here"

Where 9fceb02 is the beginning part of the commit id.

You can then push the tag using git push origin v1.2.

You can do git log to show all the commit id's in your current branch.

There is also a good chapter on tagging in the Pro Git book.

Warning: This creates tags with the current date (and that value is what will show on a GitHub releases page, for example). If you want the tag to be dated with the commit date, please look at another answer.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
dkinzer
  • 32,179
  • 12
  • 66
  • 85
  • 50
    You can also do this in the right-click menu in gitk - convenient if that's how you're finding the SHA1 anyway. – Cascabel Dec 10 '10 at 03:40
  • 144
    Omit the `-a` and the `-m "Message here"` parts if you don't want to add a message: `git tag v1.2 9fceb02` – devius Dec 07 '15 at 10:53
  • 9
    You can use the full commit ID, too. – j08lue Jan 13 '17 at 09:19
  • 3
    @devius What's the difference between using `-a` and `-m`. The man page seems to say `-a` is implied when `-m` is used. – John Strood Aug 31 '18 at 11:52
  • 1
    @JohnStrood If you use `-a` you must pass `-m` as well, but if you only use `-m` the `-a` is implied like you said, so this response could probably omit it and it would produce the same result. In my case I wasn't interested in adding the message, so that's why I omitted both options. – devius Aug 31 '18 at 19:02
  • 26
    **`git push --tags origin master`** would push all tags on local branch to remote. Prefer **`git push origin `** to just push the created tag. Refer : https://stackoverflow.com/a/5195913/452708, https://git-scm.com/book/en/v2/Git-Basics-Tagging – Abhijeet Mar 15 '19 at 07:00
  • 1
    @JohnStrood -a stands for "annotated." An annotated tag includes the date it was tagged as well as a message. Annotated tags are the standard tags for tagging version releases. devius is showing how to make a lightweight tag that will not include a date or message. As devius notes, `-am` is equivalent to `-m` and if you don't use the -m option, -a will open an editor to enter the message. – Josiah Yoder Dec 05 '22 at 16:34
  • Also beyond scope of OP's question, but if you want to tag the current branch's HEAD, you can simply omit the commit number. – Josiah Yoder Dec 05 '22 at 16:54
242

Just the Code

# Set the HEAD to the old commit that we want to tag
git checkout 9fceb02

# temporarily set the date to the date of the HEAD commit, and add the tag
GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" \
git tag -a v1.2 -m"v1.2"

# push to origin
git push origin --tags

# set HEAD back to whatever you want it to be
git checkout master

Details

The answer by @dkinzer creates tags whose date is the current date (when you ran the git tag command), not the date of the commit. The Git help for tag has a section "On Backdating Tags" which says:

If you have imported some changes from another VCS and would like to add tags for major releases of your work, it is useful to be able to specify the date to embed inside of the tag object; such data in the tag object affects, for example, the ordering of tags in the gitweb interface.

To set the date used in future tag objects, set the environment variable GIT_COMMITTER_DATE (see the later discussion of possible values; the most common form is "YYYY-MM-DD HH:MM").

For example:

$ GIT_COMMITTER_DATE="2006-10-02 10:31" git tag -s v1.0.1

The page "How to Tag in Git" shows us that we can extract the time of the HEAD commit via:

git show --format=%aD  | head -1
#=> Wed, 12 Feb 2014 12:36:47 -0700

We could extract the date of a specific commit via:

GIT_COMMITTER_DATE="$(git show 9fceb02 --format=%aD | head -1)" \
git tag -a v1.2 9fceb02 -m "v1.2"

However, instead of repeating the commit twice, it seems easier to just change the HEAD to that commit and use it implicitly in both commands:

git checkout 9fceb02 

GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" git tag -a v1.2 -m "v1.2"
Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 6
    @ColonelPanic, your wish is my command! https://github.com/lucasrangit/git-custom-commands/blob/master/git-backtag – Lucas Feb 02 '17 at 22:15
  • If you use Github to manage releases and care about the date shown on your project's /tags page, setting the GIT_COMMITTER_DATE is particularly important! – Lesley Jul 18 '17 at 11:27
  • 2
    @ColonelPanic I gave you an upvote for the pun, and wish I could give you a second one for the code, but a github star will have to do. – andyhasit Nov 09 '18 at 13:05
  • 1
    On VSCODE (windows), why I got this error? `The term 'GIT_COMMITTER_DATE=$(git show --format=%aD | head -1)' is not recognized as the name of a cmdlet, function, script file, or operable program.` – Muhammad Yasirroni Nov 03 '21 at 10:11
  • @MuhammadYasirroni Those commands are Linux shell commands, and will not work on Windows. – Phrogz Nov 12 '21 at 14:20
  • 1
    @Phrogz thanks, [this answer](https://stackoverflow.com/a/54562752/11671779) solved it for windows. – Muhammad Yasirroni Nov 14 '21 at 14:36
203

The simplest way to do this is:

git tag v1.0.0 f4ba1fc
git push origin --tags

with f4ba1fc being the beginning of the hash of the commit you want to tag and v1.0.0 being the version you want to tag.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
PatrickNLT
  • 4,075
  • 1
  • 25
  • 32
69

OK, You can simply do:

git tag -a <tag> <commit-hash>

So if you want to add tag: 1.0.2 to commit e50f795, just simply do:

git tag -a 1.0.2 e50f795

Also you add a message at the end, using -m, something like this:

git tag -a 1.0.2 e50f795 -m "my message"

After all, you need to push it to the remote, to do that, simply do:

git push origin 1.0.2 

If you have many tags which you don't want to mention them one by one, just simply do:

git push origin --tags

to push all tags together...

Also, I created the steps in the image below, for more clarification of the steps: creating tag on a commit hash

You can also dd the tag in Hub or using tools like SourceTree, to avoid the previous steps, I logged-in to my Bitbucket in this case and doing it from there:

  1. Go to your branch and find the commit you want to add the tag to and click on it:

find your commit in bitbucket

  1. In the commit page, on the right, find where it says No tags and click on the + icon:

find where it says No tags

  1. In the tag name box, add your tag:

add tag name

  1. Now you see that the tag has successfully created:

enter image description here

Alireza
  • 100,211
  • 27
  • 269
  • 172
12

This is an old question, and the answers already given all work, but there's also a new option which can be considered.

If you're using SourceTree to manage your git repositories, you can right-click on any commit and add a tag to it. With another mouseclick you can also send the tag straight to the branch on origin.

Stephan Bijzitter
  • 4,425
  • 5
  • 24
  • 44
7

Building upon the answers of the others, here is a one-liner solution that sets the tag date to when it actually happened, uses annotated tag and requires no git checkout:

tag="v0.1.3" commit="8f33a878" bash -c 'GIT_COMMITTER_DATE="$(git show --format=%aD $commit)" git tag -a $tag -m $tag $commit'
git push --tags origin master

where tag is set to the desired tag string, and commit to the commit hash.

stason
  • 5,409
  • 4
  • 34
  • 48
6

The answer by @Phrogz is great, but doesn't work on Windows. Here's how to tag an old commit with the commit's original date using Powershell:

git checkout 9fceb02
$env:GIT_COMMITTER_DATE = git show --format=%aD | Select -First 1
git tag v1.2
git checkout master
ccoxtn
  • 1,492
  • 2
  • 12
  • 14
  • When I use Powershell (instead of git bash) to checkout the commit for tagging, it throws: `fatal: reference is not a tree` – trevor4n Nov 11 '22 at 16:54
4

To tag a specific commit, print commits hashes first to view what commit that you want to add tag to it

git log --oneline

The output would like this:

dee93fc update App.js
c691fa2 autherization to roles
559528a modify depart
6aa4ad4 edit project page

Select commit id that you want to add tag to it and git checkout for commit id

git checkout 6aa4ad4

And add tag for that commit

git tag v1.0

And return to your branch After make this tag

git checkout branchName

To view all tags

git tag
Bernard Swart
  • 430
  • 6
  • 7
Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34