Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 27, 2021 10:49 pm GMT

Git Tutorial: Learn Git Branching in 5 minutes

Git is one of the most widely used version control systems and is an important tool for every developer to know. One of the greatest benefits of Git is its branching capabilities. Git branching is a fundamental aspect of your version control workflow. Today, well discuss how to create, delete, merge, and rebase Git branches. Afterward, well cover the next steps you can take to further your Git knowledge.

We'll cover:

What is branching?

Imagine youre working on a project with your team, and youre creating a new feature that requires a lot of changes to the codebase. In the process, you discover a bug that needs to be fixed. This bug is related to your new feature, and you dont want to affect your code. This situation could become complicated. Where will you store the code youve been working on?

Thats where Git branches come in. Branching is a core concept of source control that allows you to separate your work into different branches so you can work freely on your source code without affecting anyone elses work or the actual code in the main branch.

With Git, you begin with a single primary branch called main. Git allows you to create as many branches as you want.

Note: You dont always have to create branches from the main branch. You can create a new branch from any other branch.

In your separate branches, youre able to experiment and test without directly affecting your source code. Branching allows you and your team to switch between contexts without worrying about affecting different branches. Its a great tool for teams because it makes collaboration easier, convenient, and flexible.

Note: Git branches are different from SVN branches. Git branches are useful in your everyday workflow, whereas SVN branches are used in large-scale development efforts.

Creating branches

A local branch is only accessible to you on your physical device. Before you can push a branch to a remote repository, you need to create a local branch first.

If you already have a local branch, you can use git checkout:

git checkout <name>

If you dont already have the branch you need, you can create a new branch like this:

git checkout -b <name>

Now that we know how to create a new local branch, lets take a look at how to create a remote branch.

Creating remote branches

You can push your local branch to a remote repo and it will become a remote branch. When a branch is remote, that means anyone with access to the remote repository now has access to the remote branch.

If you want to push your local branch to the remote repo, you can use git push:

git push -u origin <name>

Deleting branches

Now that we know how to create a local branch and a remote branch, lets learn how to delete them. Well start with deleting a local branch using the -d flag:

git branch -d <name>

Sometimes, Git refuses to delete your local branch. This happens when your branch has commits that havent been merged into other branches or pushed to remote repositories. This refusal is meant to protect you from accidentally losing your data.

If youre sure you want to delete your branch, you can use the -D flag. This flag forces deletion.

git branch -D <name>

Deleting remote branches

To delete a remote branch, you use the git push command:

git push origin --delete <name>

Merging branches

Git merge is an essential function of branching. Merging allows you to join two or more branches through a commit. Only the target branch is changed. In our case, this would be the main branch. The history of your feature branch is preserved.

Lets think back to the feature youve been working on with your team. Your whole team has access to the main branch that you uploaded to the remote repository. Youre working on a small fix to the feature, and you dont want your work to affect the main branch.

You can create your own branch to experiment and test your changes. Once its completed and tested, you can merge that branch into the main so its accessible to everyone.

In our scenario, lets say you want to merge your experimental branch, called myfeature, into the main branch. Before merging, you should do a git fetch to gather up-to-date information about the remote repository.

git fetch --all

Then, go into the branch you want to merge your changes into and perform git checkout.

git checkout main

Now, you can merge myfeature into the main branch.

git merge myfeature

You can check your projects commit history on your repository to verify the merge. If your merge is successful and your work on myfeature is complete, you can delete that experimental branch since all the changes are now integrated into the main branch.

Rebasing branches

Git rebase is an advanced concept in Git. It can seem a bit complicated, but well go over the basics. Rebasing and merging are pretty similar. Both options take commits from a branch and put them onto another branch.

The major difference between rebasing and merging is that rebasing erases the history of your feature branch after it transfers the work from the feature branch to the main branch. Some developers prefer this method because it ensures a simple review process.

Heres how to use rebase:

git checkout myfeaturegit rebase main

Git concepts to learn next

Congratulations on taking your first steps with Git branching! Many teams use Git, so its important to have a good grasp of the control system and its features. Theres still so much to learn about Git. Some recommended concepts to cover next are:

  • Cherry-pick

  • Advanced git repository manipulation

  • Pull requests

  • Git commands

To get started with these concepts and more, check out Educatives course Learn Git the Hard Way. This course takes you through Git basics to more advanced concepts. By the end, youll have a strong understanding of Git, which will serve you well throughout your career.

Happy learning!

Continue reading about Git


Original Link: https://dev.to/educative/git-tutorial-learn-git-branching-in-5-minutes-1f8p

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To