Sitemap

Version Control with Git

7 min readMar 18, 2023

--

Git is a free version control system that can be downloaded from this web site: https://git-scm.com/. It keeps the versions of the developed projects as a copy in a repository and provides the user with various facilities such as adjustments and upgrades on the project.

Press enter or click to view image in full size
Version Control with Git

What is Commit:

Permanently committing the files we add to Git to the Git database is called commit. It is necessary to commit files after certain operations are done in the project.

What is Branch:

In the most basic terms, they can be defined as branches of the project. When you create a project in Git, a default branch is created. If we continue to work without making any changes, we will do all our operations through that branch. Moving through the main branch is not a recommended way of working.

Sample git version flows:

Press enter or click to view image in full size
Gitlab branch flow
Press enter or click to view image in full size
Version release branch flow

Git Codes

Commonly used git codes are as in the table below:

Commonly git codes list
Press enter or click to view image in full size
Local and Remote Areas

Git can be learned easily by practicing on this website. https://learngitbranching.js.org/

Git Terminal Example

What is git bash

It is a terminal emulator for users who want to use the Git command line tool for Windows operating systems. It provides a Unix-style terminal interface for using git commands. It is often preferred by windows users to run git commands. We will also perform the following settings via the git bash terminal interface.

Press enter or click to view image in full size
Git bash interface

Git config settings

We log in to our remote repo with local git configuration settings, so we can clone our private projects from our repo or send our changes on the local device to our repo at the remote server address. Our e-mail address and password are important for us to access the remote server.

git config --global user.name "samet"
git config --global user.email "examle@mail.com"
git config --global user.password "**********"

Creating a project on a local device

We go to our target directory and write the bash terminal “git init” and we ensure that our git project is created on the local device.

git init

We use the “git status” code to see the changes we have made to the directory, and we see that we have uncommitted content named example.py on my directory.

git status
Press enter or click to view image in full size
No staging area

Then we write the code “git add example.py” to import this change into the staging area.

git add example.py

When we rewrite the “git status” code, we see that the changes we made are saved in the staging area. Then we commit these changes with the code “git commit -m “init” ”.

git commit -m "init"
Press enter or click to view image in full size
On staging area

If we do not want to send our changes to our repo via the master branch, we need to create a branch with a different name. At this point, we create a branch named “tree” by typing “git branch tree”.

git branch tree
Press enter or click to view image in full size
A new branch named tree

We write the “git checkout tree” code to switch to this new branch we created. And after that, we confirm this action by typing “git branch”.

git checkout tree
git branch
Press enter or click to view image in full size
On the tree branch

Get Samet Burhan’s stories in your inbox

Join Medium for free to get updates from this writer.

When we add another change to our example.py file, git will detect the changes made on the project again.

git status
git add .
git commit -m "second commit"
Press enter or click to view image in full size
Changes detected

When we process these changes in the staging area, we can observe our commit transactions and their special codes with the “git log –oneline” code.

git log –oneline
Press enter or click to view image in full size

We can return our project to the relevant commit point through these codes. At this stage, in order to return to the “23ad73a” point, which is the “init” commit point, we return to that point by typing the “git revert 23ad73a” code into the terminal, so we undo the changes made after the “init” commit point.

git revert 23ad73a
Press enter or click to view image in full size
Git revert

After this process, when we check again with the “git status” code, we see that there is a change that has not been committed.

Press enter or click to view image in full size

After this process, when we check again with the “git status” code, we see that there is a change that has not been committed. After committing this change, we can observe all our commits with the “git log –oneline” code.

Press enter or click to view image in full size

The last operation required to commit all these changes on the remote machine is the push operation. In this example we will work on a github repository, but it is possible to perform similar operations on other git-supporting storage services. After obtaining the address to be sent from the site, we can save our changes to our repo by performing the code steps below.

With this code, we assign the address of our remote device to the “origin” keyword, so that we can easily get rid of long steps such as link copying by going to the site in every update we make.

git remote add origin https://github.com/sametburhan/example.git

At this point, we proceed by typing “git push origin tree” since the branch we will send is named tree, but in cases where the target branch is different, we need to change the branch name. And as a result, we have sent our project to our warehouse on our remote device.

git push origin tree

Git revert and Git reset

Let’s explain the difference between the often confused revert codes Git revert and Git reset through an example. In git revert code: think about a project that you started in previous years but did not finish, although many years have passed, we can continue by returning to that project, and this return does not require us to delete our new projects that we have realized after our old project and does not affect them. But this situation is slightly different in Git reset code. If we need to go through the same example, when we go back to our old project, we can think that we deleted the projects we have done later. This is how we can customize the structure that separates these two different commands.

Press enter or click to view image in full size
Git revert and git reset

Pull a project from a repository

To copy the project from our repo to the local device, it is sufficient to go to the target directory to be cloned and write the following code “git clone https://github.com/sametburhan/example.git” in the terminal. It is possible to clone any desired repo to local devices without logging in with git, provided that the target repo is not private.

git clone https://github.com/sametburhan/example.git

What is git merge

In a project consisting of two or more branches, it is possible to combine the changes made in different branches into a single main branch. We mentioned above that there should be different branches in a software development project. In these branches, the project is divided into parts and assigned to different teams as a task. These parts developed in different branches should be combined in a version as a product at the targeted stage. This merge event is called merge. It makes it easy to follow and progress the project without causing confusion for version controls. Merge detects changes between branches and merges modified files, making conflicting changes easy to resolve.

git merge example
Git merge

What is git stash

Git stash is a command line tool. With this tool, changes that have not yet been completed or tested can be temporarily stored and then restored back to a state with no changes.

Git stash codes

What is submodule

In short, it is the event of creating subprojects inside an active project on git. In large-scale projects, it can be used to work as modules by dividing the project into parts. Sub-repos can be created into the working repo by means of the code “git submodule add https://github.com/sametburhan/example.git”. In this way, when the main repo is cloned, the sub-repos will be cloned and added to the project.

git submodule add https://github.com/sametburhan/example.git
Press enter or click to view image in full size
Structure of submodules

However, in some cases, the submodules we have created are not visible in the local device directory even though we call the main project, in such cases, we can easily update the submodules by entering the following codes in the terminal.

git submodule init
git submodule update –remote

A submodule can also be merged and updated using the command “git submodule update — remote — merge”. This command updates the submodule to include the latest changes and also merges the changes from the top level git repository during the update process.

git submodule update --remote --merge

--

--