Chuck's Academy

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
"The 'git branch' command creates a new branch but does not switch to it automatically. To work on the new branch, use 'git switch' followed by the branch name. This allows you to work on a new line of development without affecting the main branch."

For example, if you are working on a new feature called "feature-X", you can create a branch for it:

bash
"With 'git branch feature-X' you create a new branch called feature-X. Then, you switch to that branch with 'git switch feature-X' to start working on it."

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
"First, switch to the main branch with 'git switch main'. Then, use 'git merge feature-X' to combine the changes from the feature-X branch into the main branch."

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
"After resolving the conflict in the affected files, use 'git add filename' to add the modified files and then 'git commit' to complete the merge."

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
"The 'git branch' command displays a list of all local branches in your repository. The active branch will be marked with an asterisk."

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
"The 'git log --oneline --graph --all' command shows you a graphical history of commits and branches in the repository. It’s a useful way to see how branches have diverged and merged over time."

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
"The 'git branch -d' command followed by the branch name deletes that branch from the local repository. It’s good practice to delete branches that are no longer being used to keep your repository clean."

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.


Ask me anything