DEV Community

The daily developer
The daily developer

Posted on

Git and Github tutorial part 2

In this post we're going to learn how a team collaborates on a project and how to use several different git operations.

Git branching

Git makes it possible to create branches so we can test out different project variations.

What is a branch ?

A newly created branch is a different version of the project you're working on.
This branch not only includes commits from the main branch but also includes commits that the main branch does not currently have.

git branch

We use this command to check the current branch.

git branch 
Enter fullscreen mode Exit fullscreen mode

The output of this command would be all the branches that the project has. The branch you're currently on will display an asterisk next to it like this.

main 
branch-1
branch-2
* feature -1
Enter fullscreen mode Exit fullscreen mode

How to create a new branch ?

By using :

git branch "name-of-the-branch"
Enter fullscreen mode Exit fullscreen mode

Branch names cannot contain space, and should have a descriptive name.

git checkout

You can switch from a branch to another by using the git command

git checkout branch-name
Enter fullscreen mode Exit fullscreen mode

Git merge

To update the main branch and have it include all the modifications made to the new branch we use this command:

git merge name-of-the-branch
Enter fullscreen mode Exit fullscreen mode

First you switch to the main branch then run the command above.

Merge conflicts

If you're collaborating with a team, you will find that the main branch will include new changes than are not present on your branch, especially if these changes is affecting the current files you're working on. This will create merge conflicts.

Merge conflicts is when git is not sure which changes you want to keep.

So when you try to merge git will ask you which version to keep. Delete the version you do not want along with

<<<<<<< HEAD
Enter fullscreen mode Exit fullscreen mode

along with any other git markings such as =====

Delete a branch

To delete a branch we use this command:

git branch -d "your-branch-name"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)