Git and Github Session 2021

Web Portfolio?

How do you create a project, and how would you manage changes?

Version Control System

Version Control System

Version Control System

  • Version control is a management software responsible for managing changes to computer programs, documents, large websites, or other collections of information.
  • Easy recovery of files/folders.
  • Rollback to the previous versions.

Version Control System

  • Version control is a management software responsible for managing changes to computer programs, documents, large websites, or other collections of information.
  • Easy recovery of files/folders.
  • Rollback to the previous versions.
  • Informs us about Who, What, When, Why changes have been made.

History of VCS

Local VCS

  • Changes are stored in a database, along with timestamp.
  • Code is in Local System. 

Local VCS

  • Changes are stored in a database, along with timestamp.
  • Code is in Local System. 

Cons :

         Project can be lost, if your hard-disk is corrupted.

Centralized VCS

Repository

A directory where your project resides. It can be a local folder in your computer or a remote directory on a server.

There are 2 types of reposity :

 1. Local Repository

 2. Central Repository

Centralized VCS contain just one repository i.e central repository and each user gets to work on the same.

Centralized VCS contain just one repository i.e central repository and each user gets to work on the same.

Cons :

  1. If the central repo goes down even for an hour.
  2. You'll lose the project if the hard disk of the            central server goes down.

Distributed VCS

  • Distributed version control systems contain multiple repositories. Each user has their own local repository & there is a central repository where the final code resides.

Distributed VCS

  • Distributed version control systems contain multiple repositories. Each user has their own local repository & there is a central repository where the final code resides.  
  • Provides full Back-up of the project.

Distributed VCS

  • Distributed version control systems contain multiple repositories. Each user has their own local repository & there is a central repository where the final code resides.  
  • Provides full Back-up of the project.
  • Git is an example of distributed VCS.

How is Git created?

Git was created in 2005 by Linus Torvalds.  (creator of Linux kernel)

?

  1. Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

 

  2. Git stores snapshot of your project (not differences)

Snapshots Over Time

Why               ?

  • Collaboration
  • Storing Versions
  • Analyse the code changes

Features

  • Distributed.

Features

  • Distributed.
  • Almost everything is Locally.

Features

  • Distributed.
  • Almost everything is Locally.
  • Non-linear/Branching.

Features

  • Distributed.
  • Almost everything is Locally.
  • Non-linear/Branching.
  • Secure/Integrity.

Features

  • Distributed.
  • Almost everything is Locally.
  • Non-linear/Branching.
  • Secure/Integrity.
  • Speed.

Setting Up the Environment

# to check the git version
git --version
# to check the git version
git --version


#configuration of global variables
git config --global user.name "Name"
git config --global user.email "username2010@gmail.com"
# to check the git version
git --version


#configuration of global variables
git config --global user.name "Name"
git config --global user.email "username2010@gmail.com"


git config --list

Command Line

Tool

Introduction

Types of Git Commands

Creating Repository

Types of Git Commands

Creating Repository

Making Changes

Types of Git Commands

Creating Repository

Making Changes

Parallel Development

Types of Git Commands

Creating Repository

Making Changes

Parallel Development

Syncing Repository

Three Stage Architecutre

Let's create a git repository

git init

This will create a hidden .git folder.

That's where git stores everything.

git status

To track files & folders.

The mysterious .git

Tracking Your

Project

Tracking your files

Each file in your working directory can be in one of two states: tracked or untracked.

 

 

- Tracked files are files that Git knows about.

-  Any file in your working directory that was not in your last snapshot and is not in your staging area is Untracked file

# for one specific file
git add <filename>


# for all files & folders
git add . 

Tracking New files :

Stages the latest version of the file.

Lifecycle of the status of a file

Commiting Changes

Moving files from staged area to the git folder is called a commit.

 

Making Commits, creates version/snapshots of your project.

Commiting Changes

git commit -m "Your commit message"

Viewing the Commit History

You can see all the commits/version of your git project.

git log

Viewing the Commit History

You can see all the commits/version of your git project.

git log


git log --oneline

Deleting a Git Project

rm -rf .git

Additional

Git Commands

Skipping the staging area.

git commit -a -m "Commit Message"

Git Diff

if you want to know exactly what you changed, not just which files were changed — you can use the

 command

git diff

Git Diff

if you want to know exactly what you changed, not just which files were changed — you can use the

 command

to see what you’ve staged that will go into your next commit

git diff
git diff --staged 

Git Ignore

  • Often times, we do not want git to keep track of some specific autogenerated files like logs, or build files .DS_Store etc.

 

  • To avoid them, you can list files or a pattern for files in .gitignore file.

 

