-2

Hi I have two branches in parallel 'dev' and 'prod' I would like to add another local branch to the local dev branch called 'issue22' (GitPro refers to this as a topic branch - others call it a feature branch). So at the terminal I created my feature/topic branch with

git checkout -b issue22 dev

Which I thought would do 'make a branch called issue22' and start with the same source code that is in the dev branch. I am expecting to see

prod
dev
dev/issue22

However when I send the command git branch -a

Instead, I see

prod
dev
issue22
remotes/origin/head -> origin/master
remotes/origin/dev
remotes/origin/prod

What command should I use to create a new branch so that 'issue22' appears to look like a topic/feature branch that runs in parallel to the 'dev' branch and also appears to stem from the dev branch?

many thanks

user1709076
  • 2,538
  • 9
  • 38
  • 59
  • Your terminology is very confusing, can you please edit the question to show the exact commands and outputs, and the fully qualified branch names and repo names; since you seem to be using the word branch and repository interchangeably when they are not. – UpAndAdam May 01 '14 at 17:51
  • Case in point You can't add a branch to a branch. You can however add a branch to a repository. – UpAndAdam May 01 '14 at 17:53
  • Please research this site - http://nvie.com/posts/a-successful-git-branching-model/ that is the model of "When starting work on a new feature, branch off from the develop branch." The whole point of git as far as i understand is to branch off of branches. yes, git only sees 'commits' - but I would like to be able to visualize branches running in parallel. I appreciate your input UpAndAdam, but I did not use the word 'repository' in my question. I am posting more commands & output... – user1709076 May 01 '14 at 18:15
  • If you use a branch visualization tool like `gitk` or `git log --all --graph --decorate` you should see that `issue22` and `dev` are in fact at the same place. This is the closest that Git gets to "branching off another branch". You can then work in `issue22` and commit to that branch. Then when you're done you can merge into `dev`. – ChrisGPT was on strike May 01 '14 at 18:47
  • Thanks @Chris - so does that mean that http://nvie.com/files/Git-branching-model.pdf is not a possible visualization when using git? I'm asking because I'm trying to make our git branch look like this (because it reads like a taxonomy hierarchy). Instead, when we make 'feature' and 'hot fix' branches, there is no hierarchy. It would be like you have one directory C:\ and you have to put all your folders (commits) in there. I'd like to have C:\Dev\fix1 C:\Prod\hotfix, instead what we have just looks like C:\prod C:\dev C:\fix1 etc. – user1709076 May 01 '14 at 20:42
  • @user1709076, you can definitely reproduce that diagram precisely with Git. Please see my answer; I hope it clears things up for you. – ChrisGPT was on strike May 01 '14 at 21:36
  • Also your typo'd command of `git branch -d issue22 dev` would actually DELETE the branches you named. It won't create them. – UpAndAdam May 01 '14 at 22:55

1 Answers1

6

The parental relationship between commits is what gives a repository its "structure". And a visualization like the one displayed on the Git Flow website is definitely possible with Git. It was designed around Git!

Here is an example that should get you started:

  1. First, create a new repository and add a single empty commit:

    $ mkdir test-repo && cd test-repo
    $ git init
    $ git commit --allow-empty -m "Initial empty commit"
    

    At this point you've got one branch and one commit. Your network will look something like

    [master]  A
    

    Here, A represents the blue dot at the extreme top-right corner of the Git-Flow site.

  2. This commit is missing its tag from the diagram. Let's add it:

    $ git tag -a 0.1
    

    Now we've got

    [master] [0.1]  A
    
  3. Now we'll create our develop branch:

    $ git checkout -b develop
    

    At this point, we haven't added any commits to develop that aren't in master, so the branches are pointing to the exact same commit:

    [master] [develop] [0.1]  A
    
  4. It's getting a bit crowded here. Let's add a few more commits to develop:

    $ # Hack, hack, hack...
    $ git add somefile.txt otherfile.dat
    $ git commit
    $ # Hack, hack, hack...
    $ git add foo.bar
    $ git commit
    

    Now our network is a little bit more interesting. When we created each of these two new commits, our develop branch was updated to point to each of them in turn. Now we've got

    [master] [0.1]  A
                     \
    [develop]         B---C
    

    develop actually points to commit C at this point, and we can trace C's ancestry back to commit A.

  5. We can do the same thing with a new branch issue22, though it won't be named develop/issue22 unless we explicitly call it that:

    $ git checkout -b issue22
    $ # Hack, hack, hack...
    $ git add foo.bar
    $ git commit
    

    and now we've got

    [master] [0.1]  A
                     \
    [develop]         B---C
                           \
    [issue22]               D
    

    issue22 branches off of develop by virtue of D's parent being C. We can do some more work on both develop and issue22

    [master] [0.1]  A
                     \
    [develop]         B---C---F---G
                           \
    [issue22]               D---E---H
    

    without changing that relationship. Then we can merge issue22 back into develop (or directly into master, though that's not the "Git-FLow way") and get something like

    [master] [0.1]  A
                     \
    [develop]         B---C---F---G---I
                           \         /
    [issue22]               D---E---H
    

This model extends to as many branches as you'd like, and with a little care you could exactly reproduce the Git-Flow diagram. Branches don't "diverge" until they contain different commits.

It might be helpful for you to understand a little bit about Git's internal workings. The website Think like (a) Git is a very good introduction.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 2
    Thanks. A masterful and thoroughly explained answer. Very cool-headed and knowledgeable. You clearly understand this and I am grateful for your ability to communicate it to me. Many thanks. – user1709076 May 02 '14 at 20:30