Branching and Merging

To branch and merge in Git, you can use the following steps: Branching
  1. 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
  1. 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
  1. 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.
Merging
  1. 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 the master branch into the feature branch, you would run the following command:.
git merge master
  1. 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.
  2. To create a new commit with the merged changes, run the git commit command.
Here are some additional things to keep in mind when branching and merging in Git:
  • 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.
ADVERTISEMENT
ADVERTISEMENT