GIT 101

Introduction to Git and Git commands

Hi.

Sanjeev Yadav

Trainee Software Engineer

Spirit Animal

What is Git?

keep track of your changes

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

Create a file

(v1)

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

Add a line

(v2)

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

Remove a line

(v3)

Synchronizes code between different people


int b = 2;
int c = 3;

int b = 2;
int c = 3;

int b = 2;
int c = 3;
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;
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int a = 1;
int b = 2;
int c = 3;
int d = 4;

Test changes to code without losing the original

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

Revert back to old versions of code

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

Create a file

(v1)

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

Add a line

(v2)

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

Remove a line

(v3)

git status

git status

  • displays the state of the working directory and the staging area
git init
fatal: not a git repository (or any of the parent directories): .git
git init
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 init

git init

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

git add

  • Modified
  • Staged
  • Committed

File stage

git add <file_name>

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

git add foo.c

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

changes to be committed:

int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
modified: foo.c

Demo

git commit

git commit -m "message"

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

git commit -m

"add line"

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

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 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 -v show remote url after name
  • git remote add <name> <url> Adds a remote named <name> for the repository at <url>

git remote add github

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

git push

git push

  • sends committed changes to remote repository
  • more explicitly, could write git push github 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

Demo

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;

Demo

git reset

git reset

  • git reset --hard <commit> reverts code back to  a previous commit
  • git reset --hard github/master reverts code back to remote repository version

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 clone

git clone <url>

int a = 1;
int b = 2;
int c = 3;
int d = 4;
  • makes a copy of the repo
  • stores it into your computer
  • a "fork" creates your own copy of someone else's repository

git clone <url>

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

Demo

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 :(

featureA branch

featurB 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"

git stash

Reference

Video this slide was plagiarised from

git add ask_question.txt

git commit -m

"The End"

GIT 101

By Sanjeev Yadav

GIT 101

Basic git commands

  • 223