How to use Git

Introduction to Git and Git commands

Hi.

Sanjeev Yadav

Software Engineer @mindfires

Version Control System(VCS)

  • Record changes to your file

  • Revert back to any version

  • Basically lets you do Time Travel

What is GIT?

๐Ÿค”

๐Ÿ—‚๏ธ Repository ๐Ÿ—‚๏ธ

a.k.a.

repo

Four Fundamental

elements in Git Workflow

๐Ÿ’จ Wind

๐Ÿ”ฅ Fire

๐ŸŒŽ Earth

๐ŸŒŠ Water

๐Ÿ™…โ€โ™‚๏ธ

Four Fundamental

elements in Git Workflow

โœจ Working Directory

โœจ Staging Area

โœจ Local Repo (HEAD)

โœจ Remote repo (MASTER)

Working

Directory

Staging

Area

Local Repo

(HEAD)

Remote Repo

(MASTER)

Git Add

Git Commit

Git Push

Git Fetch

Git Merge

Git Pull

git init

git init

  • Create an empty Git repository
  • Creates .git folder inside the repo

git status

git status

Current State of the repository

git status
fatal: not a git repository (or any of the parent directories): .git
git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

on a non-git repo

on a git repo

git add

git add <file_name>

  • adds a file to "staging area"
  • tells git to include the file in the next revision to the repository

git commit

git commit -m "message"

  • saves the changes to repository as a new revision
  • git commit -am "message" adds and commits in same step

git log

git log

  • shows a history of commits and messages
git log

commit 6aa24bb96658ce2541f69f9eccfc3d65826c726b

Author: Sanjeev Yadav<sanjeev@gmail.com>
Date: ย  Mon Apr 22 22:39:20 2019 -0300

ย  ย  Added a line

ย 

commit c861cb6c8d0f3c78b24004cfe23df55934cd3ca4
Author: Robin Thicc <thicc@gmail.com>
Date: ย  Mon Apr 8 18:20:20 2019 -0300

ย  ย  Created file

ย 

Demo

git remote

git remote

  • Manage set of tracked repositories
  • git remote add <name> <url> Adds a remote named <name> for the repository at <url>

git remote add myrepo

https://github.com/user/repo.git

git push

git push

  • sends committed changes to remote repository
  • more explicitly, could write git push myrepo master
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
git push
int a = 1;
int b = 2;
int c = 3;
int d = 4;

add line

int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;

server

local

git pull

git pull

  • retrieves changes from remote repository
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
git pull
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;

server

local

int a = 1;
int c = 3;
int d = 4;
int e = 5;
int a = 1;
int c = 3;
int d = 4;
int e = 5;

git clone

git clone <url>

int a = 1;
int b = 2;
int c = 3;
int d = 4;
  • downloads a copy of the repo in your computer

git clone <url>

int a = 1;
int b = 2;
int c = 3;
int d = 4;

Demo

git reset

git reset

  • git reset --hard <commit> reverts code back toย  a previous commit

git reset --hard

edfe30cc

int a = 1;
int b = 2;
int c = 3;
int d = 4;
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
add line

edfe30cc

int b = 2;
int c = 3;
int d = 4;
int e = 5;
remove line

51ej004

git branch

int a = 1;
int b = 2;
int a = 1;
int b = 2;
int c = 3;
int a = 1;
int c = 3;
int a = 1;
int c = 3;
int d = 4;

master branch

int a = 1;
int b = 2;
int a = 1;
int b = 2;
int c = 3;

int b = 2;
int c = 3;
int d = 4;
int a = 1;
int b = 2;
int c = 3;
int d = 4;

master branch

int a = 1;
int b = 2;
int c = 3;
String sanjeev = "handsome";
int a = 1;
int b = 2;
int c = 3;
// No you are not :(

feature branch

bugfix branch

git branch

  • shows all branches of code
  • git branch <branch_name> creates new git branch
  • git checkout <branch_name> switch to or ("checkout") to new branch

git branch

feature

int a = 1;
int b = 2;
int c = 3;
int a = 1;
int b = 2;
int c = 3;
int d = 4;
master branch
int a = 1;
int b = 2;
int c = 3;
int d = 4;
feature branch

git merge

git merge

  • Join two or more development histories together
  • git merge <branch_name> merges feature branch to current branch

int b = 2;

int b = 2;
int c = 3;

int b = 2;
int c = 3;
int d = 4;
int a = 1;
int b = 2;
int c = 3;
int a = 1;
int b = 2;
int c = 3;
int d = 4;

master branch

feature branch

Demo

Merge Conflicts

Merge Conflicts

  • when two different commits can't be automatically merged
  • need to be restored

git pull

int a = 1;
<<<<<<< HEAD
int b = 2;
=======
int b = 0;
>>>>>>> c861cb6c8d0f3c78b2
int c = 3;
int d = 4;
int e = 5;

{

your changes

{

remote changes

conflicting commit

int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;

git merge

  • git merge --abort ย can only be run after the merge has resulted in conflicts
  • git merge --continue ย can only be run after the merge has resulted in conflicts

Demo

git commit -m "I know git"

Reference

๐Ÿ“นย  An Introduction to Git and GitHub by Brian Yu - link

๐Ÿ“„ย  Learn the Basics of Git in Under 10 Minutes - link

๐Ÿ“„ ย  Learn Git in 30 Minutes - link

๐Ÿ’ปย  Visualise git - link

๐Ÿ”ฅย  Learn git in 14 Tweets by Chris Archard - link

git add ask_question.txt

FeedBack

git commit -m

"The End"

Made with Slides.com