Git and GitHub
Let’s start from the basics: Using command line
● To check the contents of the current directory
○ Linux: ls
○ Windows: dir
● To copy a file from source path to destination path
○ Linux: cp source-path destination-path
○ Windows: copy source-path destination-path
● To move a file from source path to destination path
○ Linux: mv source-path destination-path
○ Windows: move source-path destination-path
● To go back to the previous directory
○ Linux: cd ..
○ Windows: cd
● To create a new file
○ Linux: touch filename
○ Windows: notepad filename
● To create a directory
○ mkdir
Command line - Continued
● To delete a file
○ Linux: rm filename
○ Windows: del filename
● To delete a directory
○ rmdir
● To get the current directory path
○ Linux: pwd
○ Windows: chdir
● To get information about any command
○ Linux: man <command>
○ Windows: <command> /?
● To rename an existing file
○ Linux: mv old_filename new_filename
○ Windows: rename old_filename new_filename
● To exit command line
○ Linux: exit
○ Windows: exit
Version Control System
● Software tool to helps record changes to files by keeping a track of
modifications done to the code
● Multiple people can simultaneously work on a single project
● You can work on your project from various systems
● Access to historical version of the project
● Repository: Database of changes. Contains all edits and historical versions of
the project
Types of Version Control Systems
● Centralised VCS
● Distributed VCS
Centralized Version Control System
● One central remote repository
● Users make changes locally and
commit to the central repository
● To get the latest content from the
remote repository, users have to
update.
● Drawbacks: If the central repository is
corrupted, information is lost.
Distributed Version Control System
● Repository is distributed and is present with
each user.
● Users make changes to the code and
commit to their local repositories.
● For the code to be accessible to others, users
have to push the code after committing to
their local repositories.
● Since the previous versions of the code files
exist with all the users on their local
repositories, in case of failure of one system,
data is not lost.
Git
● Distributed Version Control System
● Free and Open Source - source code: https://github.com/git/git
● Allows a team of people to work together
● Works by taking snapshot of files
● Can be used to track changes in a set of files
● You can use Git without GitHub
Git Workflow - 3 tier architecture
● Working directory, staging index/
staging area and repository are all
present on our system.
● git add -> adds the changes to
staging area
● git commit -> commits the
changes from staging area to the
repository
Setting up Git and GitHub
Setting RSA key-pair:
1. Run “ssh-keygen” in terminal.
2. Run “cat .ssh/id_rsa.sh” and copy the rsa key.
3. Paste the public key in your github account by going to settings.
Setting your username and email in local system:
1. git config --global user.name “your username”
2. git config --global user.email “youremail@gmail.com”
Difference between Git and GitHub
GitHub is platform which uses Git to communicate with our local system.
In other words, Git is a TOOL and Github is a PLATFORM.
Git Clone
● git clone -> used to make a clone/ copy of an existing repo in a new directory, at
another location
● Automatically creates a remote connection “origin” to the original repo
Creating a GitHub Repository - Cloning
● Go to github.com and login with your credentials
● Click on New to create a new repository, give it a name and click on Create
Repository.
● Optionally you can add a description for the repository
● Click on Clone and Download button and copy the repo URL
● On your command line, navigate to the path where you would like to place the
project and clone the created repository:
git clone <url>
● Move inside the cloned repository:
cd reponame
Git Init
● Creates a new Git repository
● If you already have a project, no other Git command will work on it unless you
execute this command on the project directory
● After executing the command, .git subdirectory would be created with all the
required metadata
Creating a GitHub Repository - Init
● Create a project on local system
○ cd project_name
○ git init
● Go to GitHub and create a new repo
● Copy the https URL of the repo
● Create an instance of remote URL on local system
○ git remote add origin <URL>
○ git pull origin master
● Create a new branch to make your changes
○ Git checkout -b <branch_name>
● Create a new file
○ touch filename
● Commit and push the changes
○ Git add .
○ Git commit -m “”
○ Git push origin branch_name
● Go to GitHub and create a PR
Git Remote
● Used to reference a remote repository
● While forking you were working with a remote repository so you had to create
the remote reference
● origin is the default remote reference created when you clone a repository
● When using git init on a local directory, you have to create the remote reference
to whichever repo you want the directory to point to
Git Remote Commands
● Adding a remote reference
○ git remote add <remote_reference_name> <URL>
● To list all the references stored by the repository using one of the below
commands
○ git remote -v
○ git remote -verbose
● To remove a reference
○ git remote rm <reference_name>
● To rename an existing remote reference
○ git remote rename <old_name> <new_name>
● To get more information about any existing remote reference
○ git remote show origin
Git Branching
● Diverge from main line of code and make changes to the separate line (branch)
without messing with the main line
Creating a new branch
git branch testing
HEAD Pointer
● By default points to the latest
commit on the branch
● Switches when you switch the
branch
● If you go back to master
branch from testing branch,
HEAD will point to the
master again
● You can move across the
commits and the HEAD will
move accordingly in the branch.
HEAD Pointer - Moving in a Branch
HEAD Pointer - Moving across Branches
● If you switch to the testing
branch, HEAD will point the
last commit of testing branch
Committing to the new branch
● If you execute on testing
branch
○ git commit -m “message”
● The HEAD has now moved to
the new commit on testing
branch
● You create a pull request in
order to be able to merge your
branch (testing) to the master
branch
● After merge, the master and
testing branch will both point
to the latest commit i.e. ‘87ab2’
in this case
Git Branches & Pull Requests
● Create a new branch
○ git checkout -b branch-name
● Command to list down the branches
○ git branch
● Command to switch from one existing branch to another
○ git checkout branch_name
● Make your changes in the new branch
○ git add filename
○ git commit -m “message”
○ Git push origin master
● After pushing go to your repo and create pull request.
Forking - Working on someone else’s repo
● Go to https://github.com/girlscript-blr/codewithgirlscriptblr and fork the project
● Go to the forked repo present in your GitHub account and copy the repo https
URL
● Clone the repo in your local system
○ git clone <URL>
● Add https://github.com/girlscript-blr/codewithgirlscriptblr as a remote URL
○ git remote add upstream https://github.com/girlscript-blr/codewithgirlscriptblr
● Create a new branch for your changes
○ git checkout -b <branch_name>
● Create a new file/edit any existing file
● Commit and push the changes
● Create a PR on GitHub
Git Merge
Fast Forward vs True Merge
Fast Forward Merge True Merge
Merge Conflicts
Git Log
● Command to view the commit history in reverse order
○ git log
● Hash is unique to each commit and depends on the contents modified in the
commit as well as the author of the commit
● Git log will provide with the commit id, author’s name, email, date and commit
message of each commit
● To view only a certain number of past commits - below command will display last
2 commits
○ git log -p -2
Git Commit Id
● Generates a checksum for each
commit
● Checksum -> algorithm run on
some data
● Remains unique for all commits
● Data -> all the changes made in
the commit, author of the
commit, commit message and
some other metadata
Undoing in Git
● Modifying the last commit
○ To either add more files to the last successful commit or change the commit message
■ git commit --amend
● Unstaging a staged file
○ Once you perform git add . the files are added to the staging area
○ In order to remove a certain file from the staging area
■ git restore --staged file_name
● Reverting the last commit
○ To delete the last commit from staging area but still have changes in working directory
■ git reset --soft HEAD~1
○ To delete the last commit and also the changes from working directory
■ git reset --hard HEAD~1
○ Default one
■ git reset --mixed HEAD~1
Cherry Picking in Git
Git Rebase
Merging vs Rebasing
Merging vs Rebsing

