๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Working with Branches -


Introduction

  • git branch command is a fundamental part of Git that allows you to create, list and manage branches within your repository.
  • By default, you start with a single branch, usually called main or master, but you can create multiple branches to handle various tasks in parallel.
  • Below diagram shows the concept of branching in Git: Git Branching
  • Why to Use git branch:-
    • Isolated Line of Development: A branch in Git represents an isolated line of development, enabling you to work on different features or bug fixes without affecting the main project.
    • Lightweight and Efficient: Branching in Git is lightweight and efficient, making it ideal for experimenting with new ideas, implementing features, or resolving issues.
    • Keep the main branch Stable: It helps in keeping the main branch stable while allowing parallel development in separate branches.
    • Collaboration: In collaborative environments, it allows team members to work on different tasks independently and merge their work back into the main codebase when ready.

How to use git branch ?

  1. Open the Git terminal (or command prompt) and navigate to the project folder where your Git repository is initialized.
  2. Default Branch Creation:
    • When you run git init, Git sets up an empty repository and default branch (usually main or master) is created.
    • When you provide first git commit command, it saves your initial work onto that default branch.
    • (Optional) If needed, you can rename the branch to any other name using below command:
      • git branch -M <branch-name>
  3. Creating a New Branch:
    • After setting up the main branch, you can create a new branch to work on a feature or bug fix.
    • Now lets create a new branch named feature-1 which will keep your changes separate from main branch.
    • Example : git branch feature-1
    • Now check wether the branch is created or not using below command:
      • git branch
    • This will list all the branches in your repository, and the current branch will be highlighted with an asterisk (*).
    • Below is the example of how it looks like: Git Branch Commands
  4. Now you can:
    • Switch to that new branch using git checkout feature-1 command.
    • Make changes like add new feature, fix bugs, etc.
    • Commit changes using git commit command.
    • And at last merge changes back to default branch (main or master)

Different ways to use git branch ?

  1. Create a New Branch:
    • Syntax :
      • git branch <branch-name>
    • Example :
      • git branch feature-1
    • Use case: Create a new branch from the current branch without switching to it.
  2. List All Local Branches:
    • Syntax & Example :
      • git branch
    • Description: Displays a list of all local branches in the repository.
    • Use case: To see which branches exist and which one you're currently on (highlighted with *).
  3. Create a New Branch from a Specific Commit:
    • Syntax :
      • git branch <branch-name> <commit-id>
    • Example:
      • git branch debug-fix a1b2c3d
    • Use case: Useful when you want to branch out from a previous point in history.
  4. Rename a Branch:
    • Syntax :
      • git branch -m <new-name>
    • Example :
      • git branch -m dev
    • Use case: Renames the current branch to a new name.
  5. Delete a Local Branch:
    • Syntax :
      • git branch -d <branch-name>
      • (use -D to force delete)
    • Example:
      • git branch -d old-feature
    • Use case: Clean up branches that have already been merged or are no longer needed.
  6. List Remote Branches:
    • Syntax :
      • git branch -r
    • Description: Shows branches from remote repositories (e.g. origin/main, origin/dev).
  7. List All Branches (Local + Remote):
    • Syntax :
      • git branch -a
    • Description: Lists all local and remote branches.
  8. Check Last Commit on Each Branch:
    • Syntax :
      • git branch -v
    • Description: Shows the latest commit on each branch.
  9. See Which Branches Have Been Merged:
    • Syntax :
      • git branch --merged
    • Description: Lists branches that have already been merged into the current branch.
  10. See Which Branches Have Not Been Merged:
    • Syntax :
      • git branch --no-merged
    • Description: Lists branches that haven't been merged yet.