32

I am about to complete a tedious process of converting "dumb snapshots" to git. This process has been going very well (thanks to this rename process), but now I realized that some of the branches that I created, do not merit a branch but rather a tag.

Since everything is still local (never pushed to a repository), I found this question (and associated answer) somewhat more cumbersome than I prefer, so I was wondering whether I can take a shortcut via some simple "convert-from-branch-to-tag" command?

Is there such a simple command to convert a branch to a tag?

(I know I can just leave it as is, but I really like the way gitk highlights tags, helping me easily identify them).

UPDATE: Thanks to @Andy's answer below, I managed to come up with a shell script that does it all conveniently and painlessly. I am sharing this script for the benefit of all and as special thanks to this great community who made moving from CVS to git possible for me:

#!/bin/sh

BRANCHNAME=$1
TAGNAME=$2

echo "Request to convert the branch ${BRANCHNAME} to a tag with the same name accepted."
echo "Processing..."
echo " "

git show-ref --verify --quiet refs/heads/${BRANCHNAME}
# $? == 0 means local branch with <branch-name> exists. 

if [ $? == 0 ]; then
   git checkout ${BRANCHNAME}
   git tag ${BRANCHNAME}
   git checkout master
   git branch ${BRANCHNAME} -d
   echo " "
   echo "Updated list branches, sorted chronologically: "
   echo "---------------------------------------------- "
   git log --no-walk --date-order --oneline --decorate $(git rev-list --branches --no-walk) | cut -d "(" -f 2 | cut -d ")" -f 1
else
   echo "Sorry. The branch ${BRANCHNAME} does NOT seem to exist. Exiting."
fi
Daniel
  • 7,006
  • 7
  • 43
  • 49
WinWin
  • 7,493
  • 10
  • 44
  • 53
  • [Here I described an effective method to convert SVN tag-branches to git tags](http://stackoverflow.com/a/20807602/815781) – Onlyjob Dec 27 '13 at 20:53
  • 1
    *"Is there a simple command..."* - nearly nothing in Git is simple or matches basic workflows, so that one is liikely easy to answer: NO. What ever follows *simple* does not matter :) – jww Sep 07 '16 at 22:11

3 Answers3

44

The answers given are basically correct.

As tags and branches are just names for objects, there is a simpler way without touching current work area:

git tag <name_for_tag> refs/heads/<branch_name> # or just git tag <name_for_tag> <branch_name>
git branch -d <branch_name>

Or even do it to remote server without touching local repository at all:

git push origin origin/<branch_name>:refs/tags/<tag_name>
git push origin :refs/heads/<branch_name>
kauppi
  • 16,966
  • 4
  • 28
  • 19
19

Was there separate development on these branches? (the post you linked to, doesn't appear to have development on those branches) If there was no development, you could:

  1. Checkout the branch git checkout branchName.
  2. Tag it with git tag tagName.
  3. Switch back to master git checkout master.
  4. Finally, delete original branch with git branch branchName -d.

This can also be done if there was development on the branch, but you will need to use -D instead of -d. I'm not a git pro though, so not sure if that is an "acceptable" way to leave a branch.

Andy
  • 44,610
  • 13
  • 70
  • 69
  • Thank you. No, there was no development on those branches whatsoever. The `git checkout ` and the `-d` at the last step are what scared me, but I now understand that there is no other way. So I will have to experiment with this on a safe copy of the entire git (local) repository. – WinWin Jul 12 '11 at 16:16
  • 2
    If there was no development on that branch a tag is every bit as good as a branch (in the end they are both just pointing to a commit). Using the _little_ d will ensure that you can safely delete the branch and not lose work. – Andy Jul 12 '11 at 16:27
  • I just finished verifying that your 4-steps process works for me. See the shell script I wrote that reflects that. Thanks again and answer accepted now. – WinWin Jul 12 '11 at 20:16
  • Isn't it better to use annotated tags? git tag -a tagName – Quantum7 Apr 22 '13 at 23:46
  • @Quantum7 I don't think one is better than the other. They each have their own purpose. – Andy Apr 24 '13 at 21:39
  • don't forget to `git push --tags` – Alain May 12 '16 at 02:08
  • It's unnecessary to checkout the branch first (as can be seen by the other answers). – Zitrax Feb 28 '17 at 09:20
2

Per Andy's answer, I've made an alias that can also be used for the same thing:

[alias]
branch2tag = "!sh -c 'set -e;git tag $1 refs/heads/$1;git branch -D $1' -"

Usage

If you want to convert branch bug-2483 to a tag (while your main branch is master) write:

git branch2tag bug-2483 master

UPDATE 1

Changed to reflect the solution proposed by kauppi.

VitalyB
  • 12,397
  • 9
  • 72
  • 94