svg
Post Image
By Daniel Tanque19 de Outubro, 2023In GitLearning

Git Branches

In Git, branches are a way to work on different versions of a project simultaneously. They allow you to isolate different lines of development, making it easier to work on new features or fix bugs without affecting the main code base. In this article you will learn the principles behind branches and how to use them.

Basics of Branches

A branch is a pointer to the last commit, in which you can perform your changes and then merge to the master branch. We will see later on the best practices, for now you will learn the basic commands.

To create a branch you run:

git checkout -b <branch name>

To see all existing branches:

git branch

To change to an existing branch:

git checkout -b <branch name>

To delete a branch:

git branch -d <branch name>

Basics of Merging

Merging is the process of combining changes from one branch into another branch. It’s a fundamental operation when working with branches to integrate new features, bug fixes, or changes from one branch into another.

To make a merge you need to leave the branch you are at developing and pass to the one you want to merge with, normally to master thus you run the following command:

git checkout master
git merge feature/signup

There are two types of merging, fast-forward and no-fast-forward. Fast-forward merging is the simplest type of merge and occurs when the branch being merged into does not have any new commits since the branch to be merged. In this case, Git simply moves the branch pointer forward to include the new commits.

The no-fast-forward merging typically occurs when there are commits in both the source branch and the target branch that have diverged from a common ancestor. To perform a merge without fast-forwarding, you create a merge commit that combines the changes from both branches.

Best Practices

In terms of best practices you should always make atomic commits, so it becomes self-explanatory what was it’s purpose and when merging you can easily understand the development flow. To have a better view of the development/commit flow you can generate a graph, you can run the following command:

git log --graph --decorate

Conclusion

In overall you know now how to use merge, how to operate with branches and what the common best practices when working on big projects collaborating with other developers.

svgIntro to Git
svg
svgGit Remote

Leave a reply