3

The Power of git branches

 1 year ago
source link: https://souravk.hashnode.dev/git-branch
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Hey everyone in this blog we will learn what git branches are and why it is one of the most important features of git that make developers like them so much. So let start

what are Git Branches?

In Git, branches are a part of your everyday development process. When you want to add a new feature or fix a bug—no matter how big or how small—you create a new branch to encapsulate your changes. This makes it harder for unstable code to get merged into the main code base, and it gives you the chance to clean up your code before merging it into the main branch.

0e550752-0104-4d69-81ff-6bbf5cb895bd.png?auto=compress,format&format=webp

In this diagram, as you can see the visualizes a repository the center line is the main branch line and the other two are features branches. By developing them in branches, it’s not only possible to work on both of them in parallel, but it also keeps the main branch free from questionable code.

The implementation behind Git branches is much more lightweight than other version control system models. Instead of copying files from directory to directory, Git stores a branch as a reference to a commit. In this sense, a branch represents the tip of a series of commits—it's not a container for commits. The history of a branch is extrapolated through the commit relationships.

How it works

A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process. You can think of them as a way to request a brand new working directory, staging area, and project history. New commits are recorded in the history for the current branch, which results in a fork in the history of the project.

The git branch command lets you create, list, rename, and delete branches. It doesn’t let you switch between branches or put a forked history back together again. For this reason, git branch is tightly integrated with the git checkout and git merge commands.

List all of the branches in your repository.

git branch

Create a new branch called <branch>. This does not check out the new branch.

git branch <branch>

Delete the specified branch. This is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changes.

git branch -d <branch>

Rename the current branch to <branch>.

git branch -m <branch>

List all remote branches.

git branch -a

Creating Branches

It's important to understand that branches are just pointers to commits. When you create a branch, all Git needs to do is create a new pointer, it doesn’t change the repository in any other way.

If you start with a repository that looks like this:

let's create a branch using the following command:

git branch bugfix

The repository history remains unchanged. All you get is a new pointer to the current commit:

Note that this only creates a new branch. To start adding commits to it, you need to select it with git checkout, and then use the standard git add and git commit commands.

Let's add some files and then we will commit it with this command:

git checkout bugfix
git add <filename>
git commit -m <messages>

After that, it will look like this.

Deleting Branches

Once you’ve finished working on a branch and have merged it into the main code base, you’re free to delete the branch without losing any history:

git branch -d bugfix

However, if the branch hasn’t been merged, the above command will output an error message:

error: The branch 'crazy-experiment' is not fully merged. If you are sure you want to delete it, run 'git branch -D crazy-experiment'.

This protects you from losing access to that entire line of development. If you really want to delete the branch (e.g., it’s a failed experiment), you can use the capital -D flag:

git branch -D crazy-experiment

This deletes the branch regardless of its status and without warnings, so use it judiciously.

Summary

The git branch commands primary functions are to create, list, rename and delete branches. To operate further on the resulting branches the command is commonly used with other commands like git checkout


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK