JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Creating a branch from another branch in Git

Author:JIYIK Last Updated:2025/03/30 Views:

This article explains how to create a branch from another branch in Git.

Git is a distributed version control system and is a suitable tool for version control in a collaborative development environment. In Git, we create repositories, and in repositories, we create branches to track various development work.

We often create a new branch from the mainline to fix bugs or develop new features. After completing the task, we usually merge this branch back to the mainline branch for release.

Git provides us with the ability to create a branch from another existing branch. Also, we can merge branches using Git commands.


git checkoutTo create a branch from another branch in Git, use the command

Git is a distributed version control system and a useful tool for tracking changes to a project repository.

We have multiple team members or teams working in a collaborative development environment using the same project repository. Different team members or teams can create different branches from an existing branch to work on that branch.

Suppose we have a mainline branch called in our project repository main. The bug fixing team will create a new branch called on top of that branch bugfixes. Another team or team member will create a branch featureto develop a new feature.

Once the team or team members are satisfied with the changes made in the new branch, the new branch is usually merged back into the mainline branch.

Suppose we want mainto create a branch from the mainline branch featureto develop a feature. We can use git checkoutthe command.

The syntax for creating a new branch of an existing branch is as follows.

git checkout -b <new-branch> <existing-branch>

In our example, we will execute the following command.

$ git checkout -b feature main
Switched to a new branch 'feature'

mainThus, we have created a new branch on the existing branch feature. git checkoutThe option of the command -bcauses the new branch to be created. Furthermore, it causes the new branch to be checked out.

Now we will develop new features and make changes in a new branch ie feature.

After we finish developing a feature and commit our changes to a new branch feature, we want to mainmerge this branch with the mainline branch.

We can use git mergethe command to merge featurethe branch with the mainline branch main.

First, we will git checkoutswitch to the mainline branch using the command main.

$ git checkout main
Switched to branch 'main'

We will use git mergethe command to merge featurethe branch with mainthe branch.

We will execute the command as follows.

$ git merge --no-ff feature
Updating ea1b23a..05e9201
(Summary of changes)

git mergeThe -m option of the command --no-ffalways forces the creation of a new commit object, even if a fast-forward merge is performed. It causes a merge commit to be created in all cases, even if the merge could be resolved as a fast-forward.

This is useful because when we browse the Git history, we see featureinformation about the existence of the branch. It also causes featureall commits for the branch to be grouped together.

Furthermore, when we want to featurerevert a merge of the -branch into mainthe -branch, this is much easier due to the presence of the merge commit.

In case of a fast-forward merge, no merge commit is created. Only an update of the branch pointers occurs to match the branch being merged.

Finally, we need to push our changes to the remote repository.

$ git push origin main
$ git push origin feature

So, we showed how to create a branch from another branch in Git.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Changing drives in Git Bash

Publish Date:2025/03/30 Views:56 Category:Git

This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to

Adding a remote branch in Git

Publish Date:2025/03/30 Views:142 Category:Git

Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi

Synchronize your local repository with a remote repository in Git

Publish Date:2025/03/30 Views:92 Category:Git

This article outlines the process of syncing your local repository with a remote repository. We will also see how to sync a GitHub branch with a remote repository on the command line. Synchronize your local repository with a remote reposito

Creating a remote repository from a local repository in Git

Publish Date:2025/03/30 Views:105 Category:Git

This article discusses the necessary steps to create a remote repository based on a local repository. This is ideal when you have a local repository that needs to be available on a remote or SSH-enabled server. Creating a remote repository

Removing the upstream repository in Git

Publish Date:2025/03/30 Views:177 Category:Git

This article will teach you how to delete an upstream repository in Git. We may sometimes need to delete or change the remote repository that we use with our local repository. To do this, we can use the Git command git remote . Removing the

Git remote add SSH

Publish Date:2025/03/30 Views:53 Category:Git

In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se

Differences between Git Reset, Revert and Checkout commands

Publish Date:2025/03/30 Views:185 Category:Git

This article discusses the differences between the git reset , , git revert and git checkout commands. These are some of the most useful Git utilities that allow us to undo certain changes in our repository. It’s easy to get confused with

Git revert local commits

Publish Date:2025/03/30 Views:84 Category:Git

When a software engineer or a web developer uses Git, it is obvious that he pushes a lot of code and commits to the Git repository every day, and while doing so, the need to undo or revert a particular commit or a set of commits arises from

Deleting local and remote Git branches

Publish Date:2025/03/30 Views:146 Category:Git

Git comes into play in version control systems when you are working with a team and the entire team is making changes to the same code repository. Creating a new branch in Git is relatively easy than other version control systems and deleti

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial