Git cheat sheet #02

DevOps 2020. 1. 15. 17:48

+. git stash

    tomcat@dev ~/forked/gitrepo01 (dev) $ echo "Temp item for stash" > tmp_task01.txt

    tomcat@dev ~/forked/gitrepo01 (dev) $ git add .

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash save tmp_task01

    tomcat@dev ~/forked/gitrepo01 (dev) $ echo "Temp item for stash v2" > tmp_task02.txt

    tomcat@dev ~/forked/gitrepo01 (dev) $ git add . ; git stash save tmp_task02

    tomcat@dev ~/forked/gitrepo01 (dev) $ echo "Temp item for stash v3" > tmp_task03.txt

    tomcat@dev ~/forked/gitrepo01 (dev) $ git add . ; git stash save tmp_task03

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash list

        stash@{0}: On dev: tmp_task03

        stash@{1}: On dev: tmp_task02

        stash@{2}: On dev: tmp_task01

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash show stash@{0}

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash apply stash@{1}

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash drop stash@{1}

    tomcat@dev ~/forked/gitrepo01 (dev) $

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash push -m "comments for stashed files" ./conf_dev01.txt ./conf/conf_dev02.txt           <- stash for multiple files

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash list

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash show stash@{0}

    tomcat@dev ~/forked/gitrepo01 (dev) $ git stash apply stash@{0}

 

 

+. Git tag   (Lightweight tag / Annotated tag)  

$ git init ;

$ date > 1.txt ; git add . ; git commit -m "v1"

$ date > 2.txt ; git add . ; git commit -m "v2"

$ git checkout -b "dev01" ;  date > 3.txt ; git add . ; git commit -m "d1-v3"

$ git checkout master ; date > 4.txt ; git add . ; git commit -m "v4"

 

$ git tag v1.0.1

$ git tag -a 'myApp01' -m 'Main stream before merge'    

$ git lg1 -n 10

$ git tag -l

$ git tag -l -n

$ git merge develop

$ git tag -a "myApp02" -m "Main stream with new feature"

$ git lg1 -n 10

    *   10a0646 - (55 seconds ago) Merge branch 'dev01' - jjung (HEAD -> master, tag: myApp02)

    |\  

    | * 63dd5cd - (6 minutes ago) d1-v3 - jjung (dev01)

    * | c56d570 - (6 minutes ago) v4 - jjung (tag: v1.0.1, tag: myApp01)

    |/  

    * e5a381f - (7 minutes ago) v2 - jjung

    * 7fa7018 - (7 minutes ago) v1 - jjung

$ git tag -l -n

    myApp01         Main stream before merge

    myApp02         Main stream with new feature

    v1.0.1          v4

$ git checkout v1.0.1 ; git checkout master ; git checkout myApp02 ; git checkout master

$ git show v1.0.1

$ git show myApp01

 

$ git tag -d v1.0.1

$ git tag -l -n

    myApp01         Main stream before merge

    myApp02         Main stream with new feature

$ git tag -d 'myApp01'

 

$ git push origin v1.0.2

$ git push origin --tag      >> push all tag to remote

$ git tag -d v1.0.2

$ git push tag :v1.0.2       >> delete remote tag

 

 

+. cleanup old/out-dated remote branches

git remote prune origin

  > https://railsware.com/blog/2014/08/11/git-housekeeping-tutorial-clean-up-outdated-branches-in-local-and-remote-repositories/

 

 

+. git alias

 > git history log view

    $ git config --global alias.lg1 "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"

    $ git config --global alias.lg1 "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%aI)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"

    $ git config --global alias.lg1 "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(white)%s%C(reset) %C(green)(%ad)%C(reset) %C(dim white)<%an>%C(reset)%C(auto)%d%C(reset)' --date=format:'%Y-%m-%d %H:%M:%S' --all"

 

    $ git config --global alias.lg2 "log --oneline"

    $ git config --global alias.lg3 "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n%C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all"

    $ git lg1

    $ git lg1 -n 5

    $ git lg2

    $ git lg2 -n 5

 

 > using meld for git diff

    git config --global alias.meld "difftool -t meld --dir-diff"

    git config --global alias.melds "difftool -t meld --dir-diff --staged"

    git config --global alias.meldh "difftool -t meld --dir-diff HEAD"

    (or)

    meld = difftool -t meld --dir-diff

    meldh = difftool -t meld --dir-diff HEAD

    melds = difftool -t meld --dir-diff --staged

    

 

 > git amend (add to last commit with  staged files)

    git config --global alias.add2last "commit -a --amend -C HEAD"

 

 > git show alias

    aliases =  "!git config -l | grep ^alias" (to be added directly to .gitconfig)

 

