Git and GitHub Programming
Chapter No 3 : Branching in Git
Branching in Git is a powerful feature that allows you to work on different versions
of your project simultaneously. Here's a quick overview to help you get started:
Basic Concepts
● Branch: A branch in Git is a lightweight movable pointer to one of these
commits. The default branch name in Git is main (formerly master).
● Commit: A snapshot of your project at a specific point in time. Each
commit has a unique ID.
● HEAD: A pointer to the current branch reference, and thus to the latest
commit on that branch.
Git Branch
A branch is a version of the repository that diverges from the main working
project. It is a feature available in most modern version control systems. A Git
project can have more than one branch. These branches are a pointer to a snapshot
of your changes. When you want to add a new feature or fix a bug, you spawn a
new branch to summarize your changes. So, it is complex to merge the unstable
code with the main code base and also facilitates you to clean up your future
history before merging with the main branch.
Git Master Branch
The master branch is a default branch in Git. It is instantiated when first commit
made on the project. When you make the first commit, you're given a master
branch to the starting commit point. When you start making a commit, then master
branch pointer automatically moves forward. A repository can have only one
master branch.
Master branch is the branch in which all the changes eventually get merged back. It
can be called as an official working version of your project.
Why Branching in Git
Using branching in Git is essential for effective version control and collaboration.
Here are some key reasons to use branching:
1. Feature Development: Branches allow you to develop new features or
functionalities separately from the main codebase. This way, you can work
on new features without disrupting the stable version of the project.
2. Bug Fixes: When you need to fix bugs, creating a separate branch for each
fix ensures that you can address issues without affecting ongoing
development. Once the fix is complete, it can be tested and merged back into
the main branch.
3. Experimentation: Branches are ideal for experimenting with new ideas or
approaches. You can create a branch for experimental work and test changes
without risking the stability of the main codebase.
4. Parallel Work: Multiple developers or teams can work on different
branches simultaneously. This parallel development helps in speeding up the
development process and allows for concurrent contributions without
interference.
5. Code Reviews: Branches facilitate code reviews by allowing you to isolate
changes and submit them for review before merging into the main branch.
This ensures that only reviewed and approved code is integrated into the
main project.
6. Release Management: Branches can be used to manage different stages of
your release cycle, such as development, staging, and production. This helps
in maintaining separate environments and controlling the release process
more effectively.
7. Version Control: Branches help in maintaining different versions of the
codebase. You can create branches for different releases or versions of your
software, making it easier to track changes and manage releases.
8. Collaboration: Branching makes it easier for teams to collaborate on the
same project. Each team member can work on their own branch, reducing
the risk of conflicts and making it easier to integrate changes.
9. Conflict Resolution: By working on separate branches, you can isolate and
resolve conflicts more efficiently. When merging branches, Git helps
identify and address conflicts in a controlled manner.
10.Rollback and Recovery: If something goes wrong in a branch, you can
easily discard or reset the branch without affecting the main codebase. This
makes it easier to recover from mistakes and maintain stability.
In summary, branching in Git provides flexibility, organization, and control over
the development process, making it a fundamental practice for managing and
maintaining code effectively.
Operations on Branches :
We can perform various operations on Git branches. The git branch
command allows you to create, list, rename and delete branches. Many
operations on branches
The Operations that can be performed on a branch:
1) Create Branch :
You can create a new branch with the help of the git branch command. This
command will be used as:
Syntax:
git branch <branch name>
Output:
This command will create the branch B1 locally in Git directory.
2)List Branch
You can List all of the available branches in your repository by using the following
command.
Either we can use git branch - list or git branch command to list the available
branches in the repository.
Syntax:
git branch --list
or
git branch
Output:
Here, both commands are listing the available branches in the repository. The
symbol * is representing currently active branch.
3)Delete Branch
You can delete the specified branch. It is a safe operation. In this command, Git
prevents you from deleting the branch if it has unmerged changes. Below is the
command to do this.
Syntax:
git branch -d<branch name>
Output:
This command will delete the existing branch B1 from the repository.
The git branch d command can be used in two formats. Another format of this
command is git branch D. The 'git branch D' command is used to delete the
specified branch.
git branch -D <branch name>
4)Delete a Remote Branch
You can delete a remote branch from Git desktop application. Below command is
used to delete a remote branch:
Syntax:
git push origin -delete <branch name>
As you can see in the above output, the remote branch named branch2 from my
GitHub account is deleted.
5)Switch Branch
Git allows you to switch between the branches without making a commit. You can
switch between two branches with the git checkout command. To switch between
the branches, below command is used:
git checkout<branch name>
Switch from master Branch
You can switch from master to any other branch available on your repository
without making any commit.
Syntax:
git checkout <branch name>
As
you can see in the output, branches are switched from master to branch4 without
making any commit.
6) Switch to master branch
You can switch to the master branch from any other branch with the help of below
command.
Syntax:
git branch -m master
As you can see in the above output, branches are switched from branch1 to
master without making any commit.
7)Rename Branch
We can rename the branch with the help of the git branch command. To rename a
branch, use the below command:
Syntax:
git branch -m <old branch name><new branch name>
Output:
As you can see in the above output, branch4 renamed as renamedB1.
8)Merge Branch
Git allows you to merge the other branch with the currently active branch. You can
merge two branches with the help of git merge command. Below command is used
to merge the branches:
Syntax:
git merge <branch name>
From the above output, you can see that the
master branch merged with renamedB1. Since I have made no-commit before
merging, so the output is showing as already up to date.
9)Pulling branches
Pulling branches in Git is a crucial operation for keeping your local repository
up-to-date with changes from a remote repository. Here’s a detailed explanation of
the process and its significance:
What Does "Pulling" Mean?
In Git, "pulling" refers to the action of fetching changes from a remote repository
and merging them into your local branch. This ensures that your local copy of a
branch reflects the latest changes made by others.
Why Pulling Branches Is Important
1. Synchronizing with Remote Changes: Pulling updates your local branch
with the latest changes from the remote repository. This is essential for
staying current with the work of other team members and avoiding conflicts.
2. Collaboration: When multiple people are working on the same project,
pulling changes ensures that everyone has access to the latest code, which is
crucial for effective collaboration.
3. Avoiding Conflicts: Regularly pulling from the remote repository helps you
integrate changes incrementally, reducing the likelihood of complex
conflicts that might arise if you only pull infrequently.
How to Pull a Branch in Git
To pull changes from a remote branch into your local branch, follow these steps:
1. Check Your Current Branch
Before pulling, ensure you're on the branch you want to update. You can
check your current branch using:
git branch
The branch with an asterisk (*) is your current branch.
2. Pull Changes
Use the git pull command to fetch and merge changes from the remote
branch into your local branch. The command is:
git pull <remote> <branch>
o <remote> is the name of the remote repository (usually origin).
o <branch> is the name of the branch you want to pull from.
For example, to pull changes from the main branch of the origin remote, you would use:
git pull origin main
If you omit the branch name, Git will pull changes from the branch that is
currently tracked by your local branch.
3. Handle Conflicts (If Any)
3) Handle Conflicts (If Any)
If there are conflicts between your local changes and the pulled changes, Git
will notify you. You’ll need to resolve these conflicts manually. After
resolving conflicts, you’ll commit the resolved changes:
git add <file>
git commit
4. Verify Updates
After pulling, you might want to verify that the updates have been applied
correctly. You can view the commit history to see the latest changes:
git log
Tips for Effective Pulling
● Regularly Pull: To avoid large merges and conflicts, make it a habit to pull
changes regularly, especially before starting new work.
● Use git fetch and git merge: If you prefer more control, you can use git
fetch to fetch changes without merging, followed by git merge to manually
merge them:
git fetch origin
git merge origin/main
● Check Remote Branches: Before pulling, you can list remote branches to
ensure you are pulling the right one:
git branch -r
In summary, pulling branches in Git is a fundamental practice for keeping your
local repository synchronized with remote changes. It ensures that you are working
with the latest code, facilitates collaboration, and helps manage conflicts
efficiently.
10) Pushing branches
Pushing branches in Git involves uploading your local branch and its commits to a
remote repository. This process ensures that others can access your changes and
collaborate on the same branch. Here’s a detailed guide on how to push branches in
Git and why it's important:
Why Push Branches?
1. Share Your Work: Pushing allows you to share your local changes with
others working on the same project. This is crucial for collaboration and
integrating your changes into the central repository.
2. Backup Your Work: Pushing your changes to a remote repository acts as a
backup. If something happens to your local repository, you can retrieve your
work from the remote repository.
3. Continuous Integration and Deployment: In many workflows, pushing
code triggers automated processes such as continuous integration (CI) builds
and deployments. This ensures that your changes are tested and deployed
according to the project's workflow.
4. Collaboration: Pushing your branch enables others to review, comment on,
or build upon your work. This is especially important in team environments
where code review and collaborative development are key.
How to Push a Branch in Git
To push a local branch to a remote repository, follow these steps:
1. Check Your Current Branch
Make sure you are on the branch you want to push. You can check your
current branch with:
git branch
The branch with an asterisk (*) is your current branch.
2. Push the Branch
Use the git push command to push your local branch to the remote
repository. The basic syntax is:
git push <remote> <branch>
o <remote> is the name of the remote repository (often origin).
o <branch> is the name of the branch you want to push.
For example, to push your local feature-branch to the origin remote, you
would use:
git push origin feature-branch
3. Set Upstream Tracking (Optional)
If you’re pushing a branch for the first time, you may want to set it to track
the remote branch. This simplifies future pushes and pulls. You can do this
with:
git push --set-upstream origin <branch>
After this, you can use git push and git pull without specifying the remote
and branch, as Git will remember your upstream configuration.
4. Verify the Push
After pushing, you can verify that your changes are on the remote repository.
This can be done by checking the remote repository on a platform like
GitHub, GitLab, or Bitbucket, or by using:
git branch -r
This command lists remote branches, allowing you to see if your branch was
successfully pushed.
Top of Form
Bottom of Form