About branches

Use a branch to isolate development work without affecting other branches in the repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.

branch_image
  • Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.
  • When you create a repository with content on GitHub, GitHub creates the repository with a single branch. This first branch in the repository is the default branch.

How to create a new branch

  1. Open Terminal or Command Prompt.
  2. Navigate to Your Project

    If you're not already in your project folder, use the cd command to navigate there:
    # Change to your repo directory cd /path/to/your/repository.
  3. Create a New Branch:

    Run the following command to create a new branch (replace branch-name with your preferred branch name):
    # Create a new branch git checkout -b branch-name.
  4. Push the Branch to GitHub:

    To push the new branch to the GitHub remote repository
    # Push the new branch to your Github Repository git push -u origin branch-name.

    This command pushes the new branch to GitHub and sets it up for tracking.

How to create a pull request and merge pull request

A pull request (PR) in GitHub is a way to propose and request a review of changes you've made in a branch before merging them into another branch (often main or master). It allows developers to collaborate by discussing, reviewing, and approving the changes before they are integrated into the main codebase.

pull_merge

And this is what it looks like once you've submitted the PR request:

pull_request2 image

You might see a big green button at the bottom that says 'Merge pull request'. Clicking this means you'll merge your changes into the primary branch. Sometimes you'll be a co-owner or the sole owner of a repo, in which case you may not need to create a PR to merge your changes. However, it's still a good idea to make one so you can keep a more complete history of your updates and to make sure you always create a new branch when making changes.

More commands to work with branches

Here are some useful commands to work with branches

  1. List all the branches git is tracking in your local repo

    # List your branches git branch
  2. Switch from one branch to another

    # Checkout a specific branch git checkout branch-name.
  3. Merge the current local branch with another branch like main

    # Merge checked out branch to another branch git merge other_branch.
  4. Deleting the local branch when you dont need to be tracked anymore

    # Delete the local old branch git branch -d branch_name.