Basic Git commands

every developer should know

Meghna Srivastava

Frontend Developer, AUTO1 Group

A Dentist turned Programmer

hellomeghna

The Problem

final draft

final draft 2

final final

final ACTUALLY FINAL

LAST FINALLLL

hellomeghna

The Problem

  • To remember what you wrote last time

  • Stop making your computer, a store-room!

  • Collaborate with others

hellomeghna

What is

Version Control System (VCS)?

  • Records all the changes in your file
  • Revert back to any version
  • Lets you do time travel ⏲️

hellomeghna

What is Git?

🤔

hellomeghna

Git is a distributed version-control system for tracking changes in your files.

Step 0: Create a GitHub Account

 

Step 1: Install `git` on the local machine

 

Step 2: Configure git

 

 

Step 3: Generate for your machine an SSH key

Getting Started: 

git config --global user.name "Meghna Srivastava"
git config --global user.email test@example.com

hellomeghna

What is a Repository/Repo? 

Let's Git 🚀

A repository a.k.a. repo is nothing but a collection of source code.

hellomeghna

Four Fundamental

elements in Git Workflow

✨ Working Directory

✨ Staging Area

✨ Local Repo (HEAD)

✨ Remote repo (MASTER)

hellomeghna

Working Directory

Local Repo (HEAD)

Staging

Remote Repo (MASTER)

git add

git pull

hellomeghna

git commit

git push

git fetch

git merge

git init

 

  • It can be used to convert an existing, un-versioned project to a git repository or initialize a new, empty repository.
  • Creates .git folder inside the repo

hellomeghna

git status

 

  • Tells the current state of the repository
git status
fatal: not a git repository (or any of the parent directories): .git

If a project/repo HAS NO git configuration, you get:

hellomeghna

git status
On branch master
Your branch is up to date with 'origin/master'.
 
nothing to commit, working tree clean

If a project/repo HAS git configuration, you get:

git status

 

  • Tells the current state of the repository

hellomeghna

git add

hellomeghna

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 . will add all the files your have edited to your staging index

hellomeghna

git commit

hellomeghna

git commit -m "message"

  • Saves the changes to repository as a new revision
  • git commit -am "message" adds and commits in same step  👩‍🎤
  • Keep commit messages short and descriptive
  • Use the imperative mood (this just means make them sound like commands, so instead of “fixed, fixing, or fixes” just say “fix” )

hellomeghna

- A SHA (Secure Hash Algorithm) value is generated every time you make a commit

- Also referred as a checksum or a hash value

SHA (Secure Hash Algorithm)

hellomeghna

- A long string of letters and numbers that is unique to each commit

git log

hellomeghna

git log

  • shows a history of commits and messages

git log

commit 6aa24bb96658ce2541f69f9eccfc3d65826c726b

Author: Meghna Srivastava<test@example.com>
Date:   Mon Nov 02 22:39:20 2019 -0300

    Added a line

 

commit c861cb6c8d0f3c78b24004cfe23df55934cd3ca4
Author: Raj Saxena <anothertest@example.com>
Date:   Mon Nov 02 18:20:20 2019 -0300

    Created file

 

hellomeghna

git diff

  • Different from git status which just shows  the names of the unstaged files.

hellomeghna

  • Shows all the unstaged changes 
  • See all the changes to the files in staging index
    • git diff --staged 
  • See the difference between any two commits

    • git diff [commit-from]..[commit-to] 

  • See what was new in a commit by running:

         git show <commit>

$ git show b10cc123

commit b10cc1238e355c02a044ef9f9860811ff605c9b4
Author: Tutorialzine
Date:   Mon May 30 16:30:04 2016 +0300

    Added content to hello.txt

diff --git a/hello.txt b/hello.txt
index e69de29..b546a21 100644
--- a/hello.txt
+++ b/hello.txt
@@ -0,0 +1 @@
+Nice weather today, isn't it?

git show <commit>

hellomeghna

git remote

hellomeghna

git remote

git remote add myFirstRepo https://github.com/user/repo.git

hellomeghna

  • Adds a new remote
  • git remote add <name> <url> command takes two arguments:

  1. A remote name, for example, “origin”

  2. A remote URL​  

 

  • git remote -v verify new remote

git push

hellomeghna

git push

  • sends committed changes to the remote repository
  • more explicitly, could write git push origin master
const person = {
  name: 'Meghna',
  age: 28,
  sex: 'female'
}
const person = {
  name: 'Meghna',
  age: 28,
  sex: 'female',
  occupation: 'Developer'
}

