Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Git tag only for a specific branch?

Probably a very basic question. But i could not be sure even after reading through multiple resources.

In SVN, I used to create a tag called dev_tag from the branch called dev_branch. I also have a requirement to checkout the dev_tag into checkout_dir and do a maven build.

SVN - This is what i did earlier

 ~ -> svn copy -m  svn+ssh://<repo>/branches/dev_branch/ svn+ssh://<repo>/tags/dev_tag/ 
 ~ -> svn co svn+ssh://<repo>/tags/dev_tag/ checkout_dir
 ~ -> cd checkout_dir
 ~/checkour_dir -> mvn clean package

GIT - How to ?

How to do the above in Git? This is how my git looks now.

 ~ -> git branch -l
  dev_branch
* master

Re-phrasing: Agreed, w.r.t just creating a tag. My question was more in-terms of

"Suppose, if i had the git repo contain the master branch(for prod deployment) and dev branch(for QA deployment) each having different changes. Next, I create a tag by typing 'git tag mytag' with current branch as master. Now to QA , i would need to give commands which will only checkout the dev branch content on which they can do mvn build. With the same tag, I should be able to checkout the master branch content which can be built and used for prod deployment."

Finally this is what i did to create Git tags for specific branches and tar for the same which helps me build qa and prod code separately.

QA_BUILD

git clone -b dev_branch gitserve:repo.git qa
cd qa
tag -a qa_tag
git commit -a
git push --tags
git archive --format=tar --remote=gitserve:repo.git tags/qa_tag > /tmp/qabuild/qa.tar
cd  /tmp/qabuild
tar -xvf qa.tar
mvn clean package

PROD_BUILD

git clone -b master gitserve:repo.git prod
cd prod
tag -a prod_tag
git commit -a
git push --tags
git archive --format=tar --remote=gitserve:repo.git tags/prod_tag > /tmp/prodbuild/prod.tar
cd  /tmp/prodbuild
tar -xvf prod.tar
mvn clean package
like image 412
Paweł Hajdan Avatar asked Feb 20 '23 02:02

Paweł Hajdan


2 Answers

Tags in git are different than what they are in svn. They're essentially equivalent to fixed branches (i.e. once created they don't move as further commits are made) - just a pointer to a simple commit. So, to create a tag, you do one of two things - if you already have the commit you want to tag checked out, you can drop a tag where you are with:

git tag <tagname>

If you aren't at the commit you want to tag, you'll need to use git log or some other method to find the SHA hash of the commit you want, then you can run:

git tag <tagname> <sha>

(Alternatively you can git checkout <sha>; git tag <tagname>, but that's an unnecessary checkout, and can cause confusion if you forget to re-checkout to where you want to be for future development work.

like image 178
twalberg Avatar answered Feb 28 '23 03:02

twalberg


You can't.

A tag is points a <commit-id>. tags exist independently of branches.

like image 21
user2959760 Avatar answered Feb 28 '23 03:02

user2959760