Moving & Removing Files

These command stage the changes automatically. You don't have to stage explicitly.

git rm filename.txt
git mv filename.txt newfilename.txt

Unstaging & Unmodifying Files

git restore --staged filename.txt

Unstaging & Unmodifying Files

git restore --staged filename.txt

This command takes your file to the last version/commit.

git checkout --filename.txt

 

Note:

This can be a dangerous command, as it discards all your current changes in the working directory

git checkout -f

(Rollback to the previous commit. losing all newly modified files.)

Setting Alias

git config --global alias.st status

Usuage:

git st

Setting Alias

git config --global alias.st status
git config --global alias.unstage 'restore --staged --'

Usuage:

Usuage:

git st
git unstage file.txt

Non-Linear Development

Branching

What is Branching?

  • A branch in Git is simply a lightweight movable pointer to one of the commits.
  • The default branch name in Git is master
  • Branching : Diverging from the main branch.

Why Branching is required?

98ca9

Commit 1

34ac2

Commit 2

f30ab

Commit 3

master

v1.0

HEAD

Creating new branches

Creating new branches

git branch

git branch -v

Get all the branches in your project.

Creating new branches

git branch branchname
git branch

git branch -v

Get all the branches in your project.

Creating a new branch.

98ca9

34ac2

f30ab

master

HEAD

develop

Moving between branches

git checkout branchname

Checkout an existing branch.

Moving between branches

git checkout branchname

Checkout an existing branch.

git checkout -b  branchname

Creating and Checkout to a new branch.

98ca9

34ac2

f30ab

master

develop

HEAD

98ca9

34ac2

f30ab

master

develop

HEAD

98ca9

34ac2

f30ab

master

develop

HEAD

Making a commit on Branch

98ca9

34ac2

f30ab

master

87ab2

develop

HEAD

Making a commit on Branch

98ca9

34ac2

f30ab

master

87ab2

develop

HEAD

Making a commit on Branch

98ca9

34ac2

f30ab

master

87ab2

develop

HEAD

Making a commit on Branch

c2b9e

Deleting branches

git branch -d branchname

Deleting a merged branch.

Deleting branches

git branch -d branchname

Deleting a merged branch.

git branch -D branchname

Deleting a non-merged branch.

Merging

 

How to Merge Branches?

Why is it important to merge branches?

Merge Branches : Example

98ca9

34ac2

f30ab

master

develop

Merge Branches : Example

98ca9

34ac2

f30ab

master

98ca9

34ac2

f30ab

master

87ab2

develop

Merge Branches : Example

98ca9

34ac2

f30ab

master

98ca9

34ac2

f30ab

master

87ab2

develop

28bd1

impfix

Merge Branches : Example

98ca9

34ac2

f30ab

master

98ca9

34ac2

f30ab

master

87ab2

develop

28bd1

impfix

Merge Branches : Example

98ca9

34ac2

f30ab

98ca9

34ac2

f30ab

87ab2

28bd1

master

43ef8

develop

Merge Branches : Example

98ca9

34ac2

f30ab

98ca9

34ac2

f30ab

87ab2

28bd1

master

43ef8

develop

Snapshot to Merge into

Snapshot to Merge In

Common

Ancestor

Merge Branches : Example

98ca9

34ac2

f30ab

98ca9

f30ab

87ab2

28bd1

master

43ef8

develop

56ab3

Merge Conflicts

Changes in a file made by 2 different branches, trying to get merge results in a Merge Conflicts

Merge Conflicts

Changes in a file made by 2 different branches, trying to get merge results in a Merge Conflicts

Let's see how a Conflict look like.

Resolving Conflicts

As a programmer, you would have to resolve the merge conflicts manually.

Let's resolve a Merge Conflict

Merge Conflicts in a Project

git branch --merged

git branch --no-merged

To display merged & non merged branches only

Git Branching Workflow in Production

Branching Workflow

Long Running Branches

Topic Branches

Master

Development

Authentication

UI changes

Rebasing

Integrate Changes from one branch into another

Merging

Rebasing

With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch.

 

  1. Pointer goes to the common ancestor of the two branches (the one you’re on and the one you’re rebasing onto).

How Git performs rebase?

  1. Pointer goes to the common ancestor of the two branches (the one you’re on and the one you’re rebasing onto).
  2. Gets the diff introduced by each commit of the branch you’re on.

How Git performs rebase?

  1. Pointer goes to the common ancestor of the two branches (the one you’re on and the one you’re rebasing onto).
  2. Gets the diff introduced by each commit of the branch you’re on.
  3. Saving those diffs to temporary files.

