NUS Hackers Wiki
NUS Hackers Wiki
  • NUS Hackers Wiki
  • Hackerschool
    • Virtual Machines and Linux
    • Beginners' Guide to the Terminal
      • Introduction to the Terminal
      • Modern Shell Tools
      • Shell Scripting
      • Real World Scripting
      • Resources
    • Self-Hosting: Three Easy Pieces
      • 1. Setting up your server
      • 2. Running Services
      • 3. Monitoring your server
    • Vim
    • Introduction to Zig
      • Language Basics
      • Error Handling
      • Memory Management
      • Working with C
      • Exploring comptime
    • CI/CD with Github Actions
      • Background
      • Basics of Github Actions
        • Target workflow
        • Running unit tests
        • Linting code
        • Deploying to Github Pages
      • Advanced use cases
        • Pollers
        • Github script
        • Executing third-party scripts
        • Reusable workflows
      • Cookbook
    • Lightning Git
      • Git Concepts
      • Getting Started with Git
      • Making your first commit
      • Branching
      • Merge Conflicts
      • Integrating remote repositories
      • Collaborative Workflows
      • Commit Manipulation and Reflog
      • Interactive rebasing
      • filter-repo
  • Orbital
    • JavaScript
      • Browser Developer Tools
      • Getting Started
      • Datatypes
      • Operators and Operations
      • Loops and Conditions
      • Functions
      • Strings
      • Arrays
      • HTML
        • Getting Started
        • Tag Attributes
        • HTML Forms
        • Browser Inspector
      • CSS
        • Selectors
        • Colors in CSS
        • Measurements in CSS
        • The Box Model
        • Adding Styles - Part 1
        • Adding Styles - Part 2
      • Working with the DOM
        • Querying the DOM - Selectors
        • Querying the DOM - Element Attributes
        • Querying the DOM - Element Styles
        • Events with JS and HTML
        • Exercise: Click Counter
        • Editing the DOM
        • Fetch Requests
        • Exercise: The NUSMods API
    • React
      • Setup
      • State
    • React Native
      • Setup
      • Intro to JSX
      • Basic Syntax
      • Handling UI
      • Props
      • State Management
    • Git
      • Setup
      • Command Glossary
      • Fundamental Concepts
        • Getting Started
        • Integrating Remote Repositories
        • Branching
        • Merge Conflicts
      • Collaborative Workflows
        • Fork and PR Workflow
        • Branch and PR Workflow
      • Advanced Concepts
        • Ignoring Files
        • Commit Message Conventions
        • Github Collaborators
        • CI/CD with Github Actions
        • Advanced Git Commands
      • FAQ
    • Telegram Bot
      • Creating a TeleBot
      • API Calls
      • Telebot Basics
      • Integrating API's
    • Relational Database
      • Database Overview
      • Database Design
      • Entity Relationship Diagram
      • SQL Basics & PostgreSQL
    • TypeScript
      • Types and Interfaces
      • Utility Types
      • Typing Component Props, Events, and Hooks
      • Why You Should Avoid Using any (and What to Do Instead)
      • TypeScript Tricks You’ll Use All the Time in React
Powered by GitBook
On this page
  • HEAD
  • Creating a new branch
  • Combining changes of branches
Edit on GitHub
Export as PDF
  1. Hackerschool
  2. Lightning Git

Branching

PreviousMaking your first commitNextCommit Manipulation and Reflog

Last updated 26 days ago

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 -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>

This renames the current branch that you are on.

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

git checkout main
git merge feature-A

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.