Wednesday, July 24, 2019

Git Crash Course

GITUB:
IT is version control system(VCS) and Source Control Mngmt(SCM).
1,Repositories
2.Version
3,Artifact
4,Comparision
5,Collaboration
6,Accountability

Gitub Birth-2005
Git founder->Linus Torvalds

3 main states in GIT:
Modified
Staged
Commited

3 section:
Working directory ->It is a root directory
Staging Area: Also called index
Commit ARea:

git version


Configuring author and email
# home directory
$ pwd                                                          
$ mkdir git-fast          
$ cd git-fast
$ pwd  
$ git config --global --list   
$ git config --global user.name "Bibhash Roy"
$ git config --global --list
$ git config --global user.email "bibhash@whitepeaksoft.com"
$ git config --global --list
# vi is the editor, ~ is the home directory
$ vi ~/.gitconfig                                              
$ cat .gitconfig


Initializing an empty repository
$ pwd
$ mkdir myRepoFromScratch
$ cd myRepoFromScratch
$ pwd       
 
# notice the creation of .git directory                      
$ git init 
                     
# "ls -al" command lists all objects, including hidden                                           
$ ls -al
    
# without using any editor                                                               
$ echo "this is my first file in empty repository" >> firstFileUsingEcho.txt   
$ ls
$ cat firstFileUsingEcho.txt
 # create the file and enter some contents
$ vi  secondFileUsingVI.txt                                      
$ ls
$ cat secondFileUsingVI.txt


Existing unversioned project to repository
$ pwd
 # copy the downloaded file
$ cp /f/Bibhash/Downloads/initializr-verekia-4.0.zip .
# unzipping will create a folder called "initilizr"   
$ unzip initializr-verekia-4.0.zip                                      
$ ls -al
# remove copied file
$ rm initializr-verekia-4.0.zip
# rename directory                                        
$ mv initializr/ myRepoFromExistingSource  
            
# now we need to add source control to the unversioned repo "myRepoFromExistingSource"
$ cd myRepoFromExistingSource
       
# Initialized existing source                      
$ git init          
# .git folder can be seen with ls -al                                                             
$ ls -al
                                                               
Command Summary - configuration & repository creation
# Typically, you’ll want to use the --global flag to set configuration options for the current user.
# Define the author name to be used for all commits by the current user.
$ git config --global user.name     
# Define the author email to be used for all commits by the current user.
$ git config --global user.email    
# For listing all the global configuration at user level
$ git config --global --list        
 
# Transforms the current directory into a Git repository; the command is same for creating repo from ..contd..
# scratch or convert an existing unversioned code base to a git repository           
$ git init
                                               
Command Summary - Accessing Git Help system
# general help
$ git help
# lists sub-commands
$ git help -a
# lists concept guides
$ git help -g
# read about a specific sub-command
$ git help
# read about a specific concept
$ git help

Copying a GitHub repository
$ cd
$ git clone https://github.com//
$ cd example_project
$ ls -al 



Adding your changes
# home directory
$ pwd                        
$ cd git-fast
$ ls
$ cd myRepoFromScratch
$ ls

# create a file and add some content
$ vi demoFile1                  
$ cat demoFile1
$ git status
$ git add demoFile1
$ git rm --cached demoFile1
$ git status


Committing changes

 # home directory
$ pwd                            
$ git status
$ git add demofile1
$ git status
$ git commit -m "our first commit in this course"
$ git status


Command Summary - add and commit

$ git add
$ git commit -m "message goes here"

How to check your repo status
# cd to /c/Users/HP-PC/git-fast/myRepoFromScratch
$ pwd
$ ls

# start with a clean working directory
$ git status                

# create file and add some contents such as "I want to shed 15 kg 5 weeks"      
$ vi weightLossChart    

# create file and add some contents such as "I want to add more green vegetables to my diet"  
$ vi dietChart                
$ git status           

 # same as "git status" since "long" option is the default one (compare output)  
