Git Basics

What is Gitlab?

Gitlab provides services that allow hosting your project on a remote repository and provides additional features that help in continuous integration and deployment. Such as code sharing, code review, and bug tracking. 


GIT Workflow

In Git there is the notion of a "Master" code base which contains the work of all contributing members in a project.

There are two basic workflows that you may follow when using Git for version control.

  1. Committing directly to the "Master" branch.
  2. Creating branches from the "Master" branch and merging them back in when ready.

This section of the guide will walk you through these two workflow strategies.


1. Working off the "Master" Branch

Working directly off the "Master" branch can be advantageous to smaller groups who rarely (if ever) work on the same portions of the code at a time.

                             rtosbook-Page-4-(1).png

The basic workflow for this method is as follows:

  1. "Pull" from the Master branch to ensure the local copy contains the latest version of the code.
  2. Make necessary changes to the code in your local repository.
  3. Commit your changes.
  4. "Push" your changes to the remote repository.

    rtosbook-Page-5.png

In git commands this would look like this:


2. Working with feature branches

The second workflow takes advantage of the branching system in git. To protect your Master branch from code that may break your build or introduce bugs we can create what is called a "feature branch." These branches contain your development code and isolate it from the main code until you are ready to merge them together.

The workflow is as follows:

  1. Do a "git fetch" to obtain the latest version of your source branch.
  2. Check out a new branch.
  3. Perform your work on your new branch (be sure to make regular commits to avoid losing any of your work.) 
  4. Merge the two branches.
rtosbook-Page-6.png
Here is the general workflow in git commands:

When you are ready to merge your branch back into the source branch there are two routes you may take:

  1. Merge your feature branch directly into the source branch.
  2. Open a pull request for peer code review prior to merging your branch.

To merge your feature branch into the source uses the following workflow:

  1. Check out the source branch.
  2. Ensure your source branch contains the most updated code from the remote repo.
  3. Merge your feature branch into the source branch.
  4. Push the newly merged source branch back to the remote repo.

The git commands for this workflow looks like this:


3. Merge Conflicts

When working in a team it will be inevitable that the same file will be touched by multiple developers. If multiple make changes in the same part of the file, then it will result in a merge conflict when attempting to merge the files together. These conflicts can be resolved in your IDE directly or in any text editor.

What is Git Merge Conflict? 

A merge conflict is an event that takes place when Git is unable to automatically resolve differences in code between two commits. Git can merge the changes automatically only if the commits are on different lines or branches.

rtosbook-Page-7-(2).pngLet’s assume there are two developers: Two developers pull the same code file from the remote repository and try to make various amendments to the same file. After making the changes, Developer 1 pushes the file back to the remote repository from his local repository. Now, when Developer 2 tries to push that file after making the changes from his end, he is unable to do so, as the file has already been changed in the remote repository.

To prevent such conflicts, developers work in separate isolated branches. The Git merge command combines separate branches and resolves any conflicting edits.

The git commands for this workflow looks like this:

 

Synonyms for "fetch" fetch the action of fetching More (Definitions, Synonyms, Translation)

Back to top