Skip to content

Working with Branches

In Git, a branch represents an independent line of development. Branches allow you to work on different features or fixes in isolation from each other. By default, Git starts with a single branch called main (or master in older repositories). Branches make it easier to manage multiple versions of your codebase and enable parallel development.

Key Concepts:

  • Main Branch: The default branch where the stable, production-ready code resides.
  • Feature Branches: Used for developing new features or bug fixes without affecting the main branch.
  • Merge: The process of integrating changes from one branch into another.

Creating and Switching Branches (git branch, git checkout)

Section titled “Creating and Switching Branches (git branch, git checkout)”

Creating a New Branch: To create a new branch, use the git branch command followed by the branch name.

Example:

Terminal window
git branch feature/new-feature

Switching to a Branch: To switch to a branch, use the git checkout command followed by the branch name.

Example:

Terminal window
git checkout feature/new-feature

Creating and Switching in One Step: You can create and switch to a new branch in a single command using -b option with git checkout (or git switch in newer versions).

Example:

Terminal window
git checkout -b feature/new-feature
# or using git switch (newer Git versions)
git switch -c feature/new-feature

Verifying Current Branch: To verify which branch you are currently on, use:

Terminal window
git branch

The current branch will be marked with an asterisk (*).

Merging Branches: Once you’ve made changes on a branch and want to integrate those changes into another branch (typically the main branch), you use the git merge command.

Steps:

  1. Switch to the Branch You Want to Merge Into:

    Terminal window
    git checkout main
  2. Merge the Branch:

    Terminal window
    git merge feature/new-feature

What It Does:

  • Integrates the changes from feature/new-feature into main.
  • Creates a new merge commit that combines the changes if there are no conflicts.

Fast-Forward Merge: If the branch being merged is directly ahead of the current branch, Git performs a fast-forward merge without creating a new commit.

Example:

Terminal window
git merge feature/new-feature

Merge Commit: If there are changes on both branches, Git creates a merge commit that combines the changes.

Merge Conflicts: Occur when changes in the branches being merged are incompatible. Git will notify you of conflicts and require manual resolution.

Steps to Resolve:

  1. Identify Conflicted Files: Git will mark conflicts in the affected files.

  2. Open and Edit Conflicted Files: The conflicted sections are marked with conflict markers (<<<<<<<, =======, >>>>>>>).

    Example:

    <<<<<<< HEAD
    Code from the current branch.
    =======
    Code from the branch being merged.
    >>>>>>> feature/new-feature
  3. Resolve the Conflicts: Edit the files to resolve the conflicts and remove the conflict markers.

  4. Add the Resolved Files:

    Terminal window
    git add <file>
  5. Complete the Merge:

    Terminal window
    git commit

Example of Merge Conflict Resolution:

Terminal window
git checkout main
git merge feature/new-feature
# Resolve conflicts manually in files
git add conflicted-file.txt
git commit -m "Resolved merge conflict in conflicted-file.txt"

Deleting a Local Branch: To delete a branch that you no longer need, use the -d option with git branch.

Example:

Terminal window
git branch -d feature/old-feature
  • -d: Deletes the branch but prevents deletion if the branch contains unmerged changes. Use -D (uppercase) to force deletion.

Example:

Terminal window
git branch -D feature/old-feature

Renaming a Local Branch: To rename a branch, use the -m option with git branch.

Example:

  • Renaming Current Branch:

    Terminal window
    git branch -m new-branch-name
  • Renaming a Different Branch:

    Terminal window
    git branch -m old-branch-name new-branch-name

What It Does:

  • Renames the specified branch, either the current branch or a different branch.

Listing Local Branches: To list all local branches, use the git branch command.

Example:

Terminal window
git branch

Listing Remote Branches: To list all remote branches, use the -r option.

Example:

Terminal window
git branch -r

Listing All Branches: To list both local and remote branches, use the -a option.

Example:

Terminal window
git branch -a

Output:

* main
feature/another-feature
remotes/origin/main
remotes/origin/feature/new-feature

What It Shows:

  • Local Branches: Branches that exist in your local repository.
  • Remote Branches: Branches that exist on remote repositories.
  • Current Branch: Marked with an asterisk (*).

Understanding and using these branching and merging techniques helps manage your development workflow efficiently, allowing for parallel development and collaboration. Proper branch management ensures that your repository remains organized and maintainable.