When thinking of a branch: the first thing to come to mind is the part of a tree that is growing out from another part of the same tree. As a tree grows, branches are sent out from the trunk or from other branches. They grow leaves and help with supplying nourishment to the tree to become a better tree. When a specific branch dies, it might fall off or be pruned so another branch can grow in its place.
When it comes to git: branches work similarly. You have your trunk: but it's called Main (or Master if your repository was created before Oct 2020) that is the primary living portion of your code. You can create branches that copy the current Main code to allow you to build on it, make changes, and test things without effecting your live environment. Once you're done and successfully made changes, you can merge them into your Main. Branches can also be used to create other branches, and can be areas to merge other code into as well. After you're done with the branch, it can be deleted.
To get started with branches: there's a few common commands to learn:
git branch
Will list all branches, with an indicator on which one you're on. (Depending on your system this can be a symbol or different coloring)
git branch <nameOfBranch>
Will create a branch by the name of
git checkout <nameOfBranch>
Will change your pointer to the branch specified at
Using git checkout -b <nameOfBranch>
will create and switch you over to the new branch in one line!
git merge <nameOfBranch>
will combine content of branch specified in with the branch you're currently on. In the case of changes in the same areas from when both points were at similar states, you'll be notified of a merge conflict where you'll either be directed to select which changes to keep or have to manually correct the conflicting area.
git branch -d <nameOfBranch>
removes/deletes the specified branch. You do need to be on a different branch than the one you're trying to delete.
If you want to learn more about git branches be sure to check out additional documentation on branches. This includes different flags you can use, which can give more visibility to how you're displaying information about your branches, as well as extra things you can do!
Top comments (0)