Chapter 6

Branches

There are two important reasons why version histories do not always run linear, commit by commit:

  • Two or more developers worked in parallel on a project.
  • Bug fixes for older versions must be created and delivered.

In both cases, branches are created in the commit history graph.

Parallel Development

When more than one developer works on the same piece of software with Git, branches are created in the commit graph. The upper half of Figure 6.1 shows how two independent developers have created succeeding versions (commits C and D) to commit B in their local repositories. Below it you see the repository after a merge (see Chapter 9 on how to do this). It has created a branch. This type of branching can hardly be avoided in parallel development.

Figure 6.1: Parallel development

Bug Fixes in An Older Version

A branch may get created due to parallel development. In addition, a branch may also get created when fixing bugs in an older software version. Figure 6.2 shows an example: While the developers are working intensively on the version for the upcoming release (commits C and D), an error in the current software version (commit B) is detected. Since the new features in commits C and D are not ready yet for delivery, a bug fix E is created based on commit B.

Figure 6.2: Bug fixes in an older version

Branches

In the example in Figure 6.3 you can see on the one hand a release1 branch for the current release version and the master branch for current development. With each new commit, an active branch wanders. The right panel shows how the release1 branch wanders further with the bug fix.

Figure 6.3: Branches in the version graph

Swim Lanes

Branches can be thought of as parallel lines of development. You can visualize this as swim lanes in the commit graph (See Figure 6.4).

Figure 6.4: Branches as parallel lines of development

Note: Git does not know if a commit is assigned to a branch; the division into lanes is an interpretation that to some extent is arbitrary.

The Active Branch

In a Git repository there is always exactly one active branch. The branch command (without options) shows a list of all branches. The active branch is highlighted with an asterisk (*).

> git branch
  a-branch
* master
  still-a-branch 

The active branch will always continue with any new commit, pointing to the most recent commit. You can use the checkout command to change the active branch.

> git checkout a-branch

Resetting A Branch Pointer

The branch pointer to the active branch moves farther with each commit. Therefore, it is rarely necessary to set the branch pointer directly. However, sometimes you lose track of what is happening and would like to return to a previous state. In this case, you can reset the branch pointer with the reset command.

> git reset --hard 39ea21a

Here the pointer is set to the active branch of commit 39ea21a. The --hard option ensures that the workspace and staging area are also set to the state of commit 39ea21a.

Be warned that reset --hard overwrites any changes in the workspace and staging area. You should stash the changes with git stash before resetting.

Deleting A Branch

Getting Rid of the Commit Objects

The gc command (gc for garbage collect) cleans up the repository and removes commit objects that are not reachable from the current branches. If you really want to make sure your repository is clean, then clone it and delete the original.

Summary

  • Branching in the commit graph: Caused by parallel development and fixing bugs in an older version.
  • Branches: A branch is a name for a branching in the commit graph. The branch has a pointer to the latest commit in that branching.
  • Active branch: You normally work off an active branch. When you make a new commit here, the pointer is set to the new commit.
  • Creating a branch: Use the branch command to create a new branch.
  • Checkout: Use the checkout command to switch to another branch.
  • Reflog: Git records all changes to branch pointers in every commit. This is useful if you want to restore branches you delete by accident.
  • Garbage: Commits that are not predecessors of any branch should be regarded as garbage. They can be cleaned up with the gc command.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.41.205