Branching and Merging
To branch and merge in Git, you can use the following steps:
Branching
- To create a new branch, run the
git branchcommand 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 checkoutcommand 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 mergecommand followed by the name of the branch that you want to merge changes from. For example, to merge changes from themasterbranch into thefeaturebranch, 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 addcommand to stage the changes for commit. - To create a new commit with the merged changes, run the
git commitcommand.
- You can use the
git branch -acommand to list all of the branches in your repository. - You can use the
git checkout -bcommand to create a new branch and switch to it at the same time. - You can use the
git merge --no-ffcommand 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 --abortcommand to abort a merge that is in progress.