Added one property

Local repo

const person = {
  name: 'Meghna',
  age: 28,
  sex: 'female'
}

Server/Remote

git push

const person = {
  name: 'Meghna',
  age: 28,
  sex: 'female',
  occupation: 'Developer'
}

hellomeghna

git pull

hellomeghna

git pull

  • retrieves changes from remote repository
const person = {
  name: 'Meghna',
  age: 28,
  sex: 'female'
}
const person = {
  name: 'Meghna',
  age: 28,
  sex: 'female',
  occupation: 'Developer'
}

Local repo

const person = {
  name: 'Meghna',
  age: 28,
  sex: 'female'
}

Server/Remote

const person = {
  name: 'Meghna',
  age: 28,
  sex: 'female',
  occupation: 'Developer'
}
const person = {
  name: 'Meghna',
  age: 28,
  occupation: 'Developer'
}

git pull

const person = {
  name: 'Meghna',
  age: 28,
  occupation: 'Developer'
}

hellomeghna

git reset

hellomeghna

git reset

const one = 1;
const two = 2;
const one = 1;
const two = 2;
const three = 3;

const two = 2;
const threee = 3;

add line ed56f70m

remove line 5fh67bc8

git reset --hard ed56f70m

hellomeghna

  • resets back to  a previous commit in your working directory

git reset --hard <commit>

git reset

const one = 1;
const two = 2;
const one = 1;
const two = 2;
const three = 3;

add line ed56f70m

git reset --hard ed56f70m

hellomeghna

  • git revert is a tool for undoing committed changes, while git reset HEAD is for undoing uncommitted changes.

git clone

hellomeghna

git clone <url>

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

git clone <url>

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

hellomeghna

git branch

hellomeghna

const one = 1;
const two = 2;
const three = 3;
const one = 1;
const three = 3;
const one = 1;
const three = 3;
const four = 4;
const one = 1;
const two = 2;

master branch

hellomeghna

const one = 1;
const two = 2;
const three = 3;
const one = 1;
const two = 2;
const three = 3;

const name = 'Meggie';
const one = 1;
const two = 2;

master branch

const one = 1;
const two = 2;
const three = 3;
const four = 4;

const two = 2;
const three = 3;
const four = 4;

feature branch

bug-fix branch

hellomeghna

Branch

hellomeghna

  • git checkout -b <branch-name> - create a new branch and switch to that new branch
  • git branch -m <new-branch-name> - rename current branch
  • git branch - shows all branches
  • git branch <branch_name> - creates a new git branch
  • git checkout <branch_name> - switch to or ("checkout") to a new branch

git merge

hellomeghna

git merge

  • Join two or more development histories together
  • git merge <branch_name>

       merges feature branch to current  branch


const two = 2;
const three = 3;

const two = 2;
const three = 3;
const four = 4;
const one = 1;
const two = 2;
const three = 3;
const four = 4;

const two = 2;

master branch

const one = 1;
const two = 2;
const three = 3;

feature branch

hellomeghna

hellomeghna

Merge Conflicts

  • When two different commits can't be automatically merged
  • Need to be resolved
const a = 1;
<<<<<<< HEAD
const b = 2;
=======
const b = 0;
>>>>>>> c861cb6c8d0f3c78b2
const c = 3;
const d = 4;
const e = 5;

git pull

{

 

your changes

{

remote changes

conflicting commit

hellomeghna

Merge Conflicts

  • When two different commits can't be automatically merged
  • Need to be resolved
const a = 1;
const b = 2;
const c = 3;
const d = 4;
const e = 5;

git pull

{

 

your changes

{

remote changes

hellomeghna

Git Clients

hellomeghna

Where are the cool things?

hellomeghna

  • Similar to other shells, such as as bash and ksh
  • The Z shell known as zsh is a shell for Linux/Unix-like operating systems.
  • Advanced features and powerful command line options

hellomeghna

$ alias | grep -i git   

lists all the git commands

ga='git add'

gaa='git add --all'

gb='git branch'

gbD='git branch -D'

gbr='git branch --remote'

gcm='git checkout master'

gcmsg='git commit -m'

gco='git checkout'

...

hellomeghna

 

🎉

git commit -m

"The End"

hellomeghna

hellomeghna

  🎯 Atlassian git tutorial - link

  🎯 Git for beginners - link 

  🎯 Learn the basics of git in under 10 minutes - link

  🎯 Learn git in 30 minutes - link

Made with Slides.com