GIT
Working with Branches in Git
In this chapter, we will explore how to use one of Git’s most powerful features: branches. Branches allow you to work on different features or bug fixes in isolation without affecting the code in the main branch. By the end of this chapter, you will know how to create, switch, and merge branches, as well as handle conflicts that may arise during the process.
What is a Branch?
A branch in Git is simply a pointer to a sequence of commits. The default branch in most projects is called main
, but you can create new branches to work on different lines of development. This is especially useful when working on a new feature or fixing a bug without interfering with the work of other developers or the code in production.
Creating and Switching Branches
To create a new branch in Git, use the command git branch
followed by the name of the new branch. Then, you can switch to that branch with the command git checkout
or the more recent git switch
.
bash
For example, if you are working on a new feature called "feature-X", you can create a branch for it:
bash
Merging Branches
Once you have completed your work on a branch, you will want to merge those changes back into the main branch (or another branch). To do this, use the git merge
command.
First, switch to the branch where you want to merge the changes (for example, main
), and then run the git merge
command followed by the name of the branch you want to merge.
bash
If there are no conflicts, Git will automatically merge the changes. If there are conflicts, Git will prompt you to resolve them manually.
Resolving Conflicts
Sometimes, when trying to merge two branches, a conflict may occur if Git cannot automatically decide how to combine the changes. In these cases, Git will mark the conflicting files and allow you to resolve them manually.
When a conflict occurs, you can see which files are in conflict using git status
. Then, open those files and you will see sections delimited by lines like <<<<<<<
, =======
, and >>>>>>>
, indicating the different changes.
After resolving the conflict in each file, add it back to the staging area with git add
and then complete the merge with a commit.
bash
Viewing Branches
To see a list of all branches in your repository, you can use the git branch
command. This will show all local branches, with an asterisk (*) next to the branch you are currently working on.
bash
If you want to see a more detailed graph of branches and how they relate to the commits, you can use the git log
command with the --graph
option:
bash
Deleting a Branch
Once you have merged a branch and no longer need it, you can delete it with the git branch -d
command:
bash
If you try to delete a branch that has not been merged, Git will warn you and ask you to use the -D
option (uppercase) to force its deletion.
Conclusion
In this chapter, we have learned to work with branches in Git, a fundamental tool for managing different lines of development within a project. You now know how to create, switch, merge, and delete branches, as well as handle conflicts that may arise during merges. In the next chapter, we will delve into collaboration with other developers, including how to work with remote repositories and synchronize your changes with those of other people.
- Introduction to Git and Version Control
- Installation of Git and Initial Setup
- Understanding Repositories
- Basic Workflow in Git
- Working with Branches in Git
- Collaborating with Other Developers
- Undoing Changes in Git
- Working with Tags in Git
- Rebase and Squash in Git
- Stashing and Cleaning in Git
- Advanced Git Commands
- Hooks and Automation in Git
- GitHub and Repository Management
- Best Practices in Git
- Conclusion and Final Tips