Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Git Branching Tutorial

Introduction to Branching

Branching is a fundamental aspect of version control systems like Git. It allows you to diverge from the main line of development and continue to do work without affecting that main line. This tutorial will guide you through the basics of branching in Git, including creating, managing, and merging branches.

Creating a New Branch

To create a new branch in Git, you use the git branch command followed by the name of the new branch.

Example:

git branch feature-xyz

This command creates a new branch called feature-xyz based on the current branch.

Switching Between Branches

After creating a branch, you can switch to it using the git checkout command:

Example:

git checkout feature-xyz

This command changes the current working branch to feature-xyz.

Creating and Switching in One Step

Git also provides a shortcut to create and switch to a new branch in one step using the -b flag:

Example:

git checkout -b feature-xyz

This command creates the feature-xyz branch and switches to it immediately.

Listing Branches

To see a list of all branches in your repository, use the git branch command with no arguments:

Example:

git branch
  master
* feature-xyz
                

The branch with an asterisk (*) next to it is the current active branch.

Merging Branches

Once your work on a branch is complete, you might want to merge it back into another branch (usually the master branch). To merge the changes from one branch into the current branch, use the git merge command:

Example:

git checkout master
git merge feature-xyz

This will merge the changes from feature-xyz into the master branch.

Deleting a Branch

After merging a branch, you might want to delete it. To delete a branch, use the -d flag:

Example:

git branch -d feature-xyz

This command deletes the feature-xyz branch.

Conclusion

Branching is a powerful feature of Git that allows you to manage different lines of development. By mastering branch creation, switching, merging, and deletion, you can easily handle complex workflows and collaborate effectively with other developers.