Table of contents
Brief Overview
On Day 4, we will cover:
Understanding branches in Git.
Creating and switching branches.
Merging branches.
Resolving merge conflicts.
Deleting branches.
Detailed Explanation with Code
Understanding Branches in Git:
Branches allow you to work on different versions of your project simultaneously. The
main
(ormaster
) branch is the default branch created when you initialize a Git repository. You can create new branches to develop features, fix bugs, or experiment with new ideas without affecting the main codebase.Creating and Switching Branches:
Creating a New Branch: Use the
git branch
command to create a new branch.git branch <branch-name>
Example:
git branch feature-branch
Switching to a Different Branch: Use the
git checkout
command to switch to the new branch.git checkout <branch-name>
Example:
git checkout feature-branch
Creating and Switching in One Command: Use the
git checkout -b
command to create and switch to a new branch in one step.git checkout -c <branch-name>
Example:
git checkout -c feature-branch
Merging Branches:
Switch to the Branch You Want to Merge Into: For example, switch to the
main
branch.git checkout main
Merge the Branch: Use the
git merge
command to merge the changes from another branch into the current branch.git merge <branch-name> //merge the changes from another branch into the current branch i.e main
Example:
git merge feature-branch
Resolving Merge Conflicts: Sometimes, merging branches can lead to conflicts when the same lines of code have been changed in different ways. Git will mark these conflicts in the affected files.
Identify the Conflicted Files: Use
git status
to see which files have conflicts.git status
Example:
git status
Output:
On branch main You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: hello.txt
Resolve Conflicts: Open the conflicted files in a text editor and resolve the conflicts. Git marks conflicts with
<<<<<<<
,=======
, and>>>>>>>
.Example:
<<<<<<< HEAD This is a new line from main branch. ======= This is a new line from feature-branch. >>>>>>> feature-branch
Choose the correct version or merge the changes manually, then remove the conflict markers.
Stage and Commit the Resolved Changes: After resolving conflicts, stage and commit the changes.
git add <file> git commit -m "Resolve merge conflicts"
Example:
git add hello.txt git commit -m "Resolve merge conflicts in hello.txt"
Deleting Branches: Once a branch is no longer needed, you can delete it.
Delete a Branch Locally: Use the
-d
flag to delete a branch.git branch -d <branch-name>
Example:
git branch -d feature-branch
Force Delete a Branch: If the branch has unmerged changes, use the
-D
flag to force delete it.git branch -D <branch-name>
Example:
git branch -D feature-branch
Example Workflow
Initialize a New Git Repository:
mkdir my-git-project cd my-git-project git init
Create and Commit a File:
echo "Hello, Git!" > hello.txt git add hello.txt git commit -m "Add hello.txt with initial content"
Create a New Branch:
git checkout -c feature-branch
Make Changes in the New Branch:
echo "This is a new line in the feature branch." >> hello.txt git add hello.txt git commit -m "Add a new line in feature-branch"
Switch Back to the
main
Branch:git checkout main
Merge the Feature Branch into
main
:git merge feature-branch
Resolve Merge Conflicts (if any):
git status # Open conflicted files, resolve conflicts, then git add hello.txt git commit -m "Resolve merge conflicts in hello.txt"
Delete the Feature Branch:
git branch -d feature-branch
By the end of Day 4, readers should understand how to create, switch, merge, and delete branches in Git. This knowledge is essential for managing parallel development and collaborating effectively on different features or fixes within a project.