Welcome to the Git Branch (Create) tutorial! In this lesson, we'll dive deep into one of the most fundamental concepts in Git โ creating and managing branches. By the end of this tutorial, you'll be able to create, switch, and merge branches with ease. Let's get started!
A Git branch is a separate line of development in your project. Each branch contains its own set of commits, allowing you to work on different features or bug fixes without affecting the main project.
Before we begin, make sure you have Git installed on your machine. If you're unsure, you can check out our Git Installation tutorial.
Now let's create a new branch! To do this, navigate to your project directory in the terminal and run the following command:
git branch <branch-name>Replace <branch-name> with the name you want for your new branch. For example:
git branch feature-branchThis command creates a new branch but does not switch to it. To switch to the new branch, use the following command:
git checkout <branch-name>git checkout feature-branchNow you're working on the feature-branch! Let's make some changes.
Create a new file called new_file.txt and add some content:
echo "Hello, feature branch!" > new_file.txtNext, let's commit these changes:
git add new_file.txt
git commit -m "Add new_file.txt to feature branch"To check the status of your branches, use the following command:
git branchYou should see a list of branches, with an asterisk next to the branch you're currently on:
* feature-branch
mainWhen you're ready to merge your branch back into the main branch, switch to the main branch first:
git checkout mainNext, merge the feature-branch into main:
git merge feature-branchIf there are any conflicts, you'll need to resolve them manually. Once resolved, commit the merge:
git commit -m "Merge feature-branch into main"If you no longer need the feature-branch, you can delete it:
git branch -d feature-branchWhat command is used to create a new branch in Git?
That's it for this Git Branch (Create) tutorial! In the next lesson, we'll dive deeper into Git branches, exploring how to manage multiple branches and resolve merge conflicts. Until then, happy coding! ๐