Git Ready! — Basic Git Workflow. Have you heard about Git? GitLab is a…
By: Nabila Edina
Tags: Git, Gitlab, Version Control, Software Development

Photo by Pankaj Patel on Unsplash

Have you heard about Git? GitLab is a complete DevOps platform, delivered as a single application. With single application, it will speed up your workflow and help you deliver better software, faster. GitLab is an open source project to help teams collaborate on software development. It helps us to integrate, manage, configure, and maintain, slowing down your team and your deployments.

Let’s get started!

Before you can work with Git, the most basic tip is installing the system itself, if you don’t have it already. How can we do that? Well, there are several options for installing Git, it based on your operating systems. Use this commands to install:

// MacOS

$ brew install git // Linux

# apt-get install git

Go to: // WindowsGo to: https://git-scm.com/download/win

1. Repository Initialization

Now, you have the system on your local. You must initialize the Git Repository on your development environment (local computer) first. Let’s assume you already have the Git Repository as shown below:

Create a new repository on a local computer (PC or laptop)

If you want to get a copy of an existing Git repository, use this command. Make sure you are inside the working folder.

git clone <url>

Use the working directory that is already available to be a repository

If you want to use the working directory on your local to be a repository, you can use git init then git remote. git init is to create an empty Git repository or reinitialize an existing one and git remote is to manage a set of tracked repositories.

cd <existing_folder>

git init

git remote add origin <url>

2. Branching

A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process.

Create a new branch

If you want to create a new branch, use this command

git checkout -b <new_branch_name>

Switch to another branch

Let’s say you are now in a particular branch and you want to move to another branch on your repository. You can use this command

git checkout <branch_name>

See what branch you are in

You can use the command git branch to see what branch are you in.

git branch

An asterisk (*) in the branch name indicates that we are currently in that branch.

3. Adding Your Local Files to The Remote Repository

git add .

Use git add . to add all files to the given folder.

git status

It is to check on what is going on in your repository.

git commit -m “message”

It will add the change(s) to the local repository. ‘-m’ is used as a flag for a message to set the commits with the content where the full description is included. Write a brief and meaningful commit message. If the commit description can be summarized into one commit message line, make sure the commit message length is a maximum of 50 characters.

The example of commit messages

git push origin <branch name>

It is used for pushing local content to Gitlab.

git add .

git commit -m "commit_message"

git push origin <branch_name>

This is the example and the expected output after running the commands.

4. Pull Request

If you make a change in a repository, GIT PULL can allow others to view the changes.

git pull <remote_name> <branch_name>

5. Merge Request

If you’ve completed your branch and want to update your other branch with the completed branch, you can either do that in your remote repository site or use GitLab by creating a merge request. Make sure that both of the branches have been synchronized by git pull origin <target_branch> on the branch and resolve all conflicts that appear after pulling from the target branch.

Pro-Level Git Manual

git revert

You made a mistake? Don’t worry! The git revert command is used for undoing changes to a repository's commit history. Instead of removing the commit from the project history, it figures out how to invert the changes introduced by the commit and appends a new commit with the resulting inverse content.

git revert -m <parent_number> <commit_id>

git reset

If you have made a commit, but haven’t shared it with anyone else and you decide you don’t want it, then you can use git reset to rewrite the history so that it looks as though you never made that commit. Unlike git revert, it is used to undo changes in your working directory that haven’t been comitted yet.

Let’s say you have commits:

git reset --soft A , will change the commit history and repository; staging and working directory will still be at a state of C .

, will change the commit history and repository; staging and working directory will still be at a state of . git reset --mixed A , will change the commit history, repository, and staging; working directory will still be at a state of C .

, will change the commit history, repository, and staging; working directory will still be at a state of . git reset --hard A , will change the commit history, repository, staging, and working directory; you will go back to the state of A completely.

git stash

Stashing is a great way to pause what you’re currently working on and come back to it later.

git stash

For example, if you are working on something on a particular branch but you haven’t committed and move to another branch, it will appear like this:

In order to fix that, you need to stash them and after that, you can switch to another branch.

To bring back the work that we stashed we can use git stash pop like this:

git rebase

It is used to reapply commits on top of another base tip. Let’s say that the following history exists and the current branch is “branch-a”

A---B---C branch-a

/

D---E---F---G master

the result of either of the following commands:

git rebase master

git rebase master branch-a

would be:

A'--B'--C' branch-a

/

D---E---F---G master

NOTE: The latter form is just a short-hand of git checkout branch-a followed by git rebase master . When rebase exits branch-a will remain the checked-out branch.

For example, you want to reapply commits on top of another base tip and the current branch is “PBI-5-Test” with parent branch “PBI-6”.

We can see from the graph above that the branch PBI-5-Test has become on top of the branch PBI-6.

Difference With git merge

Some developers more familiar with git merge since it is more simple. Also, it preserves the complete history and chronological order and maintains the context of the branch. Git merge can combine two or more commits and it is fast forward.

For example, I want to merge branch PBI-5-Test with branch PBI-6.

We can see from the graph above that the branch PBI-5-Test and branch PBI-6 have the same HEAD or the same latest commit.

Try using git and you will love it! It will help your teams collaborate on your software development.

Source: