Hans
Hans

Reputation: 269

I think I don't understand git branches

Salutations everyone,

I have been working on a bash script as a small summer project to learn more about UNIX scripting and on using git.

This has been the first time that I have used branches in git, normally I just stick to master.

I was viewing the git log with the graph (git log --graph) when I noticed that my 'develop' branch seemed to have merged with 'master'. Something like this:

master   ----1--------3----4----5----6----HEAD
develop      \---2---/

but commits 3 onwards were done within the develop branch. Doing git checkout master and git checkout develop showed this to be true.

What exactly is going on? Is this what is known as fast-forwarding?

UPDATE #1:

Commit 1, isn't actually the first commit in my repo, its the last commit I made on 'master'

UPDATE #2:

This is the graph as given in gitk --all

                   Tag '1.0-0'
                        |
master    --*--*--*--*--*--*----2\
develop                     \1----3----4----5----6

UPDATE #3:

I have had a closer look at gitk and have discovered that all commits up until commit 1 happened both in the 'develop' branch as well as the 'master' branch.

Thanks to SimoneDemoGentili for pointing out my incorrect use of git checkout develop

P.S.: Commits 1 and 2 are also a mystery to me being that commit 2 is actually an amendment of commit 1 (as far I thought, I used this advice)

Upvotes: 3

Views: 1512

Answers (1)

Rudi
Rudi

Reputation: 19940

Branches

A git branch is only a movable pointer to a specific commit. The commits them selfs do not have any branch property attached to them. Git displays a commit to belong to a branch, if the commit is an ancestor of a commit where the branch pointer is attached to.

Say you have the following history:

     4 <- A
     |\
     | \
     2  3 <- B
     | /
     |/
     1
     |
     0

Then all commits are on branch A, since all commits are ancestors of commit 4, where branch A points to. But only 0, 1 and 3 are on branch B, since these commits are the history of commit 3.

Fast forward

Fast forward merges are merges, where the branch pointer of one branch can be simply moved to the new commit without loosing history. This is the case, when the target commit is a successor of the commit where the branch currently points to. In the previous example branch A can be FF-Merged onto branch B, since commit 4 is a successor of commit 3. After this merge B would also like A point to commit 4. The following history shows an example of a non-FF merge:

A -> 2  3 <- B
     | /
     |/
     1
     |
     0

Here B can't be moved to commit 2, since then commit 3 would fall out of the history. To solve this, a real merge between 2 and 3 must be created, leading to the following history:

     4 <- B
     |\
     | \
A -> 2  3
     | /
     |/
     1
     |
     0

Upvotes: 2

Related Questions