2. Install Git On Windows
Configuration of Git
Creating a Git Repository and
Connecting it to a Remote Repository
3. By the end of the session, we will be able to:
▪ Comprehend the concept and importance of branching
in Git.
▪ Acquire proficiency in creating, switching between,
listing.
▪ Master the process of merging branches.
▪ Develop essential skills in resolving merge conflicts.
Session Objectives
5. Branching in Git
● In Git, branching is a powerful feature that enables
developers to create separate lines of
development within a repository.
● It provides a flexible and efficient way to work on
multiple features, bug fixes, or experiments
simultaneously without interference.
● By understanding and harnessing the concept of
branching, developers can enhance collaboration,
streamline workflows, and maintain a well-
structured codebase.
6. Branching in Git
● The concept of branching revolves around the idea
of creating a divergent path from the main line of
development.
● It allows developers to isolate their work, make
changes, and iterate on specific tasks without
impacting the stability of the main codebase.
● Each branch in Git represents an independent line
of development, enabling developers to work in
parallel on different features or address separate
issues.
7. Branching in Git
Creating a Branch:
● Creating a new branch in Git is a simple and
straightforward process. By using the git branch
command followed by the desired branch name,
developers can create a new branch based on their
current position.
● To create a new branch in Git, use the following
command:
git branch <branch_name>
● Replace <branch_name> with the desired name for
your new branch.
8. Branching in Git
Creating a Branch:
● This new branch starts with the same commit
history as the branch it was created from, ensuring
that all existing work is inherited. This way,
developers can build upon the existing codebase
without disrupting the progress made on other
branches. For example:
git branch feature-branch
● This command creates a new branch named
"feature-branch" based on your current branch. It
does not switch to the new branch; it simply
creates it.
9. Branching in Git
Switching to a Branch:
● Switching between branches is another essential
aspect of branching in Git. By using the git
checkout command followed by the branch name,
developers can seamlessly transition to a different
branch and continue their work in that context.
● Switching between branches updates the working
directory to reflect the state of the selected
branch, allowing developers to access the specific
set of code and files associated with that branch.
10. Branching in Git
Switching to a Branch:
● To switch to a different branch, use the following
command:
git checkout <branch_name>
● Replace <branch_name> with the name of the branch
you want to switch to.For example-
git checkout feature-branch
● This command switches you to the "feature-branch"
and updates your working directory to reflect the
state of that branch. Now, any changes you make will
be isolated to the "feature-branch" without affecting
the main branch or any other branches.
11. Branching in Git
Listing Branches:
● To view all branches in your repository, use the
following command:
git branch
● This command will display a list of local branches,
with an asterisk (*) indicating the currently active
branch.
12. Branching in Git
Deleting a Branch:
● When a branch has served its purpose or is no longer
needed, you can delete it using the git branch -d
command followed by the branch name. However,
note that you cannot delete the branch you are
currently on. First, switch to a different branch and
then delete the branch you wish to remove.
● To delete a branch, use the following command:
git branch -d <branch_name>
● Replace <branch_name> with the name of the
branch you want to delete.For example-
git branch -d feature-branch
● This command will delete the "feature-branch" from
your repository.
14. Merging Branches in Git
● Merging is a fundamental process in Git that
enables you to combine changes from one branch
into another.
● It plays a crucial role in integrating new features,
bug fixes, or improvements into the main branch or
any target branch.
● This part of the chapter will delve into the
intricacies of merging branches in Git, ensuring a
smooth and efficient integration of changes.
● Merging branches in Git involves bringing the
changes made in one branch into another branch.
15. Merging Branches in Git
● This process consolidates the work done in
different branches, allowing developers to
synchronize their efforts and ensure that all
relevant updates are incorporated into the final
codebase.
● By merging branches, you can take advantage of
parallel development, enabling different team
members to work on separate features
simultaneously.
● Git provides various strategies for merging
branches, depending on the complexity and
context of the changes.
17. Merging Branches in Git
Fast-forward Merge:
● A fast-forward merge is performed when the
branch you want to merge has a linear commit
history and there are no new commits in the target
branch since the divergence.
● In this case, the target branch simply moves
forward to include the new commits from the
source branch. This results in a linear commit
history, with all changes seamlessly integrated.
18. Merging Branches in Git
Fast-forward Merge:
● To perform a fast-forward merge, follow these
steps:
● Switch to the target branch:
git checkout <target_branch>
● Merge the source branch into the target branch:
git merge <source_branch>
● Replace <target_branch> with the branch you want
to merge into, and <source_branch> with the
branch containing the new commits.
19. Merging Branches in Git
Fast-forward Merge:
● For example, let's say you have a feature branch
named "feature-branch" and you want to merge it
into the main branch. The following commands
would accomplish this:
git checkout main
git merge feature-branch
20. Merging Branches in Git
Automatic Commit Merge:
● An automatic commit merge is performed when
both the target and source branches have new
commits since they diverged.
● Git creates a new commit that combines the
changes from both branches, ensuring that all
modifications are incorporated while maintaining a
clear history of the individual branches.
21. Merging Branches in Git
Automatic Commit Merge:
● To initiate an automatic commit merge, use the
following commands:
● Switch to the target branch:
git checkout <target_branch>
● Merge the source branch into the target branch:
git merge <source_branch>
● During an automatic commit merge, Git opens a text
editor where you can enter a merge commit message.
This message should provide a concise summary of
the changes being merged.
22. Merging Branches in Git
Automatic Commit Merge:
● For example, if you want to merge a feature branch
named "feature-branch" into the main branch, you
would run the following commands:
git checkout main
git merge feature-branch
24. Merge conflicts occur when Git encounters conflicting changes in
different branches during a merge operation. Resolving these conflicts
is an essential skill that every developer should master.
Merge conflicts
25. Resolving Merge Conflicts
Identifying Conflicts:
● When a merge conflict occurs, Git modifies the
affected files and marks the conflicting lines with
conflict markers. These conflict markers help you
identify the conflicting changes.
26. Resolving Merge Conflicts
Identifying Conflicts:
● Typically, Git uses the following markers:
<<<<<<< HEAD
... Changes from the current branch ...
=======
... Changes from the incoming branch ...
>>>>>>> incoming_branch
● The portion between <<<<<<< HEAD and =======
represents the changes made in the current branch,
while the portion between ======= and >>>>>>>
incoming_branch represents the changes made in
the incoming branch.
27. Resolving Conflicts:
● To resolve merge conflicts, follow these steps:
● Manually edit the conflicting files: Open the
conflicting files in a text editor and locate the
conflict markers. Review the conflicting changes and
determine the desired outcome.
● Remove conflict markers: Remove the conflict
markers and decide which changes to keep. You can
choose the changes from either the current branch or
the incoming branch, or modify the content to create
a new version that combines both sets of changes.
● Save the changes: After resolving the conflicts, save
the modified files.
28. Resolving Conflicts:
● Committing the Merge: Once you have resolved all the
conflicts, you need to stage the updated files and
create a merge commit to finalize the merge
operation.
● Add updated files to the staging area: Use the git add
command to add the modified files to the staging
area. This marks the resolved conflicts as ready to be
committed.
git add <file1> <file2> ...
29. Resolving Conflicts:
● Create a merge commit: Use the git commit
command to create a merge commit that
incorporates the resolved conflicts and completes
the merge operation. Provide a meaningful commit
message that describes the changes made during
the merge.
git commit
● By following these steps, you can effectively
resolve merge conflicts in Git, ensuring that
conflicting changes are harmoniously integrated
into the codebase.