$ git status --long      
$ git status -s                                 # status "??" for untracked file
$ git add weightLossChart
$ git status -s                                 # status "A"
$ git commit -m "1st commit for weightLossChart"
$ git status -s   
$ vi weightLossChart                 # make some changes to the file
$ git status -s                              # status "M"
$ git add weightLossChart
$ git status -s                                # status "M"
$ git commit -m "2nd commit for weightLossChart"
$ git status -s 
$ mv weightLossChart weightLossChart2
$ git status -s                             # status "D"


How to check commit history

# displays the entire commit history using the default formatting
$ git log                

# oneline condensed view of each commit history                    
$ git log --oneline

# Only display commits that include the specified file                           
$ git log           

# Show only commits that occur between and . Both arguments can be either a commit ID ...contd.
# a branch, name, HEAD, or any other kind of revision reference.                  
$ git log ..                     

# Limit the number of commits by . For example, git log -n 3 will display only 3 commits.
$ git log -n                           


Command Summary - status & log

# provides a status report of the repo
$ git status             

# displays the entire commit history using the default formatting                     
$ git log                

 # oneline condensed view of each commit history                    
$ git log --oneline           

# Only display commits that include the specified file               
$ git log      

# Show only commits that occur between and . Both arguments can be either a commit ID, a branch
# name, HEAD, or any other kind of revision reference.                      
$ git log ..                    

# Limit the number of commits by . For example, git log -n 3 will display only 3 commits.
$ git log -n                          

LECTURE - Checking out commits in a Git repository - Part 1

$ cd git-fast
# created a fresh git repository named "demo-checkout-commit" using initializr.zip
# Build up a commit history by modifying the file robots.txt, say 5 times and committing each time
# Let us assume we have one commit whose id/hash is 91770af
# the choice of commit id 91770af is arbitrary

# note where the HEAD is now (since HEAD will change later while doing a checkout)
$ git log --oneline

# clear the screen
# The command below will put you in a detached HEAD state.
# HEAD no longer points to a branch; it points directly to a commit.
$ git checkout 91770af



LECTURE - Checking out commits in a Git repository - Part 2

# you are in detached HEAD state and inside git repository (demo-checkout-commit)
# You can see only commits till commit-id 91770af
$ git log --oneline

# now edit and do a express commit for file robots.txt
# note down your new commit id/hash (Say ID1)
$ git log --oneline

$ git checkout master

# changes made in detached HEAD state is no more there
# the new commit ID1 is not visible
$ git log --oneline

# again you are in detached HEAD state
$ git checkout 91770af

# the new commit ID1 is not visible which means earlier changes...
# in detached HEAD state were not preserved
$ git log --oneline

# now we want to retain our commit i.e, preserve our changes
# make changes in the robots.txt and do an express commit

# create a branch named weird-experiment
$ git branch  weird-experiment

$ git checkout master
# the new commit id is not visible here
# you can check the file robots.txt using cat command also
$ git log --oneline

# new commit id is visible now
# you can examine the file robots.txt using cat command
$ git checkout weird-experiment



LECTURE - Checking out files

# cd to git repository (demo-checkout-commit) used in previous lecture
$ git log --online

# Let us assume we have the commit-id 91770af

# step-2: examine the current contents of the file
$ cat robots.txt

# now we will able to see the content of the file as at commit-id 91770af
$ git checkout 91770af robots.txt

# this will show the file has been added to staging area
$ git status

# step-5: examine the contents of the file (should be different from step-2)
$ cat robots.txt

# if we do not want to revert back the file robots.txt
$ git checkout HEAD robots.txt

# you will see a clean working directory
$ git status

# now we want to revert back to previous version of robots.txt as at commit-id 91770af
$ git checkout 91770af robots.txt
$ git commit -am "reverting back file state to commit 91770af"

# clean working directory
$ git status