+. bash alias for git 

    $ alias tar-src='tar --exclude={.DS_Store,.git,.svn,node_modules,target,*.iml,*.ipr,*.iws,.idea,.settings,.project,.classpath} -cvf '

    $ cat ~/.bashrc | grep tar-src

alias tar-src='tar --exclude={.DS_Store,.git,.svn,node_modules,target,*.iml,*.ipr,*.iws,.idea,.settings,.project,.classpath} -cvf '

    $ tar-src tar-src-test.tar ./tar-src-folder/

 

 

+. bash with git branch name

     > https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt

    $ vi .bashrc

        parse_git_branch() {

             git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

        }

        export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

 

 

+. install git meld for windows

  > install  meld for windows

  > git config --global difftool.meld.path "C:\dev\Meld\Meld.exe"

  > git config --global diff.tool meld

  > git config --global difftool.prompt false

  > git config --global mergetool.meld.path "C:\dev\Meld\Meld.exe"

  > git config --global merge.tool meld

  > git config --global mergetool.prompt true

  > git config --global alias.meld "difftool -t meld --dir-diff"

 

 

+. git search

 > Search by author

   $ git log --author="Donohue" --oneline

 > Search by message

   $ git log --grep='SASS' --oneline

 > Search by file

   $ git log -- "src/main/lincense.txt"

 

 

+. git log graph with single line

 $ git log --graph --oneline

 

 

 

+. gitconfig

        [user]

            name = jjung

            email = jjung@waikato.ac.nz

        [core]

            editor = vi

        [push]

            default = matching

        [alias]

            lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(white)%s%C(reset) %C(green)(%ad)%C(reset) %C(dim white)<%an>%C(reset)%C(auto)%d%C(reset)' --date=format:'%Y-%m-%d %H:%M:%S' --all

            lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n%C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all

            meld = difftool -t meld --dir-diff

            melds = difftool -t meld --dir-diff --staged

            meldh = difftool -t meld --dir-diff HEAD

       [core]

            autocrlf = true            => windows environment (change cr+lf to lf before store)

            autocrlf = input           => linux/unix/mac environment (use lf as line ending)

 

 

+. handling line break : need to define global configuration

   $ git config --global core.autocrlf true   => windows environment (change cr+lf to lf before store)

   $ git config --global core.autocrlf input  => linux/unix/mac environment (use lf as line ending)

   $ git config --global core.autocrlf false  => default option, doesn't convert line-break && not recommended

 

+. dos2unix for converting CR+LF to LF (commit changes first - or will lose your code!)

    $ find . -type f -not -path "./.git/*" -exec grep -Il '.' {} \; | grep -v "./.project" | grep -v "./.classpath" | grep -v "./.mule/"| grep -v "./target/" | xargs dos2unix

      > dos2unix : utility changes CR+LF to LF

      > find ... -type f : find only file type

      > find ... -not path : exclude following path

      > find ... -exec grep -Il '.' {} \;   : exclude binary file

      > | grep -v ... : exclude following results

 

 

+. clean-up cached changes when .gitignore is not working (commit changes first - or will lose your code!)

    $  git rm -r --cached .

    $  git add .

    $  git commit -m "fixed untracked files"

 

 

+. ignore file permission changes (not recommended)

          > git records only two file mode 644 & 755

    $ git config --global core.fileMode false

    $ git -c core.fileMode=false diff     --> one-off commands    

     cf.    

      # update all directory permission to '755'

       find . -type d -not -perm 0755 | grep -v ".git" | grep -v "target"  | xargs chmod 755

      # update all normal files permission to '644'

       find ./ -type f -not -perm 0644 | grep -v ".git" | grep -v "target" | grep -v ".sh" | xargs chmod 644

      # update all script files permission to '755'

        find ./ -type f -not -perm 0755 | grep -v ".git" | grep -v "target" |  grep ".sh" | xargs chmod 755

      # update <cr>+<lf> to <lf>

        find . -type f  | grep -v ".git" | grep -v "target" | xargs dos2unix

 

+. gitignore list

 

 

+. Useful links

 > http://devpools.kr/2017/02/05/%EC%B4%88%EB%B3%B4%EC%9A%A9-git-%EB%90%98%EB%8F%8C%EB%A6%AC%EA%B8%B0-reset-revert/

 

 

 

+. bash with git branch name & git status

----------------------------------------------------------------------------------------------------------------------------

##### git bash prompt ######################

PS_COLOR_RED="\033[31m\\"

PS_COLOR_YELLOW="\033[33m\\"

PS_COLOR_GREEN="\033[32m"

PS_COLOR_OCHRE="\033[95m\\"

PS_COLOR_BLUE="\033[34m\\"

PS_COLOR_WHITE="\033[37m\\"

PS_COLOR_RESET="\e[0m"

# get current branch in git repo

function parse_git_branch() {

    BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`

    if [ ! "${BRANCH}" == "" ]

    then

        STAT=`parse_git_dirty`

        echo " (${BRANCH}${STAT})"

    else

        echo ""

    fi

}

# get current status of git repo

function parse_git_dirty {

    status=`git status 2>&1 | tee`

    dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`

    untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`

    ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`

    newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`

    renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`

    deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`

    bits=''

    if [ "${renamed}" == "0" ]; then

        bits=">${bits}"

    fi

    if [ "${ahead}" == "0" ]; then

        bits="*${bits}"

    fi

    if [ "${newfile}" == "0" ]; then

        bits="+${bits}"

    fi

    if [ "${untracked}" == "0" ]; then

        bits="?${bits}"

    fi

    if [ "${deleted}" == "0" ]; then

        bits="x${bits}"

    fi

    if [ "${dirty}" == "0" ]; then

        bits="!${bits}"

    fi

    if [ ! "${bits}" == "" ]; then

        echo " ${bits}"

    else

        echo ""

    fi

}

 

export PS1="\u@\h \[\033[32m\]\w\[\e[96m\]\$(parse_git_branch)\[\033[00m\] $ "

----------------------------------------------------------------------------------------------------------------------------

 

 

+. git alias advanced configuration - interactive mode using fzf & pygments

----------------------------------------------------------------------------------------------------------------------------

a. install prerequisite applications

    # git clone "https://github.com/junegunn/fzf.git"

    # mv fzf /usr/share/

    # cd /usr/share/fzf ; ./install

      --> check ~/.bashrc && ~/.zshrc  

      --> logout & login (or reload bash/zsh)

        ==> this configuration should be copied to each user

          # cp ./.fzf.* ~tomcat/

          # chown tomcat:tomcat ~tomcat/.fzf.*

          # cp ~tomcat/.bashrc ~tomcat/.bashrc_bak

          # echo "[ -f ~/.fzf.bash ] && source ~/.fzf.bash" >> ~tomcat/.bashrc

          # cp ~tomcat/.zshrc ~tomcat/.zshrc_bak

          # echo "[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh" >> ~tomcat/.zshrc

 

    +. run with cli

       find ./ -name "*" | fzf > selected.txt      -> single selection

       find ./ -name "*" | fzf -m > selected.txt   -> multiple selection (with tab)

 

    # apt install python3-pip

    # apt install python-pygments

       ==> python3-pip & python-pygments are used to preview screen

 

b. ~/.gitconfig

    al = "!# Prints all aliases.;\n\

        git config --list \

        | egrep '^alias.+' \

        | sed -e 's/^alias\\.//' \

        | column -t -s'=' \

        | sed 's/!#* *//; s/;$//' \

        | cut -c1-85"

    st = status

     sts = status -s

    b0 = "!git branch | awk '/^\\*/{print $2}'"

    backup = "!git branch backup-`git b0`"

    lp = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit

    ch = "!git checkout $(git bselect)"

    bselect = "! # select branch with preview; \n\

        f() { \

            _height=$(stty size | awk '{print $1}');\

            git branch | fzf --preview \"git lp {1} | head -n $_height\"; \

        }; f"

    af = "!git diff-select | xargs git add"

    diff-select = "! # add files with fzf preview diffs; \n\

        f() { \

            _height=$(stty size | awk '{print $1}');\

            git diff-info \

            | fzf -m --header \"$(git diff --shortstat)\" --preview \

                \"if [[ {1} == '??' ]]; then cat {3}; else git diff {3}; fi \

                | head -n $_height \

                | pygmentize\" \

            | awk '{print $3}'; \

        }; f"

    diff-info = "! # get diff info;\n\

        fileA=/tmp/git-s-$(uuidgen); \

        fileB=/tmp/git-diff-$(uuidgen); \

        git sts | awk '{print $2,$1}' > $fileA; \

        git diff --numstat | awk '{print $3,$1,$2}' > $fileB; \

        join -t' ' -a 1 $fileA $fileB | awk '{print $2, \"(+\"$3 \",-\"$4\")\", $1}' | sed 's/(+,-)/./' | column -t -s' ' ; \

        rm -f $fileA $fileB; \

    "

 

 

'DevOps' 카테고리의 다른 글

Ping Test for Tomcat AJP connector  (0) 2020.01.15
GitLab with Docker Image official guide (GitLab)  (0) 2020.01.15
docker cli #01  (0) 2020.01.15
Ansible quick start  (0) 2020.01.15
Git cheat sheet #01  (0) 2020.01.15
블로그 이미지

Melting

,