Branching and Merging
To branch and merge in Git, you can use the following steps:
Branching
- To create a new branch, run the
git branch
command followed by the name of the new branch. For example, to create a new branch called feature, you would run the following command:
git branch feature
- To switch to a new branch, run the
git checkout
command followed by the name of the branch. For example, to switch to the feature branch, you would run the following command:
git checkout feature
- Once you have switched to a new branch, you can make changes to the files in your working directory. These changes will be tracked by the new branch.
- To merge changes from one branch into another, run the
git merge
command followed by the name of the branch that you want to merge changes from. For example, to merge changes from themaster
branch into thefeature
branch, you would run the following command:.
git merge master
- If there are any conflicts between the changes in the two branches, Git will prompt you to resolve the conflicts. Once you have resolved the conflicts, you can run the
git add
command to stage the changes for commit. - To create a new commit with the merged changes, run the
git commit
command.
- You can use the
git branch -a
command to list all of the branches in your repository. - You can use the
git checkout -b
command to create a new branch and switch to it at the same time. - You can use the
git merge --no-ff
command to prevent Git from performing a fast-forward merge. A fast-forward merge is a merge where the new branch is simply a continuation of the old branch. - You can use the
git merge --abort
command to abort a merge that is in progress.