# file is reverted and the contents are as seen in step-5
$ cat robots.txt  



LECTURE - Reverting changes

# cd into repository "undo-demo-git-repo"

# add the text say, "line 5"
$ vim checkoutfile.txt

# modified stage visible
$ git status

# commit the above change
$ git commit -am checkoutfile.txt "commit message..."

$ git log --oneline
$ cat checkoutfile.txt

# Now let's assume the earlier commit is a buggy one and so we will revert it
# revert command introduces a new commit in a safe manner (opens the default editor)
$ git revert HEAD

# check commit history - a new commit for revert
$ git log --oneline

# note the change made earlier has been reverted "line 5" is not visible
$ cat checkoutfile.txt



LECTURE - Resetting Git repository - Part1

# we are in repository git-reset-demo-1 with 2 files goldies-time-table and jimmys-time-table
# the repository has an existing commit history

# now edit file jimmys-time-table and add a line at the end
$ vim jimmys-time-table

# add the change to staging area
$ git add jimmys-time-table

# change in staging area is visible
$ git status

# undoes changes in staging area
$ git reset jimmys-time-table

# changes in staging area is removed; file in modified state
$ git status

# the changed in working directory is intact
$ cat jimmys-time-table



LECTURE - Resetting Git repository - Part2

# we are in repository git-reset-demo-2 (this repo is clone of git-reset-demo-1)

# edit file and add one line at the end
$ vim jimmys-time-table

# edit file and add one line at the end
$ vim goldies-time-table

# files are in modified state
$ git status

# add both files to staging area
$ git add .

# changes visible in staging area
$ git status

# "reset" command removes changes in staging area leaving the working directory intact
$ git reset

# both files in modified state
$ git status

#######################################
# git reset demo using the --hard option
# we are still in git-reset-demo-2 repository(this repo is clone of git-reset-demo-1)

# files are in modified state
$ git status

# add both files to staging area
$ git add .

# changes visible in staging area
$ git status

# --hard option removes all changes in working directory and staging area
$ git reset --hard

# the working directory is clean since all changes have been reset in working dir and staging
$ git status



LECTURE - Resetting Git repository - Part3

# we are inside git-reset-demo-4 repository (this repo is clone of git-reset-demo-1)

# Let's say, we have 5 commits in the commit history
# let' say, the 5th commit-id (HEAD) is 9166e4f and 4th commit-id is d21a539
$ git log --oneline

# let's reset the repository to 4th commit-id
$ git reset d21a539

# the 5th commit-id(9166e4f) has been removed from history and the HEAD is now at d21a539
$ git log --oneline

# the files are in modified state and changes in staging area has been removed
# the files are jimmys-time-table and goldies-time-table
$ git status

# now we will commit the above changes in working dir in 2 smaller chunks
$ git add jimmys-time-table
$ git commit -m "commit message....."

# now only goldies-time-table changes are visible in modified state
$ git status

$ git add goldies-time-table
$ git commit -m "commit message....."

# clean working directory
$ git status

$ git log --oneline



# we are in git-reset-demo-5 repository (this repo is clone of git-reset-demo-1)

# let's say there are 5 commits
$ git log --oneline

# now we will reset to the 3rd commit-id a53f51c (2 commits before HEAD) with the --hard option
# this command will remove 4th & 5th commit-ids as well removed all changes in working dir and staging area
$ git reset --hard a53f51c

# the 4th and 5th commit-ids has been removed from history
$ git log --oneline

# examine the file contents to confirm things
# the lines pertaining to 4th and 5th commit-ids has been removed
$ cat goldies-time-table
$ cat jimmys-time-table



LECTURE - Cleaning Git repository

$ cd git-fast
$ mkdir git-clean-demo
$ cd git-clean-demo

# create an empty repository
$ git init

# create few empty files
$ touch clean-demo-file
$ touch clean-demo-tracked-file

