I want to commit and push changes to remote but I am detached from tag. How do I take just the name of the tag and form git commands that reattach the head where it was detached from, and commit and push to remote?
How do I [...] reattach the head where it was detached from [...]?
The output of git branch
being
* (detached from <tag>)
<possibly other branches...>
indicates that you've run git checkout <tag>
. Your situation is something like the following
The HEAD is pointing, not to a branch, but directly to a commit (the one that tag <tag>
also points to, but that's beside the point): you're in detached-HEAD state. To reattach the HEAD, you need to make it point to a branch, which you can do by running
git checkout <branch-in-question>
However, Git will, in general, prevent you from checking out a branch if you're not in a clean working state. That's where stashing comes in handy. Stashing is akin to tidying your desk by temporarily putting everything that's sitting on it in a drawer, only to retrieve that stuff at a later stage, when needed.
Here, you should run
git stash save
git checkout <branch-of-interest>
git stash pop
Resolve any conflict arising because of that last command. In my example above, your repo would then look like so,
with your local changes in the working tree. You can then stage those changes, commit, and push to remote.