Git Cheat Sheet

Find Useful Git commands in this Git Cheat Sheet.

  • Clone a project:
    git clone git_repo_url project_name
  • Switch to a branch locally:
    git checkout branch_name
  • Modify a file, then save and push it to remote repo in current branch
    git add path_to_file_modifeid/file_name
    git commit -m “Description of modification”
    git push
  • Get a new version of a file after modifying local version
    git checkout path_to_file_modified/file_modified
  • Get latest version of current branch
    git fetch
    git pull
    if you have local changes, you will be prompted to commit or stash them before pulling.
  • Create a new branch based on current branch and switch to it
    git checkout –b branch_name
  • Switch to master branch
    git checkout master
  • Merge branch_name to master
    git merge branch_name
  • Delete local branch
    git branch -d branch_name
  • Undo a merge or pull
    git reset –hard
  • Undo a commit locally and on the remote branch
    git reset –hard commit_id
    git push –force
  • Get remote url of a local branch
    git remote show origin

Source: https://git-scm.com

Script with hash tables on windows and Linux

How to declare and write a script with hash tables on windows and linux

  • Hash tables with powershell on windows

    Declaration:
    $states=@{“Alberta” = “Calgary”; “British Columbia” = “Vancouver”; “Ontario” = “Toronto” ; “Quebec” = “Montreal”}

    Name
    _____
    Value
    _______
    Alberta Calgary
    British Columbia Vancouver
    Ontario Toronto
    Quebec Montreal

    Add new key-value in hashtable:
    $states.Add(“Manitoba”,”Winnipeg”)

    Remove key-value in hashtable:
    $states.Remove(“Manitoba”,”Winnipeg”)
    Change value in hashtable:
    $states.Set_Item(“Ontario”,”Ottawa”)
    Retrieve value in hashtable:
    $states.Get_Item(“Alberta”)
    Find key in hashtable:
    $states.ContainsKey(“Alberta”)
    Find Value in hashtable:
    $states.ContainsValue(“Calgary”)
    Count items in hashtable:
    $states.Count
    Sort items by Name in hashtable:
    $states.GetEnumerator() | Sort-Object Name -descending
    Sort items by Value in hashtable:
    $states.GetEnumerator() | Sort-Object Value -descending

  • Hash tables with perl on linux or windows

     
    Declaration:
    my %hash = (); #Initialize a hash
    my $hash_ref = {}; # Initialize a hash reference. ref will return HASH
    Clear (or empty) a hash
    for (keys %hash)
    {
    delete $hash{$_};
    }
    Clear (or empty) a hash reference
    for (keys %$href)
    {
    delete $href->{$_};
    }
    Add a key/value pair to a hash
    $hash{ ‘key’ } = ‘value’; # hash
    $hash{ $key } = $value; # hash, using variables
    Using Hash Reference
    $href->{ ‘key’ } = ‘value’; # hash ref
    $href->{ $key } = $value; # hash ref, using variables
    Add several key/value pairs to a hash
    %hash = ( ‘key1’, ‘value1’, ‘key2’, ‘value2’, ‘key3’, ‘value3’ );
    %hash = (
    key1 => ‘value1’,
    key2 => ‘value2’,
    key3 => ‘value3’,
    );

    Copy a hash
    my %hash_copy = %hash; # copy a hash
    my $href_copy = $href; # copy a hash ref
    Delete a single key/value pair
    delete $hash{$key};
    delete $hash_ref->{$key};

  • Hash tables with python on linux or windows

    Hash tables are called dictionary in python.
    Declaration:
    dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
    Accessing Values
    print “dict[‘Name’]: “, dict[‘Name’]
    print “dict[‘Age’]: “, dict[‘Age’]
    Output:
    dict[‘Name’]: Zara
    dict[‘Age’]: 7
    Updating Dictionary
    dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}

    dict[‘Age’] = 8; # update existing entry
    dict[‘School’] = “DPS School”; # Add new entry
    Delete Dictionary Elements
    #!/usr/bin/python

    dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}

    del dict[‘Name’]; # remove entry with key ‘Name’
    dict.clear(); # remove all entries in dict
    del dict ; # delete entire dictionary

Source:

  1. https://technet.microsoft.com/en-us/library/ee692803.aspx
  2. http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
  3. http://www.tutorialspoint.com/python/python_dictionary.htm

How to pipe grep on command line on windows and Linux

Let’s find how to pipe grep or find a specific string after running a command using shell, batch and powershell (windows and Linux)

  • On Linux via shell

    ls -al | grep filename

  • On Windows via powershell

    GetChildItem | Select-Object “filename”
    or
    GetChildItem | where-Object {$_ -match “filename”}

  • On Windows via batch

    Dir | findstr “filename”

reverse a string on Linux and Windows

How to reverse a string on Linux and Windows

Via shell script on Linux

reverse a string on Linux and Windows

sh-3.2# vi reverse.sh
#### Start Script #####
#!/bin/bash
input_string=”$1″
reverse_string=””

