Branching & Cloning
Branching in Git allows for creating separate lines of development,
enabling developers to work on different features or experiments without
affecting the main codebase. It provides isolation, parallel development,
and easy integration of changes into the main branch when ready.
Why Branching ?
Parallel Development: Branching enables multiple developers to
work on different tasks simultaneously. Each developer can create
their own branch, make changes, and later merge those changes
back into the main branch.
Isolation: Branching provides a way to isolate changes and keep
them separate from the main codebase until they are fully
developed, tested, and ready for integration.
Feature Development: Branching allows developers to create
dedicated branches for new feature development. They can work on
implementing and testing the feature without interfering with the
stable version of the software.
Bug Fixing: Branches can be created specifically for addressing bugs
or issues reported in the main branch. Developers can fix the issues
in the bug-fixing branch and then merge the changes back into the
main branch.
Experimentation: Branching offers a safe space for experimenting
with new ideas or approaches. Developers can create a branch, try
out different solutions, and discard or merge the branch based on
the results.
Master Branch
The main/master branch is the primary branch in Git repositories. It
contains the stable and production-ready version of the project.
Developers collaborate by creating feature branches and merging them
into the main/master branch. Major releases and deployments often
happen from this branch. It serves as the default and central line of
development.
.gitignore File
The .gitignore file is used to specify which files and directories should be
ignored by Git, preventing them from being tracked or committed to the
repository. It helps keep the repository clean and focused on relevant files
by excluding unnecessary files and directories.
Working :
Branching allows for the creation of separate lines of development
in a Git repository.
Each branch represents an independent workspace with its own set
of commits and changes.
When a new branch is created, it initially points to the same commit
as the branch it was created from.
Developers can make changes in their branch, creating new
commits specific to that branch.
The branch's pointer moves forward with each new commit, keeping
track of the latest commit in the branch.
Developers can switch between branches to work on different tasks
or features.
Branches can be merged together, combining the changes from
multiple branches into a single branch.
Git identifies the common commit where the branches diverged and
applies the changes from the source branch to the target branch
during a merge.
If there are conflicting changes in the branches being merged, Git
helps resolve those conflicts.
Branches provide isolation, allowing developers to work
independently without affecting the main codebase.
Branches can be used for feature development, bug fixes,
experimentation, or any other independent line of work.
Branches can be pushed to remote repositories, allowing
collaboration with other developers.
Branches can be deleted once they have served their purpose or
merged into the main branch.
Creating Branches :
To create and work with branches using Git Bash, you can follow these
steps:
1. Open Git Bash on your machine.
2. Navigate to the repository directory using the cd command. For
example:
cd /path/to/repository
3. Check the existing branches in the repository using the command:
git branch
This will display a list of branches, with the current branch highlighted.
4. Create a new branch using the command:
git branch <branch-name>
Replace <branch-name> with the desired name for your branch.
5. Switch to the newly created branch using the command:
git checkout <branch-name>
This will switch your working directory to the specified branch.
6. Start making changes to your code, adding or modifying files as
needed.
7. Stage and commit your changes using the standard Git commands:
git add .
git commit -m "Commit message"
8. Push the branch to the remote repository using the command:
git push origin <branch-name>
Replace <branch-name> with the name of your branch.
Cloning Of Branches
To clone a specific branch using Git Bash, you can use the following
command:
clone -b <branch-name> <repository-url>
Replace <branch-name> with the name of the branch you want to clone,
and <repository-url> with the URL of the repository.
Example :
To clone the "development" branch of a repository, you would run:
git clone -b development https://github.com/example/repository.git
This command will clone the specified branch from the repository to your
local machine.
Mark as Read
Report An Issue