Git and GitHub

  • 1.
  • 2.
    Let’s start fromthe basics: Using command line ● To check the contents of the current directory ○ Linux: ls ○ Windows: dir ● To copy a file from source path to destination path ○ Linux: cp source-path destination-path ○ Windows: copy source-path destination-path ● To move a file from source path to destination path ○ Linux: mv source-path destination-path ○ Windows: move source-path destination-path ● To go back to the previous directory ○ Linux: cd .. ○ Windows: cd ● To create a new file ○ Linux: touch filename ○ Windows: notepad filename ● To create a directory ○ mkdir
  • 3.
    Command line -Continued ● To delete a file ○ Linux: rm filename ○ Windows: del filename ● To delete a directory ○ rmdir ● To get the current directory path ○ Linux: pwd ○ Windows: chdir ● To get information about any command ○ Linux: man <command> ○ Windows: <command> /? ● To rename an existing file ○ Linux: mv old_filename new_filename ○ Windows: rename old_filename new_filename ● To exit command line ○ Linux: exit ○ Windows: exit
  • 4.
    Version Control System ●Software tool to helps record changes to files by keeping a track of modifications done to the code ● Multiple people can simultaneously work on a single project ● You can work on your project from various systems ● Access to historical version of the project ● Repository: Database of changes. Contains all edits and historical versions of the project
  • 5.
    Types of VersionControl Systems ● Centralised VCS ● Distributed VCS
  • 6.
    Centralized Version ControlSystem ● One central remote repository ● Users make changes locally and commit to the central repository ● To get the latest content from the remote repository, users have to update. ● Drawbacks: If the central repository is corrupted, information is lost.
  • 7.
    Distributed Version ControlSystem ● Repository is distributed and is present with each user. ● Users make changes to the code and commit to their local repositories. ● For the code to be accessible to others, users have to push the code after committing to their local repositories. ● Since the previous versions of the code files exist with all the users on their local repositories, in case of failure of one system, data is not lost.
  • 8.
    Git ● Distributed VersionControl System ● Free and Open Source - source code: https://github.com/git/git ● Allows a team of people to work together ● Works by taking snapshot of files ● Can be used to track changes in a set of files ● You can use Git without GitHub
  • 9.
    Git Workflow -3 tier architecture ● Working directory, staging index/ staging area and repository are all present on our system. ● git add -> adds the changes to staging area ● git commit -> commits the changes from staging area to the repository
  • 10.
    Setting up Gitand GitHub Setting RSA key-pair: 1. Run “ssh-keygen” in terminal. 2. Run “cat .ssh/id_rsa.sh” and copy the rsa key. 3. Paste the public key in your github account by going to settings. Setting your username and email in local system: 1. git config --global user.name “your username” 2. git config --global user.email “youremail@gmail.com”
  • 11.
    Difference between Gitand GitHub GitHub is platform which uses Git to communicate with our local system. In other words, Git is a TOOL and Github is a PLATFORM.
  • 12.
    Git Clone ● gitclone -> used to make a clone/ copy of an existing repo in a new directory, at another location ● Automatically creates a remote connection “origin” to the original repo
  • 13.
    Creating a GitHubRepository - Cloning ● Go to github.com and login with your credentials ● Click on New to create a new repository, give it a name and click on Create Repository. ● Optionally you can add a description for the repository ● Click on Clone and Download button and copy the repo URL ● On your command line, navigate to the path where you would like to place the project and clone the created repository: git clone <url> ● Move inside the cloned repository: cd reponame
  • 14.
    Git Init ● Createsa new Git repository ● If you already have a project, no other Git command will work on it unless you execute this command on the project directory ● After executing the command, .git subdirectory would be created with all the required metadata
  • 15.
    Creating a GitHubRepository - Init ● Create a project on local system ○ cd project_name ○ git init ● Go to GitHub and create a new repo ● Copy the https URL of the repo ● Create an instance of remote URL on local system ○ git remote add origin <URL> ○ git pull origin master ● Create a new branch to make your changes ○ Git checkout -b <branch_name> ● Create a new file ○ touch filename ● Commit and push the changes ○ Git add . ○ Git commit -m “” ○ Git push origin branch_name ● Go to GitHub and create a PR
  • 16.
    Git Remote ● Usedto reference a remote repository ● While forking you were working with a remote repository so you had to create the remote reference ● origin is the default remote reference created when you clone a repository ● When using git init on a local directory, you have to create the remote reference to whichever repo you want the directory to point to
  • 17.
    Git Remote Commands ●Adding a remote reference ○ git remote add <remote_reference_name> <URL> ● To list all the references stored by the repository using one of the below commands ○ git remote -v ○ git remote -verbose ● To remove a reference ○ git remote rm <reference_name> ● To rename an existing remote reference ○ git remote rename <old_name> <new_name> ● To get more information about any existing remote reference ○ git remote show origin
  • 18.
    Git Branching ● Divergefrom main line of code and make changes to the separate line (branch) without messing with the main line
  • 19.
    Creating a newbranch git branch testing
  • 20.
    HEAD Pointer ● Bydefault points to the latest commit on the branch ● Switches when you switch the branch ● If you go back to master branch from testing branch, HEAD will point to the master again
  • 21.
    ● You canmove across the commits and the HEAD will move accordingly in the branch. HEAD Pointer - Moving in a Branch
  • 22.
    HEAD Pointer -Moving across Branches ● If you switch to the testing branch, HEAD will point the last commit of testing branch
  • 23.
    Committing to thenew branch ● If you execute on testing branch ○ git commit -m “message” ● The HEAD has now moved to the new commit on testing branch ● You create a pull request in order to be able to merge your branch (testing) to the master branch ● After merge, the master and testing branch will both point to the latest commit i.e. ‘87ab2’ in this case
  • 24.
    Git Branches &Pull Requests ● Create a new branch ○ git checkout -b branch-name ● Command to list down the branches ○ git branch ● Command to switch from one existing branch to another ○ git checkout branch_name ● Make your changes in the new branch ○ git add filename ○ git commit -m “message” ○ Git push origin master ● After pushing go to your repo and create pull request.
  • 25.
    Forking - Workingon someone else’s repo ● Go to https://github.com/girlscript-blr/codewithgirlscriptblr and fork the project ● Go to the forked repo present in your GitHub account and copy the repo https URL ● Clone the repo in your local system ○ git clone <URL> ● Add https://github.com/girlscript-blr/codewithgirlscriptblr as a remote URL ○ git remote add upstream https://github.com/girlscript-blr/codewithgirlscriptblr ● Create a new branch for your changes ○ git checkout -b <branch_name> ● Create a new file/edit any existing file ● Commit and push the changes ● Create a PR on GitHub
  • 26.
  • 28.
    Fast Forward vsTrue Merge Fast Forward Merge True Merge
  • 29.
  • 30.
    Git Log ● Commandto view the commit history in reverse order ○ git log ● Hash is unique to each commit and depends on the contents modified in the commit as well as the author of the commit ● Git log will provide with the commit id, author’s name, email, date and commit message of each commit ● To view only a certain number of past commits - below command will display last 2 commits ○ git log -p -2
  • 31.
    Git Commit Id ●Generates a checksum for each commit ● Checksum -> algorithm run on some data ● Remains unique for all commits ● Data -> all the changes made in the commit, author of the commit, commit message and some other metadata
  • 32.
    Undoing in Git ●Modifying the last commit ○ To either add more files to the last successful commit or change the commit message ■ git commit --amend ● Unstaging a staged file ○ Once you perform git add . the files are added to the staging area ○ In order to remove a certain file from the staging area ■ git restore --staged file_name ● Reverting the last commit ○ To delete the last commit from staging area but still have changes in working directory ■ git reset --soft HEAD~1 ○ To delete the last commit and also the changes from working directory ■ git reset --hard HEAD~1 ○ Default one ■ git reset --mixed HEAD~1
  • 33.
  • 34.
  • 35.
  • 36.