Welcome to our Git Checkout (Branch) tutorial! In this lesson, you'll learn how to work with branches in Git, a powerful tool for managing code changes. Let's dive in! 💡
A Git branch is a separate line of development, allowing you to work on different features or fixes without affecting the main project. Think of it as a detour on a road where you can work on something new without blocking the main route.
To create a new branch, use the command:
git branch <branch-name>For example:
git branch feature-branchThis command creates a new branch named feature-branch but doesn't switch to it yet.
To switch to a newly created branch, use the command:
git checkout <branch-name>Now, let's create and switch to a new branch:
git branch feature-branch
git checkout feature-branchNow you're working on the feature-branch.
Now that you're on the feature-branch, you can make changes to your heart's content without affecting the main project.
Once you've made changes, you need to save them. Use the commands:
git add <file>
git commit -m "Your commit message"For example:
git add index.html
git commit -m "Added feature-branch"When you're ready to merge your changes into the main project, switch back to the main branch:
git checkout masterThen, merge the feature-branch into master:
git merge feature-branchIn case of conflicts between the branches, Git will alert you. You'll need to resolve these conflicts manually before you can successfully merge the branches.
To delete a branch, switch to the branch you want to delete and then use the command:
git branch -d <branch-name>For example:
git checkout feature-branch
git branch -d feature-branchWhat command creates a new Git branch?
How do you switch to a new Git branch?
How do you merge a Git branch into the main branch?