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.

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.

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.

  • 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

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:

Hit M while to merge a branch to your current branch

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.

Last updated