In Git, there are common misconceptions about branching. For example, some assume that a branch is a group of commits, or that a branch must diverge in the Git graph.
In fact, a branch is simply a pointer to one specific commit.
The branch pointer moves along with each new commit you make, and only diverges in the graph if a commit is made on a common ancestor commit.
The branching model offered by Git is lightweight compared to other version control systems, and helps protect you from merging unstable code into the main code base and gives you the chance to clean up your history before merging into the main branch.
Creating a new branch in Git is quick and easy.
You can create a Git branch from any commit in your commit history; this can be great if you want to make changes starting from a previous point in your project.
If you want to create a Git branch using the terminal, you will use the git branch command, followed by your desired branch name. This will create a Git branch on your currently checked-out reference.
Learn more about how to create a branch in Git, including how to use one command to create a and checkout your new branch simultaneously.
You can easily rename a Git branch at any time.
To rename a Git branch locally using the terminal, you will use git branch -m followed by the desired new branch name.
But if you are attempting to rename a branch that has already been pushed to a remote, you will need to push the new branch to the remote and update the upstream using the git push command with the -u (or --set-upstream) option.
See step-by-step how to rename a Git branch, including how to rename a local Git branch that you do not have checked out.
If you decide you no longer need it, you can delete a Git branch to clean up your repository.
To delete a Git branch using the terminal, you're going to want to use the git branch -d command along with the name of the local branch you want to delete. Learn more about how to delete a Git branch locally.
To delete a remote branch in Git, you're actually going to use the git push command. Learn more about how to delete a remote branch in Git.
As the number of branches in your Git repository grows, there will likely come a time when you will be working on multiple tasks at the same time and will need the ability to move from one branch to another.
To switch to another branch in Git, you’re going to have to checkout the branch using the git checkout command.
Now, let’s go over how branching works when you’re using a Git client like GitKraken to visualize your repository.
In this example, we’re working with two Git branches: dev and production; to begin work on a new feature, we need to create a feature branch off the latest commit on the dev branch.
To create a Git branch using GitKraken, right-click on the target commit and select Create branch here from the menu.
To rename a Git branch using GitKraken, simply right-click on the branch name and select Rename [branch name].
To delete a Git branch in GitKraken, right-click the branch name from the graph and select Delete [branch name].
The GitKraken Git client makes Git faster and more intuitive with its graphical user interface.