Working with Branches
Branching and Merging
Section titled “Branching and Merging”Understanding Branches in Git
Section titled “Understanding Branches in Git”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:
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:
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:
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:
git branch
The current branch will be marked with an asterisk (*
).
Merging Branches (git merge
)
Section titled “Merging Branches (git merge)”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:
-
Switch to the Branch You Want to Merge Into:
Terminal window git checkout main -
Merge the Branch:
Terminal window git merge feature/new-feature
What It Does:
- Integrates the changes from
feature/new-feature
intomain
. - 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:
git merge feature/new-feature
Merge Commit: If there are changes on both branches, Git creates a merge commit that combines the changes.
Handling Merge Conflicts
Section titled “Handling Merge Conflicts”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:
-
Identify Conflicted Files: Git will mark conflicts in the affected files.
-
Open and Edit Conflicted Files: The conflicted sections are marked with conflict markers (
<<<<<<<
,=======
,>>>>>>>
).Example:
<<<<<<< HEADCode from the current branch.=======Code from the branch being merged.>>>>>>> feature/new-feature -
Resolve the Conflicts: Edit the files to resolve the conflicts and remove the conflict markers.
-
Add the Resolved Files:
Terminal window git add <file> -
Complete the Merge:
Terminal window git commit
Example of Merge Conflict Resolution:
git checkout maingit merge feature/new-feature# Resolve conflicts manually in filesgit add conflicted-file.txtgit commit -m "Resolved merge conflict in conflicted-file.txt"
Branch Management
Section titled “Branch Management”Deleting Branches (git branch -d
)
Section titled “Deleting Branches (git branch -d)”Deleting a Local Branch:
To delete a branch that you no longer need, use the -d
option with git branch
.
Example:
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:
git branch -D feature/old-feature
Renaming Branches (git branch -m
)
Section titled “Renaming Branches (git branch -m)”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.
Viewing Branches (git branch -a
)
Section titled “Viewing Branches (git branch -a)”Listing Local Branches:
To list all local branches, use the git branch
command.
Example:
git branch
Listing Remote Branches:
To list all remote branches, use the -r
option.
Example:
git branch -r
Listing All Branches:
To list both local and remote branches, use the -a
option.
Example:
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.