Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git commit against tag with no branch

If I check out a tagged version of my source code without creating a branch, Git indicates that I'm not associated with any branch at all. It's happy to let me make changes and check them in though. Where do those changes go? If I switch back to 'master' they disappear (overwritten by what was in master) and I can't seem to find them again. What gives? If Git lets me commit changes against what's essentially an anonymous branch, surely I can get them back?

like image 934
Andrew Avatar asked Dec 14 '08 02:12

Andrew


People also ask

What happens to tag when branch is deleted?

The tag and commit would still exist if the branch is deleted. A branch is simply a way to track a collection of commits.

Can you commit to a tag in git?

Assign an annotated tag to a commitFrom the main menu, choose Git | New Tag. In the Tag dialog that opens, under Git Root, select the path to the local repository in which you want to tag a commit, and specify the name of the new tag. In the Commit field, specify the commit that you want to tag.

Can I commit on a tag?

You can't put a new commit into an existing tag without breaking an important Git guideline: Never(*) modify commits that you have published. Tags in Git aren't meant to be mutable. Once you push a tag out there, leave it alone.

Are git tags tied to a branch?

A tag is a pointer to a commit, and commits exist independently of branches. It is important to understand that tags have no direct relationship with branches - they only ever identify a commit.


2 Answers

Because your commit isn't on any branch, you can't see it in the working directory unless you checkout that specific commit, using its SHA1. You can find the commit by looking at the reflog which tracks changes in what you have checked out from the repo. If your tag was XXX you'll see something like:

$ git reflog
7a30fd7... HEAD@{0}: checkout: moving from master to XXX
ddf751d... HEAD@{1}: checkout: moving from 96c3b0300ccf16b64efc260c21c85ba9030f2e3a to master
96c3b03... HEAD@{2}: commit:  example commit on tag XXX, not on any branch
7a30fd7... HEAD@{3}: checkout: moving from master to XXX

That tells you the SHA1 that you would have to checkout in order to see your commit in the working directory.

$ git checkout 96c3b03
Note: moving to "96c3b03" which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b <new_branch_name>
HEAD is now at 96c3b03... example commit on tag XXX, not on any branch
$ git checkout -b newbranch
$ git branch                #lists all branches
    feature1
    master
  * newbranch

This all seemed a little weird to me at first, until I realized that git checkout places all the project files as of a particular commit into my file system (working directory). In effect, the working directory acts as a browser on the local Git repository. So your changes haven't been overwritten in the repository, they're just not being shown in your working directory when you've checked out the master.

like image 89
Paul Avatar answered Oct 07 '22 13:10

Paul


Yes, they'll be in reflogs.

You can name the branch at any time like this:

git checkout -b my-branch-name
like image 8
Dustin Avatar answered Oct 07 '22 13:10

Dustin