# Branching

On top of commits, we like to label different "lines of work" as branches. By default, your branch will be something like "master" or "main". We can create new branches to group together a bunch of changes and commit.

<figure><img src="/files/qQV7R65RLQRpgqIg8pTN" alt=""><figcaption></figcaption></figure>

## HEAD

Notice that all commits come with a unique ID. This can make it difficult to reference the exact commit you are on. This is where `HEAD`  label comes in.

`HEAD` is a special label given to the current commit you are looking at.

<figure><img src="/files/aHFqH6tMjOJhdvNHK9sc" alt=""><figcaption></figcaption></figure>

To move you HEAD around in lazygit, go to the Commits submenu (hit 4), then navigate with arrow keys/mouse to the commit you want and hit spacebar to switch to that commit.

Without lazygit, you would do something like:

```
git checkout <commit-hash/branch name>
```

## Creating a new branch

By default, a new branch is always created from the point of `HEAD` of your current branch (usually `main`) onwards. This means that the branch will have all the snapshots that precede (and include) `HEAD` but any new snapshots made on the branch are not reflected (yet) on `main.`

{% tabs %}
{% tab title="On Lazygit" %}

* Hit 3 to go to the branches submenu
* Hit n to create a new branch
* Hit space to switch branches
* Hit d to delete a branch
* Hit R to rename a branch
  {% endtab %}

{% tab title="On Git CLI" %}
There are two ways to create a new branch:

```
git branch <branch name>
```

Then, you can switch to the branch by using:

```
git checkout <branch name>
```

Alternatively, you can use the `git checkout` command for both:

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

## Changing branches

As mentioned earlier, you can switch to a branch via:

```
git checkout <branch name>
```

## Viewing all branches created

To view all branches, you can use the following:

```
git branch -v
```

## Deleting a branch

To delete a branch, you add the `-d` flag:

```
git branch -d <branch name>
```

## Renaming a branch

You may have misspelled the branch name or parts of it. You can rectify it using the `-m` flag:

```
git branch -m <new branch name>
```

{% hint style="info" %}
This renames the current branch that you are on.
{% endhint %}
{% endtab %}
{% endtabs %}

## Combining changes of branches

Recall that we mentioned that the changes of a branch are not reflected across any other branch UNTIL otherwise specified? How exactly do we specify this?

An easy way to do so is by merging the branches into one another.

Suppose we have two branches: `main` and `feature-A` and we want all the changes from `feature-A` to be present in `main` so that we can demo it to the executives. We first need to clearly denote which is the source branch (where the changes exist) and the target branch (where we want the changes to appear in). In this scenario, `feature-A` is the source branch and `main` is the target branch.

Then, a simple procedure to perform the merge would be:

1. Switch to the target branch
2. Merge source branch into target branch

This can be done via:

{% tabs %}
{% tab title="On lazygit" %}
Hit M while to merge a branch to your current branch
{% endtab %}

{% tab title="On Git CLI" %}

```
git checkout main
git merge feature-A
```

{% endtab %}
{% endtabs %}

However, this process is not always so straightforward. As you will see in the coming chapter, merging has its own set of "problems" that may arise.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.nushackers.org/hackerschool/lightning-git/branching.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