$ git add clean-demo-tracked-file
$ git commit -m "commit tracked file"

# shows clean-demo-file as an untracked file
$ git status

# -n option allows to make a dry run of "git clean" command
$ git clean -n

#################################################

# -f option (-f means force) removes untracked files from current directory
# so this will remove clean-demo-file
$ git clean -f

#################################################

$ mkdir clean-demo-dir
$ cd clean-demo-dir

# create an untracked file
$ touch clean-demofile-2

$ cd ..

# removes untracked file in path clean-demo-dir
$ git clean -f clean-demo-dir/

#confirm untracked file cleaning
$ cd clean-demo-dir

# "ls" produces no results meaning the untracked file was cleaned
$ ls

#################################################
$ cd ..
$ ls
$ touch clean-demo-file-2
$ ls

# -df option cleans both untracked files and directories
$ git clean -df
$ ls

#################################################
# create .gitignore file and add the file name "clean-demo-file-5"
$ vim .gitignore
$ git add .gitignore
$ git commit -m "committing .gitignore file"

$ touch clean-demo-file-4
$ touch clean-demo-file-5
$ ls -al

# -xf option cleans all untracked files including those mentioned in .gitignore file
$ git clean -xf

$ ls

Command Summary - pull and push
$ git pull

# cloning generally sets up both the names "origin and master" for you automatically
$ git push origin master 


Checking existing SSH Keys
$ ls -al ~/.ssh         # Lists the files in your .ssh directory, if they exist


Generate an SSH Key

# Generates public/private rsa key pair with default values - DON'T RUN THIS ONE
$ ssh-keygen           

 # 't' for type of key, 'b' for length of key, 'C' for comment for maintaining multiple keys                              
$ ssh-keygen -t rsa -b 2048 - C "home machine"

        

Enable SSH Agent & add the SSH Key to the agent

# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"        

# Add your SSH key to the ssh-agent  
$ ssh-add ~/.ssh/id_rsa.pub           


Testing SSH connection

# Verify SSH connection; here 'git" is the user name; note that you cannot use your github user name such as 'william'
$ ssh -T git@github.com                 

# another alternative way to verify if firewall issues are present
$ ssh -T -p 443 git@github.com          


Remote URL switching and SSH push

# List your existing remotes in order to get the name of the remote you want to change
$ git remote -v      

 # set remote URL
$ git git remote set-url origin git@github.com:USERNAME/OTHERREPOSITORY.git       

 # Verify new remote URL
$ git remote -v                                


Changing passphrase

# for changing password occasionally
$ ssh-keygen -p          

Installing and Configuring text editor (Sublime Text) for Git on Windows

# Specifying -w will cause the subl command to not exit until the file is closed
$ git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"

Installing and Configuring text editor (Sublime Text) for Git on Linux

# extract downloaded file - you can use "tar --help"...CONTD
# to see the meaning of the options "xvzf"
$ tar vxjf "sublime_text_3_build_3059_x32.tar.bz2"
 
# It is common to locate 3rd-party applications in..CONTD
# the /opt/ directory, so move the extracted files there
$ sudo mv sublime_text_3 /opt/
 
# make a Bash script which will allow us to execute..CONTD
# Sublime Text 3 with a simple command
$ cd /opt/sublime_text_3
 
# create file subl ; vim is improved vi
$ vim subl
 
# add these 2 code lines to file subl
# see below *** for command meaning
#!/bin/sh
exec /opt/sublime_text_3/sublime_text "$@"
 
# This allows anyone to read or execute the script, but only the owner can write to it.
$ sudo chmod 755 subl
 
# moving to /usr/bin
# This is the primary directory of executable commands on the system.
$ sudo mv subl /usr/bin/
 
# Specifying -w will cause the subl command to not exit until the file is closed
$ git config --global core.editor "subl -w"
 
# test to see whether sublime text is the default editor now
$ git config --global --edit



