Welcome to our comprehensive Git Branch tutorial! In this lesson, we'll explore the power of Git branches, learning how to create, switch, merge, and delete branches to manage your projects efficiently.
A Git branch is a separate line of development in a Git repository. It allows you to work on different features, fixes, or experiments independently without affecting the main project.
Let's create a new branch named feature-branch:
$ git branch feature-branchNow, let's switch to the new branch:
$ git checkout feature-branchš Note: By default, Git creates branches based on the current HEAD, so feature-branch will start from the same commit.
Now, you can make changes in your project as usual. Let's add a new file new-file.txt and commit the changes:
$ touch new-file.txt
$ git add new-file.txt
$ git commit -m "Adding new file"To see all branches, use the following command:
$ git branchYou should see both master and feature-branch.
When your feature is complete, you can merge feature-branch into master:
$ git checkout master
$ git merge feature-branchAfter merging, you can delete the feature-branch:
$ git branch -d feature-branchWhen merging branches, conflicts may occur. Resolve them manually and then commit the merge again.
Which command creates a new Git branch?
How do you switch to a different branch in Git?