0

I run into a detached head state often. I would like to create a script that, when I run, automatically creates a branch where I am at and names it after the current commit id. It appears that git will let me do it, but could a branch and commit having the same name lead to any possible ambiguity or side effects with any git commands?

Chance
  • 2,653
  • 2
  • 26
  • 33
  • 1
    What is your problem with manually creating a branch? – hek2mgl May 27 '15 at 14:20
  • @hek2mgl, I don't like having to keep thinking of branch names "whatever1", etc... As far as not doing it manually, it's for the same reason I like typing `ll` instead of `ls -l` or `uu` instead of `cd ../..`. I just want to type "branch" and it create the branch. – Chance May 27 '15 at 14:27
  • 1
    But surely if you want a branch, it's do make new commits? It would be rather relevant to take the time to find a good branch name that reflects these intended changes, I think? I know it's hard to find good names, but `fix_typo` would be better than `whatever1`. – Gauthier May 27 '15 at 15:31
  • @Gauthier, many times yes. There are sometimes when I'm just using a branch as a stash and/or the branches are short-lived. It may not be good practice, I don't know. – Chance May 27 '15 at 18:24
  • @Gauthier, another motivation is seen here. http://stackoverflow.com/questions/2763006/change-the-current-branch-to-master-in-git. Many times (probably all the time) I plan on doing meaningful development in git, but there is no easy way to check in something and make it the new master - I have to basically create a meaningless branch as an intermediate step when doing what I want to do. In my mind any commit should be tracked even if I forgot to check out a branch - this is a way for me to do it. I probably just need to get in the git workflow. – Chance May 27 '15 at 19:03
  • 1
    generally, reflog/doubleclick/middleclick (or ^C^V) is a worst case scenario for going back to someplace you forgot. If you can remember a memorable word or phrase from the commit message, you can use `:/regex_here` to refer to it, that gets you the most recent commit with a match in its message. So for short-term branches, just don't bother naming them. – jthill May 27 '15 at 20:53
  • @hek2mgl, also, as I've found out recently, sometimes I have to try 2 or 3 different ways of doing something, or a combination of the two. So, the branches will be short lived. I have my commit comment to tell me what it's doing. Coming up with branch names, to me, is just an unnecessary extra step. – Chance Jul 17 '15 at 15:10

2 Answers2

5

It appears that git will let me do it, but could a branch and commit having the same name lead to any possible ambiguity or side effects with any git commands?

If nothing else, it's going to be pretty confusing for you. You could always construct a name for the branch that includes the commit hash but is still easy to distinguish from an actual hash, like Branch_6ac738f999. Using the actual hash by itself seems like a poor plan.

Caleb
  • 124,013
  • 19
  • 183
  • 272
2

As said by others, don't give an understandable name to a branch is a bad idea and with doing that, you are taking a bad way...

That said, perhaps you could think about creating an alias to create a branch on the current commit, for example:

git b my_new_branch

And to create it:

git config --global alias.b 'checkout -b'

More information here

Philippe
  • 28,207
  • 6
  • 54
  • 78