Use Git

I will familiarize you with the archive of git commands in this article. Today, the number of open source software is widespread in the IT world. git is a version control software that also falls into the category of OpenSource software. The main task of this software is to manage the source code as well as to review the distributed source code. The main focus of this software is on tracking the changes related to computer files and the work that has been done on them.

What is git?

As explained in the Git introduction section, git is one of the version control software that also fits into the open source software category. The main task of this software is to manage the source code as well as to review the distributed source code. The software is mostly used in software projects, but you can also use it to check for changes to a set of files.

How to install git?

git and GitHub are different from each other and you should not mistakenly consider these 2 as a single software. To install git, you must go to the https://git-scm.com site and follow the installation process according to the help inside the site. To install GitHub also from the site https://desktop.github.com,download and install the GitHub software. Once installed, you can easily manage the repositors you produce.

Usually in projects, gitlab is used more and in this article we mention more of these commands, but you should know that practically the orders of Gitlab and GitHub are not very different from each other.

Build SSH key for GitLab

If you want to access projects, you need to create an SSH key in your profile. With this key, you will be able to manage your own project at GitLab. To create this key, first log in to your profile on GitLab. In your profile, a Generate link is displayed in order to build a dedicated key on the gitlab. If you click on this link, the key will be made for you, which you can place in the box specified in your profile.

 

To activate this code, you must open the terminal according to the desired path and enter the code shown below:

ssh-keygen -t rsa -C "yourname@yourdomain.ext"
Gi

We place the built-in code in the section specified in the image above and copied according to the following command:

pbcopy < ~/.ssh/id_rsa.pub

GitLab

The commands in git are as follows:

The following is the command to build a fresh repository on your computer:

git init [project name]

This command is to download from a repository (url) available on your system:

git clone MY_URL

For new files that have not yet been commited, as well as the list of changes made, the following command is used:

git status

This is the command to add all fresh files to the stage and prepare it for commit:

git add .

This is the command to add your desired file to the stage and prepare for commit:

git add [file name]

This command is to commest all files that are available in the stage:

git commit -m "Description "

With the following command, we add all files simultaneously, and we also specify an explanation for the commit section:

git commit am "Commit Description "

This command is for uploading as well as sending new files to the server and following the branch we are in:

git push

This command is for uploading as well as sending new files to the server and under the supervision of the master branch. But normally this is not done. So preferably we push on another branch and then merge.

git push origin master
git pull 

The following command is to get the changes that have been made recently from the server and merge:

Note: When you are working on a project in the form of a team (e.g. on a branch called example, and the changes that have recently been made to the project are on the test branch of the project called dev branch), in this case you do not need to send the ricochet merge and you will be able to get the latest changes from the dev branch.

git pull origin dev

By this command, you can specify which branch you are in and which branch your codes will be changed or received in:

git branch

This is the command to get the latest changes from the branch origin (no merge):

(If we are in a Branch and want to change the branch, if we have built the branch recently, we need to enter the following code in order to identify the branch and change it. )

git fetch

or 

git fetch --all

This is the command to display the list of orders:

git branch --all

or

git branch

This is the command to change the branch to something else, for example, to the Develop directory:

git checkout Develop

This command is to create a new Branch called New_Branch:

git branch New_Branch

This command is to display the list of all Branch as well as Local and Remote:

git branch -av

This is the command to remove My_Branch from the list of all Branch:

git branch -d My_Branch

This is the command for Merge off the Branch_B inside the Branch_A:

git checkout Branch_A
git merge Branch_B 

This command is to display changes to files that have not yet been entered into the stage:

git diff

This command is to display changes to files that have been imported into the stage:

git diff --cached

This command is to display all changes to the files that have been imported to the stage, as well as files that have not yet entered the stage (Unstage files).

git diff HEAD

This command shows the difference between the two commits:

git diff commit1 commit2

By this command, you can see the list of all changes to the file you want, along with the date and name of the Doloprey that made these changes:

git blame [File Name]

This is the command to display the list of all file changes:

git log

This is the command to show the history of all changes along with their tag information:

git log --decorator

By this command, the file/directory file change list history is displayed along with their list of changes (diff) during commits:

git log -p [file/directory]

The following is the command to unstage the file along with applying the new changes:

git reset [file]

This command has been performed to revert or restore all changes from the last commit:

git reset --hard

This is the command to show the list of available tags:

git tag

This command is to add tags to the latest files as well as changes made:

git push --delete origin TagName

This is the command to remove the tag from the repository:

git push --tags

This command is used to remove the tag generated on the local:

git tag -d TagName

By this command, all tags created on the dipazitori are sent:

git push --tags

This is the command to add tags along with their descriptions. After using this command, a new page opens and you can enter a new tag description in it:

git tag -a TagName

This command is used to create the tag along with its description:

git tag -am "Description" TagName

This is the command to show the list of tags, along with descriptions of each tag:

git tag -n

Experience:

Sometimes we may not push them after applying the changes and committing them, in this case we may need to go back to the size of a commit, in which case we use this command:

git reset HEAD~1

Tips:

If we want to clone a project from GitLab and launch it on our computer, we can use this command:

git clone git@gitlab.com:user/project-name.git

After running this command, a folder called project-name that you already created on git will be created on your computer. For example, if we want to pull our project onto the server and need to clone the project before then, we may not want the project name to be the same on the gate and server, and its contents will be dumped in a folder with the desired name that we want. In this case, we use the following command:

git clone [repository] [local path]

or 

git clone git@gitlab.com:user/project-name.git example

We can also log in to the desired folder and do so by this command:

git clone git@gitlab.com:user/project-name.git

Putting the dot mark at the end of the command is very important. If we don’t write it, our project will be clone with the name of the repository.

Experience #1:

In one of our projects, a file called composer.json was accidentally modified on the server by one of the group. We weren’t allowed when we wanted to pull the latest changes onto the server. The reason for this was that the task of the changes made to the server should have been specified first.

As you can see in the image, it is not possible to pull the latest changes until the file has been modified. We can use the following command to restore the file to the previous state:

git checkout -- filename

Experience #2:

Use the following command when you want to copy a branch in full and place it on another branch:

git checkout -b new_branch old_branch

Return the project to the desired commit

As explained above, by reseting the git command, you can return the project to the commit you want, but in this case you must return to this commit by the following command as well as having the ID related to the commit you want.

git reset COMMIT_ID

After running this command, all additional files created after committing will remain and you can keep or delete them. But if you want to delete all changes and added files and only go back to the previous commit, you need to use the following command:

git reset --hard COMMIT_ID

Loading project on GitLab

After creating the project on GitLab, you can create this project on your own local by cloneing. You might be wondering how we should do this if the project folder has already been created, and we want to send it on git?

After creating the project on GitLab, the following image containing the right tips will be displayed to you:

As you can see in these tips in the picture, one of these two things we need to do. Either you need to clone the project or if the project folder already exists and you want to put it on the gitlab, run the following codes in order:

cd existing_folder
git init
git remote add origin git@gitlab.com:xxxxxxxxxxx
git add .
git commit -m "Initial commit"
git push -u origin master