Difference Between Gitlab and Github: Why Is Git Needed?
Difference Between Gitlab and Github: Why Is Git Needed?
There are many words to define git, but it is an open-source distributed version control
system in simpler words.
When a team works on real-life projects, git helps ensure no code conflicts between the
developers. Furthermore, the project requirements change often. So a git manages all
the versions. If needed, we can also go back to the original code. The concept of
branching allows several projects to run in the same codebase.
● GitLab is available
with many bugs and
it makes the user ● There is a limited
Disadvantage experience sloppy. private repository.
s ● It is difficult to ● It supports only Git
manage code version control.
reviews for
first-timers.
It is owned by Microsoft
Company It is owned by GitLab Inc.
Corporation.
Git Ignore:
Git ignore files is a file that can be any file or a folder that contains all the files that we
want to ignore. The developers ignore files that are not necessary to execute the project.
Git itself creates many system-generated ignored files. Usually, these files are hidden
files. There are several ways to specify the ignore files. The ignored files can be tracked
on a .gitignore file that is placed on the root folder of the repository. No explicit
command is used to ignore the file.
● dependency caches
● compiled code
traditional undo alternative. It does not delete any data in this process; instead, it will
create a new change with the opposite effect and thereby undo the specified commit.
It can be useful for tracking bugs in the project. If you want to remove something
Moreover, we can say that git revert records some new changes that are just
opposite to previously made commits. To undo the changes, run the below
command:
Syntax:
$ git revert
need the commit reference id. The git log command can access it.
Suppose you have made a change to a file say newfile2.txt of your project. And later,
you remind that you have made a wrong commit in the wrong file or wrong branch.
Now, you want to undo the changes you can do so. Git allows you to correct your
As you can see from the above output that I have made changes in newfile2.txt. We
can undo it by git revert command. To undo the changes, we will need the
$ git log
The above command will revert my last commit. Consider the below output:
As you can see from the above output, the changes made on the repository have
been reverted.
Git Cherry-pick
Cherry-picking in Git stands for applying some commit from one branch into another
branch. In case you made a mistake and committed a change into the wrong branch,
but do not want to merge the whole branch. You can revert the commit and apply it
on another branch.
The main motive of a cherry-pick is to apply the changes introduced by some existing
update the changes that were part of that last commit to the current working tree.
The definition is straight forward, yet it is more complicated when someone tries to
Cherry-pick is a useful tool, but always it is not a good option. It can cause duplicate
commits and some other scenarios where other merges are preferred instead of
ways such as merge and rebase command. Merge and rebase can usually apply
Why Cherry-Pick
Suppose you are working with a team of developers on a medium to large-sized
project. Some changes proposed by another team member and you want to apply
some of them to your main project, not all. Since managing the changes between
several Git branches can become a complex task, and you don't want to merge a
whole branch into another branch. You only need to pick one or two specific
commits. To pick some changes into your main project branch from other branches
is called cherry-picking.
Git cherry-pick is helpful to apply the changes that are accidentally made in the
wrong branch. Suppose I want to make a commit in the master branch, but by
In the above example, I want to make a commit for the master branch, but
accidentally I made it in the new branch. To make all the changes of the new branch
into the master branch, we will use the git pull, but for this particular commit, we will
Copy the particular commit id that you want to make on the master branch. Now
switch to master branch and cherry-pick it there. See the below output:
Syntax:
cherry-pick command and made that commit into my master branch. You can check
facilitates you to take the data created by git branch and integrate them into a single
branch. Git merge will associate a series of commits into one unified history.
Use the below command to merge the specified commit to currently active branch.
You can also merge the specified commit to a specified branch by passing in the
branch name in <commit>. Let's see how to commit to a currently active branch.
See the below example. I have made some changes in my project's file newfile1.txt
Copy the particular commit you want to merge on an active branch and perform the
To merge a specified commit into master, first discover its commit id. Use the log
$git log
To merge the commits into the master branch, switch over to the master branch.
git merge command along with master branch name. The syntax for this is as
follows:
Two files have changed in master branch. However, we have made this commit in the
test branch. So, it is possible to merge any commit in any of the branches.
Open new files, and you will notice that the new line that we have committed to the
Git allows merging the whole branch in another branch. Suppose you have made
many changes on a branch and want to merge all of that at a time. Git allows you to
Now, switch to the desired branch you want to merge. In the given example, I have
switched to the master branch. Perform the below command to merge the whole
As you can see from the given output, the whole commits of branch test2 have
When two branches are trying to merge, and both are edited at the same time and in
the same file, Git won't be able to identify which version is to take for changes. Such
a situation is called merge conflict. If such a situation occurs, it stops just before the
Suppose my remote repository has cloned by two of my team member user1 and
Now commit the changes and update it with the remote repository. See the below
output:
Now, at the same time, user2 also update the index file as follows.
User2 has added and committed the changes in the local repository. But when he
tries to push it to remote server, it will throw errors. See the below output:
In the above output, the server knows that the file is already updated and not merged
with other branches. So, the push request was rejected by the remote server. It will
throw an error message like [rejected] failed to push some refs to <remote URL>. It
will suggest you to pull the repository first before the push. See the below command:
In the given output, git rebase command is used to pull the repository from the
remote URL. Here, it will show the error message like merge conflict in <filename>.
Resolve Conflict:
To resolve the conflict, it is necessary to know whether the conflict occurs and why it
occurs. Git merge tool command is used to resolve the conflict. The merge
The above output shows the status of the conflicted file. To resolve the conflict, enter
in the insert mode by merely pressing I key and make changes as you want. Press
the Esc key, to come out from insert mode. Type the: w! at the bottom of the editor to
save and exit the changes. To accept the changes, use the rebase command. It will
be used as follows:
In the above output, the conflict has been resolved, and the local repository is
Git Rebase
In Git, the term rebase is referred to as the process of moving or combining a
branch named test. If you merge this, then it will merge all commits in a time. But if
you rebase it, then it will be merged in a linear manner. Consider the below image:
The above image describes how git rebase works. The three commits of the master
branch are merged linearly with the commits of the test branch.
How to Rebase
When you made some commits on a feature branch (test branch) and some in the
master branch. You can rebase any of these branches. Use the git log command to
track the changes (commit history). Checkout to the desired branch you want to
Syntax:
$ git status
The above command is used to continue with the changes you made. If you want to
When the rebasing is completed. Push the repository to the origin. Consider the
Suppose that you have a branch say test2 on which you are working. You are now on
the test2 branch and made some changes in the project's file newfile1.txt.
$ git commit -m "new commit for test2 branch." [test2 a835504] new commitfor
test2
Now you are on the master branch. I have added the changes to my file, says
newfile.txt. The below command is used to add the file in the repository.
to rebase my test2 branch, what should I do? See the below rebase branch scenario:
Rebase Branch
If we have many commits from distinct branches and want to merge it in one. To do
so, we have two choices either we can merge it or rebase it. It is good to rebase your
branch.
From the above example, we have committed to the master branch and want to
This command will switch you on the test2 branch from the master.
Now you are on the test2 branch. Hence, you can rebase the test2 branch with the
This command will rebase the test2 branch and will show as Applying: new commit
It is a most common puzzling question for the git user's that when to use merge
command and when to use rebase. Both commands are similar, and both are used to
Rebasing is not recommended in a shared branch because the rebasing process will
create inconsistent repositories. For individuals, rebasing can be more useful than
merging. If you want to see the complete history, you should use the merge. Merge
tracks the entire history of commits, while rebase rewrites a new one.
Git rebase commands said as an alternative of git merge. However, they have some
key differences:
Merging can be performed on both public and private It is the wrong choice to
branches. use rebasing on public
branches.
Merging integrates the content of the feature branch Rebasing of the master
with the master branch. So, the master branch is branch may affect the
changed, and feature branch history remains feature branch.
consistence.
Usage
You use git blame like this: git blame NAME_OF_THE_FILE
Each line is annotated with the SHA, name of the author and date of the last commit.
Git Version Control System
A version control system is a software that tracks changes to a file or set of files over
time so that you can recall specific versions later. It also allows you to work together
The version control system is a collection of software tools that help a team to
manage changes in a source code. It uses a special kind of database to keep track of
Developers can compare earlier versions of the code with an older version to fix the
mistakes.
developing software without using version control is unsafe. It provides backups for
allows software teams to preserve efficiency and agility according to the team scales
● Simultaneously working
● Traceability
Just like an app has different updates due to bugs and additional feature addition,
version changes, git also supports this feature. Many developers can add their code
in parallel. So the version control system easily manages all the updates that are
done previously.
Git provides the feature of branching in which the updated code can be done, and
then it can be merged with the main branch to make it available to the users.