# Branching, Merging and Other Useful Commands

## Branching

### What is a branch?

A **branch** is like a snapshot of the different changes you've made on your project - essentially a pointer to a commit. New branches in Git are normally created when adding new features or trying to fix a bug. When working on group projects, each member of the group might have a separate branch to work on their code. The main branch in a project is called the **master.** The master branch is where the most up to date, error free version of the code is kept.&#x20;

To show a list of all the branches in your repository,&#x20;

```bash
git branch
```

To create a new branch,

```bash
git branch <branch-name>
```

Remember, **branches are pointers to commits.** When you create a new branch, you don't make any changes to the actual repository or its history.

You can switch between branches (in your local repository) using the **checkout** command.

```bash
git checkout <branch-name>
```

If you want to switch to a **new** branch,&#x20;

```bash
git checkout -b <new-branch-name>
```

{% hint style="info" %}
If you want to switch to a branch from your **remote repository** then you must first fetch the contents of that branch by running the command **git fetch --all**
{% endhint %}

![Simple branching diagram, from http://davidjcastner.github.io/git-tutorial/Lab3](https://756082718-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCRW-9p33ZuXPYcFb3Q%2F-MDBMGwylyikKsJ76kRJ%2F-MDBQCYIz7S8gzpYE83a%2Fbranches.png?alt=media\&token=1da37614-d625-4abc-a01a-5ffff3829e25)

## Merging

The most difficult part of using Git is **merging**. By creating separate branches we have different lines of development. **Merging** is the process of integrating these branches into a single branch so we can have finalised versions of the project.&#x20;

### Preparations for merging

* Make sure your local repository is up to date with remote changes by running **git fetch**
* Checkout to the target branch that you'll be merging into by running **git checkout \<target-branch>**
* Ensure the target branch is up to date by running **git pull**
* Checkout to the target branch with **git checkout \<target-branch>**

Once preparations are complete, You can **merge** your branch by running

```bash
git merge <source-branch>
```

### Merge Conflicts

Merging doesn't always go smoothly. **Merge conflicts** occur when two branches have changed the same part of the same file and Git doesn't know which version to use.

### Useful commands for handling merge conflicts

**Git status** is used to check the current state of the working directory and the staging area. When trying to resolve conflicts, this can show us the files that have been modified which means we need to inspect them to solve the conflict.

```bash
git status
```

Git status is used to identify **which** files were changed that are causing merge conflicts. With **git diff**, you can find out **how** these files were changed in detail.

```bash
git diff
```

{% hint style="info" %}
After manually changing your files, you must run the **git add \<file-name>** command to mark them as merged to show you've resolved the conflicts. Then proceed to **commit** your changes.
{% endhint %}

One of the easiest way to resolve merge conflicts is to inspect your code in a **code editor** like Visual Studio Code. It has brilliant version control support where you can compare code when there are merge conflicts, with the conflicting sections of code highlighted.

![Handling merge conflicts in VSCode, from https://code.visualstudio.com/docs/editor/versioncontrol](https://756082718-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCRW-9p33ZuXPYcFb3Q%2F-MDBRFlIHmn5XWFvHjRp%2F-MDBX60bRLVwqLeVqrmE%2Fmerge-conflict.png?alt=media\&token=cc7e61a2-d1ca-4f56-96a0-30e7210418e1)

## Other Useful Commands

* Display the current commit history

```bash
git log
```

* Undoing changes - create a new commit that undoes all the changes made in \<commit>, then apply it to the current branch

```bash
git revert <commit>
```

* Current branch version moves back towards \<commit> version and resets the staging area, but doesn't change the working directory

```bash
git reset <commit>
```

And that's a very quick introduction on using Git! Take a look at the further resources on the next page to learn more about version control.
