Branch
In Git, a branch is a sequence of commits that represents the evolution of the project (or a specific feature) over time. There's always at least one branch in a repository, usually called the main branch.
You can create new branches to work on new features or bug fixes and merge them back into the main branch when they're ready. When you create a new branch, it starts from the latest commit on the current branch.
The term feature branch refers to a normal branch devoted to a specific feature or task. A feature branch can be named in any way that makes sense for you or your team, it doesn't matter to Git (e.g., login, feature-login, feature/login, bugfix/issue-123, etc.)
Branches allow you to experiment with new ideas, work on different features in parallel, and isolate changes from the main branch until they're ready to be integrated. While you can work on only one branch at a time, you can switch between different branches as needed.
Branches are lightweight and easy to create or manage in Git. Creating a branch is instantaneous and doesn't require duplicating any files. This makes it easy to create multiple branches for different features or experiments without worrying about storage space or performance.
Cloning or fetching from a remote repository creates read-only copies of the branches in that remote repository. These branches are called remote-tracking branches. These branches are named using the format
<remote-name>/<branch-name>(e.g.,origin/main). They let you see the state of the branches in the remote repository without affecting your local branches.Main entry: Remote-tracking branch
Examples
Create a new branch called new-feature and switch into it right away:
git switch -c new-featureSwitch to the main branch:
git switch mainMerge the other-feature branch into the current branch:
git merge other-featureDelete the other-feature branch:
git branch -d other-featureSet your current branch to track the remote branch origin/feature:
# Currently, on a branch named `feature`:git branch --set-upstream-to=origin/feature# Shorter version:git branch -u origin/feature# Now, you can run `git pull` without specifying# the remote branch. This will fetch changes# from `origin/feature` and merge them into# the `feature` branch:git pull