Installing and Configuring text editor (Sublime Text) for Git on Mac

# Create the symlink
$ ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl

# # test to see whether sublime text is the default editor now
$ git config --global core.editor "subl -n -w"

** CONTD : means "continued"

Installing and Configuring text editor (Sublime Text) for Git on Windows

# Specifying -w will cause the subl command to not exit until the file is closed
$ git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"

Installing and Configuring text editor (Sublime Text) for Git on Linux

# extract downloaded file - you can use "tar --help"...CONTD
# to see the meaning of the options "xvzf"
$ tar vxjf "sublime_text_3_build_3059_x32.tar.bz2"
 
# It is common to locate 3rd-party applications in..CONTD
# the /opt/ directory, so move the extracted files there
$ sudo mv sublime_text_3 /opt/
 
# make a Bash script which will allow us to execute..CONTD
# Sublime Text 3 with a simple command
$ cd /opt/sublime_text_3
 
# create file subl ; vim is improved vi
$ vim subl
 
# add these 2 code lines to file subl
# see below *** for command meaning
#!/bin/sh
exec /opt/sublime_text_3/sublime_text "$@"
 
# This allows anyone to read or execute the script, but only the owner can write to it.
$ sudo chmod 755 subl
 
# moving to /usr/bin
# This is the primary directory of executable commands on the system.
$ sudo mv subl /usr/bin/
 
# Specifying -w will cause the subl command to not exit until the file is closed
$ git config --global core.editor "subl -w"
 
# test to see whether sublime text is the default editor now
$ git config --global --edit

Installing and Configuring text editor (Sublime Text) for Git on Mac
# Create the symlink
$ ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl

# # test to see whether sublime text is the default editor now
$ git config --global core.editor "subl -n -w"

** CONTD : means "continued"

Git diff command:
1. Difference b/w working directory and staging area:
git diff
git difftool
git diff --staged
git diff --cached
2. Difference b/w working directory and commit area
git diff HEAD
git difftool HEAD
3.Difference b/w staging area and commit area
git diff --staged HEAD
git diff --cached HEAD
4. Difference b/w orbitraory commit
git diff dsf3434  sdfds345
5. git diff master origin/master
git log command:
1.git log --oneline --decorate  --graph --all
2.git log --oneline --decorate  --graph
3. git log --stat
4. git log --oneline -n 2
5. git log -p
6. git log -p --oneline -1
7. git log --oneline grep="quick-feature"

git branch -a  a means all local and remote branch
git branch small-feature
git branch -m small-feature quick-feature
git branch
git branch -d quick-feature
git checkout small-feature
git branch -D small-feature  -->it is forcefully deleted


git merge demorbracnh
git merge demobranch --no-ff

git mergetool
git commit --amend --no-edit

git reflog
git show HEAD@{52}
.git/logs
git log -g master


git stash
git stash list
git stash save "modified line dimension-404.html"
git stash show stash@{2}
git stash -u
git stash apply stash@{2}
git stash drop stash@{4}
git stash pop stash@{1}
git stash clear
git stash branch demobranch
git branch -D brnachname
git stash apply --index
git stash --keep-index


git tag v-1.8-rc1
git tag
git tag --list
git show v-1.8-rc1
git tag -a v-1.9-rc2 -m "release version 1.9"
git tag -annaoted v-1.9-rc2 -m "release version 1.9"
git cat-file -t v-1.9-rc2
git tag -l "v-2.1,5*"
git tag --list "v-2.1,5*"
git tag
git tag -a v-3.1.0 -m  "Release 3.1"
git diff v1  v2
git tag -a v2  f--force 198a296
git tag v2 --delete
git push origin --tags

No comments:

Post a Comment

Python Challenges Program

Challenges program: program 1: #Input :ABAABBCA #Output: A4B3C1 str1="ABAABBCA" str2="" d={} for x in str1: d[x]=d...