Version Control With Git
Version Control With Git
Developers used to submit their codes to the central server without having copies of their own
Any changes made to the source code were unknown to the other developers
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 every modification to the code.
Developers can compare earlier versions of the code with an older version to fix the mistakes.
The localized version control method is a common approach because of its simplicity. But this approach leads
to a higher chance of error. In this approach, you may forget which directory you're in and accidentally write to
the wrong file or copy over files you don't want to.
To deal with this issue, programmers developed local VCSs that had a simple database. Such databases kept all
the changes to files under revision control. A local version control system keeps local copies of the files.
The diagram below shows there are three files in the local system. A snapshot of these files are stored in the
remote repository as Version 1.
The major drawback of Local VCS is that it has a single point of failure.
All the versions of the file are stored on the Central VCS server
In case the central server crashes, the entire data of the project will be lost. Hence, distributed VCS was
introduced.
Every programmer has a copy of all the versions of the code on their local systems
Distributed VCS moves from the client-server approach of central VCS to a peer-to-peer approach
They can update their local repositories with new data from the central server and changes are reflected in
the principal repository
There are many benefits and drawbacks of using both the version control systems. Let's have a look at some
significant differences between Centralized and Distributed version control system.
In CVCS, The repository is placed at one place and In DVCS, Every user has a local copy of the repository in
delivers information to many clients. place of the central repository on the server-side.
It is the most straightforward system based on the concept It is flexible and has emerged with the concept that everyone
of the central repository. has their repository.
In CVCS, the server provides the latest code to all the In DVCS, every user can check out the snapshot of the code,
clients across the globe. and they can fully mirror the central repository.
CVCS is easy to administrate and has additional control DVCS is fast comparing to CVCS as you don't have to
over users and access by its server from one place. interact with the central server for every command.
The popular tools of CVCS are SVN (Subversion) The popular tools of DVCS are Git and Mercurial.
and CVS.
CVCS is easy to understand for beginners. DVCS has some complex process for beginners.
If the server fails, No system can access data from another if any server fails and other systems were collaborating via it,
system. that server can restore any of the client repositories
2. Basics of Git
What is Git?
Git is an open-source distributed version control system. It is designed to handle minor to major projects
with high speed and efficiency. It is developed to co-ordinate the work among the developers. The version
control allows us to track and work together with our team members at the same workspace.
Git is foundation of many services like GitHub and GitLab, but we can use Git without using any other Git
services. Git can be used privately and publicly.
Git was created by Linus Torvalds in 2005 to develop Linux Kernel. It is also used as an important distributed
version-control tool for the DevOps.
Git is easy to learn, and has fast performance. It is superior to other SCM(Source code management) tools like
Subversion, CVS(Concurrent Versions System), Perforce, and ClearCase.
Features of Git
Some remarkable features of Git are as follows:
1. Open Source
Git is an open-source tool. It is released under the GPL (General Public License) license.
2. Scalablility
Git is scalable, which means when the number of users increases, the Git can easily handle such
situations.
3. Distributed
One of Git's great features is that it is distributed. Distributed means that instead of switching the
project to another machine, we can create a "clone" of the entire repository. Also, instead of just having
one central repository that you send changes to, every user has their own repository that contains the
entire commit history of the project. We do not need to connect to the remote repository; the change is
just stored on our local repository. If necessary, we can push these changes to a remote repository.
4. Security
Git is secure. It uses the SHA1 (Secure Hash Function) to name and identify objects within its
repository. Files and commits are checked and retrieved by its checksum at the time of checkout. It
stores its history in such a way that the ID of particular commits depends upon the complete
development history leading up to that commit. Once it is published, one cannot make changes to its old
version.
5. Speed
Git is very fast, so it can complete all the tasks in a while. Most of the git operations are done on the
local repository, so it provides a huge speed. Also, a centralized version control system continually
communicates with a server somewhere. Performance tests conducted by Mozilla showed that it
was extremely fast compared to other VCSs. Fetching version history from a locally stored repository
is much faster than fetching it from the remote server. The core part of Git is written in C,
which ignores runtime overheads associated with other high-level languages.
Git was developed to work on the Linux kernel; therefore, it is capable enough to handle
large repositories effectively. From the beginning, speed and performance have been Git's primary
goals.
6. Supports non-linear development
Git supports seamless branching and merging, which helps in visualizing and navigating a non-linear
development. A branch in Git represents a single commit. We can construct the full branch structure
with the help of its parental commit.
Branching and merging are the great features of Git, which makes it different from the other SCM
tools. Git allows the creation of multiple branches without affecting each other. We can perform tasks
like creation, deletion, and merging on branches, and these tasks take a few seconds only. Below are
some features that can be achieved by branching:
We can create a separate branch for a new module of the project, commit and delete it
whenever we want.
We can have a production branch, which always has what goes into production and can be
merged for testing in the test branch.
We can create a demo branch for the experiment and check if it is working. We can also
remove it if needed.
The core benefit of branching is if we want to push something to a remote repository, we do not
have to push all of our branches. We can select a few of our branches, or all of them together.
8. Data Assurance
The Git data model ensures the cryptographic integrity of every unit of our project. It provides
a unique commit ID to every commit through a SHA algorithm. We can retrieve and update the
commit by commit ID. Most of the centralized version control systems do not provide such integrity by
default.
9. Staging Area
The Staging area is also a unique functionality of Git. It can be considered as a preview of our next
commit, moreover, an intermediate area where commits can be formatted and reviewed before
completion. When you make a commit, Git takes changes that are in the staging area and make them as
a new commit. We are allowed to add and remove changes from the staging area. The staging area can
be considered as a place where Git stores the changes.
Although, Git doesn't have a dedicated staging directory where it can store some objects representing
file changes (blobs). Instead of this, it uses a file called index.
Another feature of Git that makes it apart from other SCM tools is that it is possible to quickly stage
some of our files and commit them without committing other modified files in our working
directory.
10. Maintain the clean history
Git facilitates with Git Rebase; It is one of the most helpful features of Git. It fetches the latest commits
from the master branch and puts our code on top of that. Thus, it maintains a clean history of the project.
Benefits of Git
A version control application allows us to keep track of all the changes that we make in the files of our project.
Every time we make changes in files of an existing project, we can push those changes to a repository. Other
developers are allowed to pull your changes from the repository and continue to work with the updates that you
added to the project files.
Git is lightning fast technology. Each command takes only a few seconds to execute so we can save a lot
of time as compared to login to a GitHub account and find out its features.
2. Offline Working
One of the most important benefits of Git is that it supports offline working. If we are facing internet
connectivity issues, it will not affect our work. In Git, we can do almost everything locally.
Comparatively, other CVS like SVN is limited and prefer the connection with the central repository.
3. Undo Mistakes
One additional benefit of Git is we can Undo mistakes. Sometimes the undo can be a savior option for
us. Git provides the undo option for almost everything.
Git facilitates with some exciting features such as Diff, Log, and Status, which allows us to track
changes so we can check the status, compare our files or branches.
Why Git?
We have discussed many features and benefits of Git that demonstrate the undoubtedly Git as the leading
version control system. Now, we will discuss some other points about why should we choose Git.
1. Git Integrity
Git is developed to ensure the security and integrity of content being version controlled. It uses
checksum during transit or tampering with the file system to confirm that information is not lost.
Internally it creates a checksum value from the contents of the file and then verifies it when transmitting
or storing data.
Git is the most widely used version control system. It has maximum projects among all the version
control systems. Due to its amazing workflow and features, it is a preferred choice of developers.
3. Everything is Local
Almost All operations of Git can be performed locally; this is a significant reason for the use of Git. We
will not have to ensure internet connectivity.
5. Impress Recruiters
We can impress recruiters by mentioning the Git and GitHub on our resume. Send your GitHub profile
link to the HR of the organization you want to join. Show your skills and influence them through your
work. It increases the chances of getting hired.
Downloading
Step 1: Go to the official website: https://git-scm.com
Step 2: Click on 64-bit Git for Windows Setup and allow the download to complete.
Step 6: Select the install location. If you don’t have a reason to modify it, leave it to default and
click Next.
Step 7: A screen for component selection will display. Leave the settings as it is and click Next.
Step 8: The installer asks you to create a start menu folder. Simply click Next.
Step 9: Choose the text editor you want to use with Git and click Next.
Step 10: The following step allows you to give your original branch a new name. ‘Master’ is the
default. Leave the default choice selected and press the Next button.
Step 11: You can adjust the PATH environment during this installation phase. When you run a
command from the command line, the PATH is the default set of folders that are included. Continue
by selecting the middle (recommended) option and clicking Next.
Step 12: The following option concerns server certificates. The default choice is used by the majority
of users. Simply click Next.
Step 13: This step deals with how data is structured, and altering this option may create issues. So, it
is advised to leave the default selection.
Step 14: Select the terminal emulator that you wish to use. Because of its features, the default
MinTTY is suggested. Click Next.
Step 15: The installer now prompts you to specify what the git pull command should perform. Leave
the default selected option and click Next.
Step 16: The next step is to decide which credential helper to employ. Credential helpers are used by
Git to retrieve or save credentials. Leave the default selection and click Next.
Step 17: Although the default choices are suggested, this step allows you to select which additional
features to activate.
Step 18: Git offers to install some experimental features. Leave them unchecked and click Install.
Step 19: Once the installation is complete, launch the Git bash.
Command to create git repository in folder and store files and track changes.
1.git init
Set up global config variables with github account username and email- If you are working with other
developers, you need to know who is checking the code in and out, and to make the changes.
2.git config
The gitconfig command is used initially to configure the user.name and user.email. This
specifies what email id and username will be used from a local repository.
When gitconfig is used with --global flag, it writes the settings to all repositories on the
computer.
This will lead you to the Git help page on the browser, which will display the following:
This will lead you to the Git help page on the browser, which will display the following:
Create a file called index.html in the week1 folder; write something and save it.
4. Common Git Commands
GIT Repository
Repositories in GIT contain a collection of files of various different versions of a Project. These files are
imported from the repository into the local server of the user for further updations and modifications in
the content of the file. A VCS or the Version Control System is used to create these versions and store
them in a specific place termed as a repository.
Repositories in Git are of two types:
Local Repository: Git allows the users to perform work on a project from all over the world because
of its Distributive feature. This can be done by cloning the content from the Central repository stored in
the GitHub on the user’s local machine. This local copy is used to perform operations and test them on
the local machine before adding them to the central repository.
Remote Repository: Git allows the users to sync their copy of the local repository to other repositories
present over the internet. This can be done to avoid performing a similar operation by multiple
developers. Each repository in Git can be addressed by a shortcut called remote.
Git provides tools to perform work on these repositories according to the needs of the user. This
workflow of performing modifications to a Repository is referred to as the Working Tree.
Fig.Working Flow of Local Repository and Remote Repository
Git directory ( Local Repository) - Perform a commit that stores the snapshots permanently to your Git
directory. Checkout any existing version, make changes, stage them and commit.
Command to create git repository in folder and store files and track changes.
1.gitinit
Set up global config variables with github account username and email- If you are working with other
developers, you need to know who is checking the code in and out, and to make the changes.
2.gitconfig
The gitconfig command is used initially to configure the user.name and user.email. This
specifies what email id and username will be used from a local repository.
When gitconfig is used with --global flag, it writes the settings to all repositories on the
computer.
3. git help
This will lead you to the Git help page on the browser, which will display the following:
Create a file called index.html in the week1 folder; write something and save it.
3.git add
Add command is used after checking the status of the files, to add those files to the staging
area.
Before running the commit command, "git add" is used to add any new or modified files.
4.git commit
The commit command makes sure that the changes are saved to the local repository.
The command "git commit –m <message>" allows you to describe everyone and help them
understand what has happened.
The git status command tells the current state of the repository.
The command provides the current working branch. If the files are in the staging area, but not
committed, it will be shown by the git status. Also, if there are no changes, it will show the
message no changes to commit, working directory clean.
6.git log
The git log command shows the order of the commit history for a repository.
The command helps in understanding the state of the current branch by showing the commits
that lead to this state.
7.git diff
Save modified data to git repository using command git commit -a -m "modified"
5. Branching and Merging Strategies
Git Branch
A branch is a version of the repository that diverges from the main working project. It is a feature available in
most modern version control systems. A Git project can have more than one branch. These branches are a
pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new
branch to summarize your changes. So, it is complex to merge the unstable code with the main code base and
also facilitates you to clean up your future history before merging with the main branch.
Master branch is the branch in which all the changes eventually get merged back. It can be called as an official
working version of your project.
Operations on Branches
We can perform various operations on Git branches. The git branch command allows you
to create, list, rename and delete branches. Many operations on branches are applied by git checkout and git
merge command. So, the git branch is tightly integrated with the git checkout and git merge commands.
Create Branch
You can create a new branch with the help of the git branch command. This command will be used as:
Syntax:
Output:
List Branch
You can List all of the available branches in your repository by using the following command.
Either we can use git branch - list or git branch command to list the available branches in the repository.
Syntax:
or
1. $ git branch
Output:
Here, both commands are listing the available branches in the repository. The symbol * is representing currently
active branch.
Delete Branch
You can delete the specified branch. It is a safe operation. In this command, Git prevents you from deleting the
branch if it has unmerged changes. Below is the command to do this.
Syntax:
1. $ git branch -d<branch name>
Output:
This command will delete the existing branch B1 from the repository.
The git branch d command can be used in two formats. Another format of this command is git branch D. The
'git branch D' command is used to delete the specified branch.
Syntax:
Output:
As you can see in the above output, the remote branch named branch2 from my GitHub account is deleted.
Switch Branch
Git allows you to switch between the branches without making a commit. You can switch between two
branches with the git checkout command. To switch between the branches, below command is used:
You can switch from master to any other branch available on your repository without making any commit.
Syntax:
1. $ git checkout <branch name>
Output:
As you can see in the output, branches are switched from master to branch4 without making any commit.
Rename Branch
We can rename the branch with the help of the git branch command. To rename a branch, use the below
command:
Syntax:
Output:
Git Merge
In Git, the merging is a procedure to connect the forked history. It joins two or more development history
together. The git merge command 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. Generally, git merge is used
to combine two branches.
It is used to maintain distinct lines of development; at some stage, you want to merge the changes in one branch.
It is essential to understand how merging works in Git.
In the above figure, there are two branches master and feature. We can see that we made some commits in
both functionality and master branch, and merge them. It works as a pointer. It will find a common base commit
between branches. Once Git finds a shared base commit, it will create a new "merge commit." It combines the
changes of each queued merge commit sequence.
Use the below command to merge the specified commit to currently active branch.
The above command will merge the specified commit to the 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 and committed it in
my test branch.
Copy the particular commit you want to merge on an active branch and perform the merge operation. See the
below output:
In the above output, we have merged the previous commit in the active branch test2.
Merge Branch
Git allows you to merge the other branch with the currently active branch. You can merge two branches with
the help of git merge command. Below command is used to merge the branches:
Syntax:
Output:
From the above output, you can see that the master branch merged with renamedB1. Since I have made no-
commit before merging, so the output is showing as already up to date.
Example:-
Create three more text files in the local repository - “info1.txt”, “info2.txt”, “info3.txt”.
The above command switches to the new branch from the master branch.
The above command creates and adds “info3.txt” to the first_branch.
The above command shows that the new branch has access to all the files.
The above command shows that the master branch does not have an “info3.txt” file.
The above command is used to merge “first_branch” with the master branch. Now, the master branch has
“info3.txt” file.
Suppose my remote repository has cloned by two of my team member user1 and user2. The user1 made
changes as below in my projects index file.
Update it in the local repository with the help of git add command.
Now commit the changes and update it with the remote repository. See the below output:
It will show the status of the file like edited by whom and when.
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 command is used as follows:
1. $ git mergetool
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:
To see that which is the first edited text of the merge conflict in your file, search the file attached with conflict
marker <<<<<<<. You can see the changes from the HEAD or base branch after the line <<<<<<< HEAD in
your text editor. Next, you can see the divider like =======. It divides your changes from the changes in the
other branch, followed by >>>>>>> BRANCH-NAME. In the above example, user1 wrote "<h1>Git is a
version control</h1>" in the base or HEAD branch and user2 wrote "<h2>Git is a version control</h2>".
Decide whether you want to keep only your branch's changes or the other branch's changes, or create a new
change. Delete the conflict markers <<<<<<<, =======, >>>>>>> and create final changes you want to
merge.
GitHub is an American company. It hosts source code of your project in the form of different programming
languages and keeps track of the various changes made by programmers.
It offers both distributed version control and source code management (SCM) functionality of Git. It also
facilitates with some collaboration features such as bug tracking, feature requests, task management for every
project.
o Collaboration
o Integrated issue and bug tracking
o Graphical representation of branches
o Git repositories hosting
o Project management
o Team management
o Code hosting
o Track and assign tasks
o Conversations
o Wikisc:- Every repository on GitHub.com comes equipped with a section for hosting documentation, called a
wiki. You can use your repository's wiki to share long-form content about your project, such as how to use it, how
you designed it, or its core principles.
Benefits of GitHub
GitHub can be separated as the Git and the Hub. GitHub service includes access controls as well as
collaboration features like task management, repository hosting, and team management.
1.git remote
The git remote command is used to create, view, and delete connections to other
repositories.
The connections here are not like direct links into other repositories, but as bookmarks that
serve as convenient names to be used as a reference.
2.git push
The command git push is used to transfer the commits or pushing the content from the local
repository to the remote repository.
The command is used after a local repository has been modified, and the modifications are to
be shared with the remote team members.
Refresh your repository page on GitHub. You will get your local file on your remote GitHub repository
Fig.6.Now File is uploaded
3.remote commands
To check the configuration of the remote server, run the git remote command. The git remote command allows
accessing the connection between remote and local. If you want to see the original existence of your cloned
repository, use the git remote command. It can be used as:
git remote
And
git remote -v
3.2 Remove Remote
You can remove a remote connection from a repository. To remove a connection, perform the git remote
command with remove or rm option. It can be done as:
Syntax:
Or
In the above output, I have removed remote server "origin" from my repository.
Syntax:
Output:
In the above output, I have renamed my default server name origin to hd. Now, I can operate using this name in
place of origin. Consider the below output:
In the above output, I have pulled the remote repository using the server name hd. But, when I am using the old
server name, it is throwing an error with the message "'origin' does not appear to be a git repository." It
means Git is not identifying the old name, so all the operations will be performed by a new name.
We can change the remote URL simply by using the git remote set command. Suppose we want to make a
unique name for our project to specify it. Git allows us to do so. It is a simple process. To change the remote
URL, use the below command:
The remote set-url command takes two types of arguments. The first one is <remote name >, it is your current
server name for the repository. The second argument is <newURL>, it is your new URL name for the
repository. The <new URL> should be in below format: https://github.com/URLChanged
Usually, the original repository is located on a remote server, often from a Git service like GitHub,
Bitbucket, or GitLab. The remote repository URL is referred to the origin.
Syntax:
Step 1:
Step 2:
Select the Clone with HTTPs section and copy the clone URL for the repository. For the empty
repository, you can copy the repository page URL from your browser and skip to next step.
Step 4:
Open Git Bash and change the current working directory to your desired location where you want to
create the local copy of the repository.
Step 5:
Use the git clone command with repository URL to make a copy of the remote repository. See the
below command:
Now, Press Enter. Hence, your local cloned repository will be created. See the below output:
Syntax:
In the given output, only the master branch is cloned from the principal repository Git-Example.
5. "git pull" command
The pull command is used to access the changes (commits)from a remote repository to the local
repository. It updates the local branches with the remote-tracking branches. Remote tracking
branches are branches that have been set up to push and pull from the remote repository. Generally,
it is a collection of the fetch and merges command. First, it fetches the changes from remote and
combined them with the local repository.
Syntax:
In which:
<option>: Options are the commands; these commands are used as an additional option in a
particular command. Options can be -q (quiet), -v (verbose), -e(edit) and more.
<repository URL>: Repository URL is your remote repository's URL where you have stored your
original repositories like GitHub or any other git service. This URL looks like:
1. https://github.com/ImDwivedi1/GitExample2.git
To access this URL, go to your account on GitHub and select the repository you want to clone. After
that, click on the clone or download option from the repository menu. A new pop up window will
open, select clone with https option from available options. See the below screenshot:
Copy the highlighted URL. This URL is used to Clone the repository.
<Refspec>: A ref is referred to commit, for example, head (branches), tags, and remote branches. You
can check head, tags, and remote repository in .git/ref directory on your local
repository. Refspec specifies and updates the refs.
It is essential to understand how it works and how to use it. Let's take an example to understand how
it works and how to use it. Suppose I have added a new file say design2.css in my remote repository
of project GitExample2.
To create the file first, go to create a file option given on repository sub-functions. After that, select
the file name and edit the file as you want. Consider the below image.
Go to the bottom of the page, select a commit message and description of the file. Select whether
you want to create a new branch or commit it directly in the master branch. Consider the below
image:
Now, we have successfully committed the changes.
To pull these changes in your local repository, perform the git pull operation on your cloned
repository. There are many specific options available for pull command. Let's have a look at some of
its usage.
Syntax:
1. $ git pull
Output:
In the given output, the newly updated objects of the repository are fetched through the git pull
command. It is the default version of the git pull command. It will update the newly created
file design2.css file and related object in the local repository. See the below image.
As you can see in the above output, the design2.css file is added to the local repository. The git pull
command is equivalent to git fetch origin head and git merge head. The head is referred to as the
ref of the current branch.
Syntax:
Output:
Syntax:
Output:
In the above output, the complete repository has fetched from a remote URL.
GitHub: GitHub is a web-based Git repository hosting service, which offers all of the distributed
revision control and source code management (SCM) functionality of Git as well as adding its own
features.
Git is focused on version control and code GitHub is focused on centralized source
5.
sharing. code hosting.
Git has no user management feature. GitHub has a built-in user management
8.
feature.
Git competes with CVS, Azure DevOps Server, GitHub competes with GitLab, Bit Bucket,
12.
Subversion, Mercurial, etc. AWS Code Commit, etc.
Key Git Commands