Chapter 5. Work With Branches

Branch Operation Basic Life Cycle

Branch Operation Basic Life Cycle
Tag:

On this page, we'll explain a basic life cycle for the branch operation in seven steps.

Step 1: Create a new branch

As a default setting, you are working on the master branch. Edit and commit operations explained in the previous chapter were conducted on the master branch.

When you want to separate your work from the master branch, you need to create a new branch by running the git branch [new branch name] command.

Step 2: Check the branch status

To check your branch status (i.e., the list of branches and the current branch) in the command line, you need to run the git branch command. When you specify a branch name, the command creates a new branch. For checking status purposes, you simply run the command without any parameter.

Step 3: Switch branches

When you create a new branch by running the git branch [new branch name] command, you are still on the original branch; if you are creating a new branch for the first time, you are still on the master branch. To move to the new branch, you have to run one of the following commands

  • git checkout [branch name]
  • git switch [branch name]

Previously, the git checkout command was used only as an option to do this, however, git switch command was introduced as a substitute for git checkout command. Currently, you can use both commands to get the same result.

Step 4: Edit and Commit on branches

We already explained edit and commit operations in the previous chapter. The edit and commit operations can be applied regardless of the branch you are working on. What you may need to be careful of is on which branch you are operating. When you make a commit, the commit is applied to a specific branch on which you are operating.

Step 5: Integrate branches

There are two ways to integrate branches: Merge and Rebase.

Merge is used to join two or more development histories together. You can merge branches by running git merge in your command line. Rebase is used to reapply commits on top of another base branch. The command to implement rebase is git rebase.

Both merge and rebase operations can be done on the GitHub platform. On GitHub, rebase is one option of the merge action. The merge operation on the GitHub platform will be explained in the next chapter.

Step 6: Manage conflicts

If there are changes in the same line of code on different branches, the Git system cannot merge those branches automatically. This situation is called conflict. You need to resolve conflicts before merging the branches. You can manually fix the lines of code to resolve conflicts.

Step 7: Delete an unused branch

A merged branch is not automatically deleted unless you delete it. After several branching and merging operations, you may see many unused branches in your repository. To delete a branch, you can use the git branch -d [branch name] command.

In the following pages, you’ll learn how to manage branches in more detail.

On this page, we'll explain a basic life cycle for the branch operation in seven steps.

Step 1: Create a new branch

As a default setting, you are working on the master branch. Edit and commit operations explained in the previous chapter were conducted on the master branch.

When you want to separate your work from the master branch, you need to create a new branch by running the git branch [new branch name] command.

Step 2: Check the branch status

To check your branch status (i.e., the list of branches and the current branch) in the command line, you need to run the git branch command. When you specify a branch name, the command creates a new branch. For checking status purposes, you simply run the command without any parameter.

Step 3: Switch branches

When you create a new branch by running the git branch [new branch name] command, you are still on the original branch; if you are creating a new branch for the first time, you are still on the master branch. To move to the new branch, you have to run one of the following commands

  • git checkout [branch name]
  • git switch [branch name]

Previously, the git checkout command was used only as an option to do this, however, git switch command was introduced as a substitute for git checkout command. Currently, you can use both commands to get the same result.

Step 4: Edit and Commit on branches

We already explained edit and commit operations in the previous chapter. The edit and commit operations can be applied regardless of the branch you are working on. What you may need to be careful of is on which branch you are operating. When you make a commit, the commit is applied to a specific branch on which you are operating.

Step 5: Integrate branches

There are two ways to integrate branches: Merge and Rebase.

Merge is used to join two or more development histories together. You can merge branches by running git merge in your command line. Rebase is used to reapply commits on top of another base branch. The command to implement rebase is git rebase.

Both merge and rebase operations can be done on the GitHub platform. On GitHub, rebase is one option of the merge action. The merge operation on the GitHub platform will be explained in the next chapter.

Step 6: Manage conflicts

If there are changes in the same line of code on different branches, the Git system cannot merge those branches automatically. This situation is called conflict. You need to resolve conflicts before merging the branches. You can manually fix the lines of code to resolve conflicts.

Step 7: Delete an unused branch

A merged branch is not automatically deleted unless you delete it. After several branching and merging operations, you may see many unused branches in your repository. To delete a branch, you can use the git branch -d [branch name] command.

In the following pages, you’ll learn how to manage branches in more detail.

Tag: