GIT - Cheat Sheet

GIT Commands
Source: Edureka
Published Version: 1.0

Git (most of us know to some extent), is a freely available, distributed and decentralized version control system that helps developers to collaborate with multiple people on their projects and makes their life easy.

A few of the git commands including basics and advance are listed below;


# initial setup

git init

git clone


# ignore Untracked files

git status / git status -uno


git branch

git branch prod


# will show remote branches as well

git branch -a


# delete a local branch

git branch -d <branch-name>


git branch --merged

 

git checkout prod


# create a new branch with current branch code and checkout on that

git checkout -b <branch-name>


git pull

git pull origin prod

git fetch


git pull = git fetch + git merge

 

# add file to index/stage for commit

git add .

git add <folder-name>/*


# remove file from index/stage

git reset <file_path> /

git restore  --staged  <file_path>


# to view changes in working directory, press q to exit from diff screen

git diff <file_path>


# to discard/revert changes in working directory

git restore <file_path>


# add file to .gitignore to avoid mistake repetition

echo filename >> .gitignore


# will remove the file from both the index/stage and the local working tree which is not always desirable

git rm <file-name>


git mv <old_filepath> <new_filepath>

 

# with commit

git cherry-pick -e <commit-SHA>


# after resolving the conflicts if any occurs

git cherry-pick --continue


# without commit

git cherry-pick -n <commit-SHA>


git commit -m "initial commit" / git commit -am "initial commit"


# if only the commit message gets changes or destroyed, it's acceptable

git commit --amend


git push


# for first time push

git push --set-upstream origin prod 

git push -u origin prod


# remove a remote branch

git push origin -d <branch-name>


git revert <commit-SHA>

 

# Hard reset if rollback to any previous point/head

git checkout <branch-name>

git reset --hard <tag> / git reset --hard <commit-SHA>

git push --force origin <branch-name>


# Showing log, commit, branch information

git log / git log <file-name>

git reflog

git ls-tree -r <branch-name>

git show <commit-SHA> / git show <branch-name> 


# Tagging a commit

git tag -a <tag-name> -m "initial tag"

git tag -a <tag-name> <commit-SHA> -m "message"

git show <tag-name> / press "q" to quit

 

# Create patch file and applying that rollback to any previous point/head

git checkout <tag-name>

git diff <branch-name> > <patch-name>.patch

git checkout <branch-name>

cat <patch-name>.patch | git apply

git commit -am 'Rolled back to <tag-name>'

git push origin <branch-name>

 

# save uncommitted changes locally, switch branches to work on other parts  

git stash

git stash list / git stash show stash@{1}

git stash apply stash@{1}

git stash pop (LIFO) = git stash apply + git stash drop


# removes untracked files from the working directory

git clean

 

git config

git config --local user.name "Prasun Das"

git config --local user.email "test@gmail.com"


git --version

 

git help <keyword>


#Multiple git functions with single command

git fetch && git pull


# to clean the command terminal

clear


Difference between Git and GitHub?

Git

GitHub

This is a distributed version control system installed on local machines which allow developers to keep track of commit histories and supports collaborative work.


This is a cloud-based source code repository developed by using git.

SVN, Mercurial etc are the competitors.

GitLab, Atlassian BitBucket etc are the competitors.


GitHub provides a variety of services like forking, user management (organization, team), releases etc along with providing a central repository for collaborative work.


To check GitHub Status;

https://www.githubstatus.com/

Using Git hook pre-commit we can restrict developers to commit code without PSR2 standard code at the time of commit any changes.

 

.git/hooks/pre-commit

 

Code Snippets

STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".php\{0,1\}$")

 

if [[ "$STAGED_FILES" = "" ]]; then

  exit 0

fi

 

PASS=true

 

# Check for phpcs

which ./vendor/bin/phpcs &> /dev/null

if [[ "$?" == 1 ]]; then

  echo "Please install PHPCS"

  exit 1

fi

 

RULESET=./phpcs.xml

 

for FILE in $STAGED_FILES

do

  ./vendor/bin/phpcs --standard="$RULESET" "$FILE"

 

  if [[ "$?" == 0 ]]; then

    echo "PHPCS Passed: $FILE"

  else

    echo "PHPCS Failed: $FILE"

    PASS=false

  fi

done

 

if ! $PASS; then

  echo "COMMIT FAILED"

  exit 1

fi


PHP Code sniffer tools


Install with composer

composer require "squizlabs/php_codesniffer=*"


php vendor/bin/phpcs --standard=PSR2 <file-name>

php vendor/bin/phpcs --standard=phpcs.xml <file-name>


PHP Code Beautifier and Fixer

php vendor/bin/phpcbf --standard=PSR2 <file-name>

php vendor/bin/phpcbf --standard=phpcs.xml <file-name>


php vendor/bin/phpcs --version


You can set rule at phpcs.xml in the project’s root directory

 

Code Snippets

 <ruleset name="PSR2">    

    <description>The PSR2 coding standard.</description>    

    <rule ref="PSR2"/>     

    <file>app/</file>

    <file>test/</file>     

    <exclude-pattern>vendor</exclude-pattern>    

    <exclude-pattern>reports</exclude-pattern>    

</ruleset>


Using Git hook commit-msg we can restrict developers to add commit messages except for the defined pattern at the time of commit any changes.

 

.git/hooks/commit-msg


Code Snippets

shopt -s nocasematch


INPUT_FILE=$1

START_LINE=`head -n1 $INPUT_FILE`

REF=$(git rev-parse --abbrev-ref HEAD)

PATTERN="^(.*) in ${REF}$|si"


if ! [[ "$START_LINE" =~ $PATTERN ]]; then

  echo "COMMIT FAILED"

  exit 1

else

  echo "COMMIT SUCCEEDED"

fi


Another Pattern

PATTERN="^This is Prasun. Code committed for EAT-[0-9]{4} - (.*)$"


Used version: 

  • Git version: 2.31.1.windows.1

  • Composer: 2.1.3

  • PHP_CodeSniffer version: 3.6.0 (stable)

  • PHP: 7.3.8

"Remember, Learning is a journey, not a destination." - Prasun Das

Comments

Popular posts from this blog

Coding best practices - Part 3

Coding best practices - Part 2

Coding best practices - Part 1