input_string_length=${#input_string}
for (( i=$input_string_length-1; i>=0; i– ))
do
reverse_string=”$reverse_string${input_string:$i:1}”
done

echo “$reverse_string”
##### End Script #####

Let’s run it:

sh-3.2# chmod 775 reverse.sh
sh-3.2# ./reverse.sh Etienne
enneitE

Via powershell script on Windows

#Let’s use the script reverse.ps1 below.
######
$string=”Etienne”
$string_array=$string -split “”
[array]::Reverse($string_array)
$string_array -join ”

#####Output#####
PS C:\Users\etienne_noumen\Documents\Etienne\Scripting> .\reverse.ps1

E t i e n n e

enneitE

Via powershell script on Windows in one line

([regex]::Matches($String,’.’,’RightToLeft’) | ForEach {$_.value}) -join ”

Via batch script on Windows

::Note: ReverseStr also calls StrLen
::and string length is not greater than 80 chars
:: but can be changed.

@echo off
SetLocal EnableDelayedExpansion
cls
set Str=Etienne
call :StrLen %Str%
echo Length=%Len%
call :ReverseStr %Str%
echo String=%Str%
echo Reverse Str=%Reverse%
exit /b

::—————-
:: Calc Var Length
::—————-
:: %*=Str to Check
:: Returns %Len%
:: —————
:StrLen %*
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not “!Char!”==”” (
set /a Len=%%a+1
) else (exit /b)
)
exit /b

::—————
:: Reverse String
::—————
:: %* Str to Reverse
:: Returns %Reverse%
::——————
:ReverseStr %*
set Data=%*
call :StrLen %Data%
for /L %%a in (!Len!,-1,0) do (
set Char=!Data:~%%a,1!
set Reverse=!Reverse!!Char!
)
exit /b

Via perl script on Windows or Linux

Via python script on Windows or Linux

Source:

  1. http://www.computing.net/answers/programming/reverse-a-string-in-dos/26004.html

List only regular file names in a directory

How to List only regular file names in a directory on Linux and Windows

Listing regular files in a directory without including . and .. files.


  • On Linux

    Solution 1:$ ls -p | grep -v /
    Solution 2: $ ls -F | grep -v ‘[/@=|]’
    Solution 3: $for list in `ls` ; do ls -ld $list | grep -v ^d > /dev/null && echo $list ; done ;
    Solution4:$ for list in `ls` ; do ls -ld $list | grep ^d > /dev/null || echo $list ; done ;
    Solution5 (exclude sym links):$ for list in `ls` ; do ls -ld $list | grep -v ^l > /dev/null && echo $list ; done ;


  • On Windows

    Solution 1: dir /a-d /b >..\File_List.txt

prompt and read input variables from keyboard

Let’s find how to prompt and read input variables from keyboard while executing a script using shell, perl, python, batch and powershell (windows and Linux)

  • On Linux via shell

    read -p “Enter your name: ” name
    echo “Hi, $name. Let’s be friend!”

  • On Windows via powershell

    $name=read-host “Enter your name:”
    write-host “Hi $name, Let’s be friend!”

  • On Windows via batch

    Set /p Name=”Enter your name:”
    echo “Hi %name%, Let’s be friend!”

  • On Windows or Linux via perl

    print “Enter your name “;
    my $name = ;
    chomp $name; # Get rid of newline character at the end
    print “Hello $name, let’s be friend”;

  • On Windows or Linux via python

    name=input(“Enter your name: “)
    print (“Hello ” + name + ” let’s be friend”)

Replace all instances of a string in a file

Shell script to replace all instances of a string in a file on Linux & Windows.

  • On Linux via bash script

    sed “s/$stringToReplace/$replaceWith/g” $File_Name > $File_Name

  • On Windows using Powershell

    ( get-content $File_Name ) | % { $_ -replace $stringToReplace, $replaceWith } | set-content $File_Name

  • On Windows using Batch

    set str=teh cat in teh hat
    echo.%str%
    set str=%str:teh=the%
    echo.%str%

    Script Output:
    teh cat in teh hat
    the cat in the hat

  • On Windows or Linux using Perl

    perl -pi.orig -e “s///g;”

  • On Windows or Linux using Python

Source:

  1. http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir

Search all files containing a specific string

How to search all files containing a specific string on Linux and Windows?

  • On Linux

    grep -rnw ‘directory’ -e “pattern”
    grep –include=\*.{txt,log} -rnw ‘directory’ -e “pattern”
    This will only search for files with .txt or .log extension.
    grep –exclude=*.txt -rnw ‘directory’ -e “pattern”
    This will exclude files with .txt extensions.

  • On Windows

    CD Location
    FINDSTR /L /S /I /N /C:”pattern” *.log

Browse the internet via command line

How to browse the internet via command line on Linux and Windows?

  • On Linux

    lynx http://google.ca
    If you don’t have lynx on your linux installation, you will have to install it. On Linux Red hat, install it like this:
    yum list lynx (to check the availability of the package)
    yum -y install lynx (to install the package)
    you can also use: curl -0 http://yoursite/index.html to get the source code of a specific file.

  • On Windows

    start /max http://google.ca
    Will open the url using your default browser.