GİT USAGE

1.Why is Git needed?

When we want to make changes on the file (such as Word, excel, python or powerpoint file …) while working on a file, but want to keep the state before making changes, we save the file with different names (myfile1.doc, myfile2.doc, myfile3.doc etc…). However, this situation causes too many files to accumulate on our computer. Version Control Systems are used to prevent this situation.

In addition, Version Control Systems allow different people to work on the same file.

2. What is Git?

Git is a Version Control System software. Also There are different Version Control System softwares.

  • SVN (Subversion),
  • CVS (Concurrent Versions System) are version control system softwares.

3. What is GitHub?

GitHub is a web application or web storage service integrated with software such as git, where we can store our projects on the internet. There are different web applications like GitHub. These are;

  • GitLab
  • Bitbucket

4. How to install Git?

5. Git Usage

We can store our projects with all versions in two places. Local(our computer) , Remote(web storing service like github, gitlab…) or both local and remote’ at same time. Generally we store projects in both paces to avoid data loss.

Two topics are very important in Git ‘s workflow. The first of these is three”threes” managed in the local repository by git, the second is the branches;

5.1. Git Sections

Git lifecycle;  local repo is consists of three “trees” managed by git. First one is working directory that holds actual files. Second is  Stage(index) and last is HEAD which point last commit.

Şekil -1 git sections

In order to save the final version of the changes we made in our project to the git repo in the local, firstly, it is necessary to save it to the intermediate layer called staging area (also called index.) And then carry it from stage to git repo.

You can create git repo in two ways in local.
A local repo can be created with.

git init

or we can download the remote rep repo to our local with:

git clone https://gitlab.com/etem1/trial.git 

Şekil -2 git trees

The final version of the project is sent to the repo created for our project in the local.

To save the final version of the project to the repo created for our project in local as follows:

git add .  

adds a change in the working directory to the staging area. Then

git commit –m “short explanation of changes” 

saved to local repo.

5.2. Git Branches

More than one person can work on a project or we may want to do a code review in the project we will work on. Git manages this for us.

When the Git project is started in Local, a branch called master is created.

git branch  

with the command, we can check which branch we are in. we can switch to the branch we want by

git checkout <branch_name> 

We can create lots of branches with different names (such as developer, admin panel branch ..)

In local, we can create a branch and switch to that branch as follows:

git checkout -b <branch_adi>  

6. Git Configuration 

For the remote repo that git will use, we can configure our accounts like github or gitlab locally as follows:

git config --global user.name “trial”
git config --global user.email trial@gmail.com

7. Pull Remote Server to Local and Merge

We can pull the latest changes (commit) to our local repository as follows:

git pull origin master  

master is the name of the branch.

We can merge branches as follows:

git merge <branch_name>  

8. Git other Commands

gitk

With the internal git GUI (Graphical User Interface) we can print out our git commits in color. Graphically, we can also see past commits.

9. Create Version

If we are using git for our software projects, we can create a version before publishing the software.

git tag 1.0.0 c325331a301a30e293b4851d8aa1bd6e9bd00049

c325331a301a30d293b4851d8aa1bd6e9bd00049 

is the process number of our software version to be released.

git log

We can see the transaction number of our last commit.

Frequently Used Git Commands

git init                        	     :    to start our project in local.
git add .                       	     :    to submit all files at working directory to staging area.
git commit -m "message"                  :    save changes with message to git repo.
git log                         	     :    to list all versions.
git branch                             :    to show current branch.
git status		                         :    to show changes info. 
git pull origin master	               :    remote' ta master brancindeki projeyi locale çekmek için kullanılır.
git push origin master	               :    to push changes to remote server.
git -- checkout <file_name>             :   to go back before save to git repo. 
git reset HEAD <file_name>              :    to reset changes at stage area
git checkout <commit_ID>               :   to go to the desired version.

========== to make git configuration  ==========
git config --global user.name "etem"
git config --global user.email etemkeskin@gmail.com

======= create  branch both local and remote ========
to create new branch locally with same name at remote server:
1) git checkout -b <branch_name>
2) We submit changes to stage area and then commit them.
Changes are pushed to remote..:
3) git push -u origin <branch_name>
Sources

https://www.etemkeskin.com

https://git-scm.com

https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet