Show List

Branching and merging in Git

Branching and merging are two of the key features in Git that allow you to work on multiple versions of your codebase simultaneously.

Here are some examples of how to use branching and merging in Git:

  1. Creating a new branch:
$ git branch <branch-name>

In this example, <branch-name> is the name of the new branch you want to create. The git branch command will create a new branch with the specified name.

  1. Switching to a different branch:
$ git checkout <branch-name>

In this example, <branch-name> is the name of the branch you want to switch to. The git checkout command will switch you to the specified branch, allowing you to work on that version of the codebase.

  1. Merging two branches:
$ git checkout <destination-branch>
$ git merge <source-branch>

In this example, <destination-branch> is the name of the branch you want to merge into, and <source-branch> is the name of the branch you want to merge from. The git checkout command will switch you to the destination branch, and the git merge command will merge the source branch into the destination branch.

  1. Resolving conflicts during a merge:

Sometimes, when you merge two branches, Git will encounter conflicts between the two versions of the code. In this case, you will need to resolve the conflicts manually before the merge can be completed.

When a conflict occurs, Git will mark the conflicting lines in the file, and you will need to edit the file to resolve the conflict. For example:

<<<<<<< HEAD
This is the line of code in the destination branch.
=======
This is the line of code in the source branch.
>>>>>>> <source-branch>

You can edit the file to choose the version of the code that you want to keep, then add the file back to the staging area and commit the merge.

These are some of the basics of branching and merging in Git. By using branches, you can work on multiple versions of your codebase simultaneously, then merge the changes back into the main branch when they are ready. This can be useful for developing new features, fixing bugs, or experimenting with different approaches.


    Leave a Comment


  • captcha text