How Git performs rebase?

  1. Pointer goes to the common ancestor of the two branches (the one you’re on and the one you’re rebasing onto).
  2. Gets the diff introduced by each commit of the branch you’re on.
  3. Saving those diffs to temporary files.
  4. Resetting the current branch to the same commit as the branch you are rebasing onto.

How Git performs rebase?

  1. Pointer goes to the common ancestor of the two branches (the one you’re on and the one you’re rebasing onto).
  2. Gets the diff introduced by each commit of the branch you’re on.
  3. Saving those diffs to temporary files.
  4. Resetting the current branch to the same commit as the branch you are rebasing onto.
  5. Finally applying each change in turn.

How Git performs rebase?

git merge experiment

No difference in the end product of the integration.

but rebasing makes for a cleaner history.

The log history looks like a linear history: it appears that all the work happened in series, even when it originally happened in parallel.

Often, you’ll do this to make sure your commits apply cleanly on a remote branch.

Key 🔑  Points on Rebasing

When you should not use Rebase 💀

Do not rebase commits that exist outside your repository and that people may have based work on.

When you rebase stuff, you’re abandoning existing commits and creating new ones that are similar but different.

Merge Vs. Rebase

One point of view is

Commit history is a record of what actually happened.

The opposing point is

the commit history is the story of how your project was made. 

Introduction to Github 🧑🏻‍💻

Git Vs. Github

Git is a version control system that lets you manage and keep track of your source code history.

GitHub is a cloud-based hosting service that lets you manage Git repositories.

If you want to work with open-source projects that use Git, then GitHub is designed to help you better manage them.

Exploring Github

Creating Remote Repository

Working with remotes

Cloning a Repository

Social Coding with Github

Fetch changes from Github

The git fetch command downloads commits, files, and refs from a remote repository into your local repo.

Fetch changes from Github

git fetch

The git fetch command downloads commits, files, and refs from a remote repository into your local repo.

Fetch changes from Github

git fetch

The git fetch command downloads commits, files, and refs from a remote repository into your local repo.

git merge

Pulling Changes form Github

git pull

Pulling Changes form Github

git pull

git pull = git fetch + git merge

Forking a Repository

Making a Pull Request

Merging a Pull Request

Github Issues

Github

Pages

Miscellaneous
Git Tools

git show commitid

Display the changes in a commit

Stashing

Stashing takes the dirty state of your working directory — i.e (your modified tracked files and staged changes), and saves it on a stack of unfinished changes that you can reapply at any time.

git stash

Stashing

Stashing takes the dirty state of your working directory — i.e (your modified tracked files and staged changes), and saves it on a stack of unfinished changes that you can reapply at any time.

git stash
git stash list

Stashing

Stashing takes the dirty state of your working directory — i.e (your modified tracked files and staged changes), and saves it on a stack of unfinished changes that you can reapply at any time.

git stash
git stash list
git stash apply stash@{0}

Stashing

Stashing takes the dirty state of your working directory — i.e (your modified tracked files and staged changes), and saves it on a stack of unfinished changes that you can reapply at any time.

git stash
git stash list
git stash apply stash@{0}
git stash drop stash@{0}

Cleaning your working directory

You may not want to stash some work or files in your working directory, but simply get rid of them; that’s what the git clean 

command is for.

git clean -f -d

Changing the Last Commit

git commit --amend

Changing the message of the last commit

Changing the Last Commit

Changing the actual content of the last commit.

       - Make changes in the working directory.

       - Add files to the staging area.

          - Run the same command.

 

Changing the Last Commit

Changing the actual content of the last commit.

       - Make changes in the working directory.

       - Add files to the staging area.

          - Run the same command.

 

Note: don’t amend your last commit if you’ve already pushed it.

Checkout Commit

git checkout commitid

you can travel the time to go in the past to any specific commit.

Checkout Commit

git checkout commitid

you can travel the time to go in the past to any specific commit.

git switch -

Checkout Commit

git checkout commitid

you can travel the time to go in the past to any specific commit.

git switch -c  branchname
git switch -
git revert commitid

Revert Commit

you can undo the changes of any commit that you want.

git revert commitid

Revert Commit

you can undo the changes of any commit that you want.

# explicitly commit the revert changes
git revert -n commitid

git revert --abort

Reset Commit

git reset --soft commitid

git reset --hard commitid

very dangerous command.

     - delete all the commits from the

        given commit id onwards.

Deleting last n commits

git rebase -i commitid

git push -f origin master
git reset --hard commitid

git push -f origin master

Alternative Method

🍒 Cherry-Picking Commits

git cherry-pick commitid

Pick the changes from a specific commit from any branch and merge with another branch.