Code Versioning With GIT

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Code Versioning with

www.newpharma-development.ro
Agenda Code versioning
What is GIT ?
GIT Operations
GIT Flavors
Demo

www.newpharma-development.ro
Code versioning

Version Control
• software practice for managing and tracking changes to software
code

Version Control System


• version control system (VCS) tracks changes to a file or set of files
over time

www.newpharma-development.ro
Code versioning

Local Version Control System

www.newpharma-development.ro
Code versioning

Centralized Version Control System

www.newpharma-development.ro
Code versioning

Distributed Version Control System

www.newpharma-development.ro
What is GIT ?

www.newpharma-development.ro
What is GIT ?

GIT
free and open-source distributed version control system
designed to handle everything from small to very large projects
with speed and efficiency.

www.newpharma-development.ro
www.newpharma-development.ro
Install GIT

• https://git-scm.com/downloads
• https://git-scm.com/downloads/guis

www.newpharma-development.ro
GIT Environment Setup

Git supports a command called git config that lets you get and
set configuration variables that control all facets of how Git
looks and operates.

git config --global user.name “Hello Devops"


git config --global user.email “devops@newpharma.ro”
git config --global core.editor "nano"

www.newpharma-development.ro
GIT Operations

www.newpharma-development.ro
GIT basics - Getting a git repository

You typically obtain a Git repository in one of two ways:

1. You can take a local directory that is currently not under


version control, and turn it into a Git repository
2. You can clone an existing Git repository from elsewhere

www.newpharma-development.ro
GIT Initialize a repository

git init directory – create a local repository


.git
• Hidden by default
• Local git data storage
• Information about project history

If you used git init to make a fresh repo, you'll have no


remote repo to push changes to.
git remote add <remote_name> <remote_repo_url>
git push -u <remote_name> <local_branch_name>

www.newpharma-development.ro
GIT – Get a remote repository

git clone URL - make a copy of a repository from an existing


URL

Two connection options:


• SSH – private/publick key
• HTTPS – user/password

Example:
git clone git@github.com:grafana/grafana.git
git clone https://github.com/grafana/grafana.git

www.newpharma-development.ro
GIT Basic Workflow

www.newpharma-development.ro
GIT – Working with Branches

In git a branch is a new /separated version of the main


repository.
A branch allow you to work on different parts of the project
without impacting the main branch.
You can even switch between branches and work on different
projects without them interfering with each other.
When the work is complete, a branch can be merged with
the main project.

www.newpharma-development.ro
GIT – Working with Branches

To create a new branch, you have to decide where to start


from.​
A branch can be created from another branch, a commit or a
tag
Ways to create a branch:
• git checkout –b <branchName>
• git checkout -b <branch_name> <commit_sha>
• git branch <branchName> / git checkout <branchName>

www.newpharma-development.ro
GIT – Working with Branches

You can merge a branch into another branch.


First, move to the branch you want to merge ON. Then
we tell git to merge branch into this branch.
• git checkout main - switch to the branch you want to merge
ON​
• git merge new_branch - merge branch new_branch OVER
branch main (that you are on)

www.newpharma-development.ro
GIT – Conflict

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.
The branch or commit you are on (checkout) is called „Yours”
or „Mine” or „Local”, or synonyms​
The branch or commit you are merging with (commits from it
come to your current branch) is called „Theirs” or „Server”, or
synonyms
NEVER resolve a conflict „using mine” or „using theirs”. You
are throwing away code that has a reason for being there​

www.newpharma-development.ro
GIT – Add and Commit

To have git track changes of a file, you first create the file
and then run `git add filename`.
To attach a history for that file in the branch that you are
on, locally, you use `git commit -m „Message for this
commit”`.
Each commit is a snapshot of the entire project.
When you add a file to a commit it is called „staged”.
When you commit a file we call it „committed”

www.newpharma-development.ro
GIT – Pushing and pulling

You do not send files to remote git.


You send history/snapshots.
To send ALL local commits to remote git you run `git push`.​
To get all changes from remote into your local git, you run
`git pull`.
You can not push until you pull from remote (if there is
anything there)​
A git pull is actually a `git fetch` followed by a `git merge`

www.newpharma-development.ro
GIT – Status

The status command is used to display the state of the


working directory and the staging area.
• ?? - Untracked files
• A - Files added to stage
• M - Modified files
• D - Deleted files

www.newpharma-development.ro
GIT – Diff

Git diff - lists out the changes between your current working
directory and your staging area

www.newpharma-development.ro
GIT – Log

You can see a log of all commits ever made on the branch
you are on​
To do this, run `git log`​
You’ll see ​
• the commit hash (unique)​
• Author​
• Date​
• Commit message

www.newpharma-development.ro
GIT – gitignore

Git sees every file in your working copy as one of three


things:
• tracked - a file which has been previously staged or committed;
• untracked - a file which has not been staged or committed; or
• ignored - a file which Git has been explicitly told to ignore.
Ignored files are tracked in a special file named .gitignore
that is checked in at the root of your repository.
If you want to ignore a file that you've committed in the past,
you'll need to delete the file from your repository and then
add a .gitignore rule for it.

www.newpharma-development.ro
Merge Request vs Pull Request

Merge or pull requests are created in a git management


application and ask an assigned person to merge two
branches.
Tools such as GitHub and Bitbucket choose the name pull
request since the first manual action would be to pull the
feature branch.
Tools such as GitLab choose the name merge request since
that is the final action that is requested of the assignee

www.newpharma-development.ro
GIT Flavors

www.newpharma-development.ro
Git Flavors

Git - A source code versioning system that lets you locally


track changes and push or pull changes from remote
resources.
GitLab, GitHub, Bitbucket - services that provides remote
access to Git repositories.

www.newpharma-development.ro
Resources

• https://learngitbranching.js.org/
• http://gitreal.codeschool.com/levels/1
• https://testautomationu.applitools.com/git-tutorial/
• https://education.github.com/git-cheat-sheet-
education.pdf

www.newpharma-development.ro
Homework
• Create a Github/Gitlab account
• Create a public repository for a new project called:
devopscademy
• Add the previous homework files to the main branch and push
them into the repository, with commit message "Homework
from module 2 and 3"
• Create a branch called develop from main branch, modify the
shell script from linux module to display your RAM on Linux,
commit the changes with message "Display available RAM" and
push the develop branch to the remote repository.
• Create a merge request/pull request between develop and main
branch
• Send the repository URL via email hello@newpharma-
development.ro

www.newpharma-development.ro
Thank you!

www.newpharma-development.ro

You might also like