git branch command: create, remove, checkout, list, change name, option
In this post, we'll cover how to properly understand branches and how to create, delete, change, verify, rename, and other options using the git branch
command.
1. Understanding git branch
Branching is a key feature of collaboration in Git. In traditional file systems, if you wanted to modify the same file at the same time, the only way to do so was to make copies, modify them separately, and then manually merge them back together. With branches, Git enables different operations on the same file at the same time, almost like a multiverse.
The principle behind this useful feature is actually quite simple. In the previous git add and git commit posts, we mentioned that a commit object, which stores information about a single commit, has an ID for its parent commit. The commit history is eventually linked by the parent commit ID for each commit.
A branch is just a name tag that points to the most recent commit in this commit history.
If we're working in the main
branch, the branch name main has the last commit ID in the current commit history.
This looks like the following image
Since commit B knows the ID of commit A, 2b89
, the two commits can be linked by history.
And since the branch named main
knows the ID of commit B, 991b
, which is the last commit in that history, we call the current branch main
.
If we were to make a new commit in the current branch, the last commit ID in the main
tag would change to the new commit ID.
This would look like the following
This way, the last commit ID is the only information used to implement the branch function. And even changing branches, which we'll look at in the next section, is just a matter of jumping to the appropriate commit based on the iD in the branch list.
If we were to create a new branch and make a new commit to that branch, it would look something like this
If you create a new branch called topicA
in the main
branch, both main
and topicA
will point to v274
. When you create a new commit in the topicA
branch, the topicA
branch changes to point to commit D's ID 1g8c
.
In this situation, switching to the main
branch would take you to commit C, and switching back to the topicA
branch would take you to commit D. It's important to remember that a branch is a name tag for the last commit ID.
2. Getting a list of local and remote branches
The command to see all the branches in the current local repository is
$ git branch
In this case, the branch marked with a *
is the current working branch. The result of the execution will be
The git branch
listing command is available with the -r
and -a
options. Use them to print only remote branches, or both local and remote branches.
Use them as follows
3. Create a new branch
To create a new branch that points to the same commit as the current branch, there are three commands you can use, with slight differences.
First, the command below is the most basic branch creation command.
$ git branch <new-branch-name>
The second command is to create a branch and then immediately move to it. You can skip the step of creating and then moving.
$ git checkout -b <new-branch-name>
The last command is newer than checkout
and produces the same result. This is the recommended method.
$ git switch -c <new-branch-name>
Here is the result of the execution
After creating a new branch as described, the change was made to that branch.
4. Copy the remote branch to your local environment
To copy a branch created in a remote repository to your local environment, use the -t
option with the checkout
or switch
command. If you do not specify a new branch name, it will be copied with the same name as the remote branch.
$ git checkout -t <remote-name>/<branch-name>
or
$ git switch -c <new-branch-name> -t <remote-name>/<branch-name>
Here is the result of the execution
A feature-a
branch has been created in your local environment, tracking the feature-a
branch in the remote repo.
5. Moving to the change branch
There are two commands you can use to change the branch you are working on. They are checkout and switch. checkout is a traditional command that allows you to navigate not only to branches, but also to commits, tags, and other criteria. switch is a relatively newer command that was created solely for branch manipulation. For safe use of Git, we recommend using the switch command with its limited functionality.
Here's how to use both commands
$ git checkout <branch-name>
or
$ git switch <branch-name>
This is the result of the execution
6. How to delete and undelete a branch
There are two ways to delete a Git branch. The first is to delete a branch that has already been merged, and the second is to delete a branch that has not been merged.
6.1. Deleting a branch
The command to delete a branch that has been merged is as follows. You can't delete the branch I'm currently working on, so please go to another branch and enter it.
$ git branch -d <branch-name-merged>
In this case, merge commit has the last commit ID of the branch you are deleting as its parent commit ID. Therefore, all the commits that belonged to the branch will remain in the commit history of this repo.
Here are the commands to delete a branch that hasn't been merged.
$ git branch -D <branch-name-not-merged>
In this case, the pointer to the commit that belonged to the branch you're deleting disappears. This is known as a dangling or orphaned commit state, and Git's garbage collector takes care of cleaning up these inaccessible commits.
First, let's see the result of deleting a merged branch.
The git log graph above shows that we merged the feature-c
branch into the develop
branch.
If you then delete the feature-c
branch and print the log graph again, the feature-c
branch is gone, but the commits to that branch are still in the history.
This is because the merge commit, 0dd7b0b
, has the IDs of both commit 80d10f1
in the develop
branch and commit 8480ae1
in the feature-c
branch.
Here is the result of deleting the unmerged branch
If you look at the log graph above, you can see that we created a new feature-d
branch in the develop
branch and made a new commit. Then we deleted the feature-d
branch without merging it, and it disappeared from the log graph.
6.2. Undelete a branch
Finally, let's take a look at how to undelete a branch you accidentally deleted. As you may have noticed, the delete branch command prints out a success message and the last commit ID the branch pointed to.
For example, 3a31806
in the message Deleted branch feature-d (was 3a31806)
. You can see this in the screenshot above.
To undelete a branch, you need to use a commit ID like this. The command looks like this Put the commit ID in the <base-commit-id>
part.
$ git branch <branch-name> <base-commit-id>
Let's revert the two branches we deleted above with this command
The commit history will look like this
You can see that the feature-c
, feature-d
branches have been resurrected from their previous locations.
6.3. Deleting a remote branch
All Git hosting services support deleting a remote branch with a GUI, but you can also delete it with a command.
The command looks like this.
$ git push <remote-name> --delete <branch-name>
This is the result of running
If there is a branch in your local environment that tracks the remote branch you deleted, it will not be deleted along with it. Please delete it using the command we learned in Section 61 .
7. Conclusion
The git branch
command is the basis for working on multiple projects at once, and it's these concepts that we've covered. I hope this article has helped you understand and use branch properly.
