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.

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.

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
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 -vDeleting 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>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:
Switch to the target branch
Merge source branch into target branch
This can be done via:
Hit M while to merge a branch to your current branch
git checkout main
git merge feature-AHowever, 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