ragnarokkrr
4/27/2016 - 8:02 PM

Git

Git

#Git

##Table of Contents

  1. Enhancing Git
  2. Git Subtreeing

##Enhancing Git

###Setup .git_prompt

  1. Create the file by opening terminal and typing nano ~/.git_prompt and paste in:

    NONE="\[\033[0m\]"    # unsets color to term's fg color
    K="\[\033[0;30m\]"    # black
    R="\[\033[0;31m\]"    # red
    DR="\[\033[1;31m\]"   # red
    G="\[\033[0;32m\]"    # green
    Y="\[\033[0;33m\]"    # yellow
    B="\[\033[0;34m\]"    # blue
    M="\[\033[0;35m\]"    # magenta
    C="\[\033[0;36m\]"    # cyan
    W="\[\033[0;37m\]"    # white
    EMK="\[\033[1;30m\]"
    EMR="\[\033[1;31m\]"
    EMG="\[\033[1;32m\]"
    EMY="\[\033[1;33m\]"
    EMB="\[\033[1;34m\]"
    EMM="\[\033[1;35m\]"
    EMC="\[\033[1;36m\]"
    EMW="\[\033[1;37m\]"
    BGK="\[\033[40m\]"
    BGR="\[\033[41m\]"
    BGG="\[\033[42m\]"
    BGY="\[\033[43m\]"
    BGB="\[\033[44m\]"
    BGM="\[\033[45m\]"
    BGC="\[\033[46m\]"
    BGW="\[\033[47m\]"
    
    __git_prompt ()
    {
            local g="$(git rev-parse --git-dir 2>/dev/null)"
            if [ -n "$g" ]; then
                    local r
                    local b
                    if [ -d "$g/../.dotest" ]
                    then
                            if test -f "$g/../.dotest/rebasing"
                            then
                                    r="|REBASE"
                            elif test -f "$g/../.dotest/applying"
                            then
                                    r="|AM"
                            else
                                    r="|AM/REBASE"
                            fi
                            b="$(git symbolic-ref HEAD 2>/dev/null)"
                    elif [ -f "$g/.dotest-merge/interactive" ]
                    then
                            r="|REBASE-i"
                            b="$(cat "$g/.dotest-merge/head-name")"
                    elif [ -d "$g/.dotest-merge" ]
                    then
                            r="|REBASE-m"
                            b="$(cat "$g/.dotest-merge/head-name")"
                    elif [ -f "$g/MERGE_HEAD" ]
                    then
                            r="|MERGING"
                            b="$(git symbolic-ref HEAD 2>/dev/null)"
                    else
                            if [ -f "$g/BISECT_LOG" ]
                            then
                                    r="|BISECTING"
                            fi
                            if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
                            then
                                    if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
                                    then
                                            b="$(cut -c1-7 "$g/HEAD")..."
                                    fi
                            fi
                    fi
    
            local git_status="$(git status 2> /dev/null)"
    
            local remote
            local ahead_pattern="# Your branch is ahead of .* by ([0-9]+) commit"
            local behind_pattern="# Your branch is behind .* by ([0-9]+) commit"
            local diverge_pattern="and have ([0-9]+) and ([0-9]+) different"
            if [[ ${git_status} =~ ${ahead_pattern} ]]; then
                remote="${M}↑${BASH_REMATCH[1]}"
            elif [[ ${git_status} =~ ${behind_pattern} ]]; then
                remote="${Y}↓${BASH_REMATCH[1]}"
            elif [[ ${git_status} =~ ${diverge_pattern} ]]; then
                remote="${M}↑${BASH_REMATCH[1]}${Y}↓${BASH_REMATCH[2]}"
            fi
    
            local state
            if [[ ! ${git_status}} =~ "working directory clean" ]]; then
                state="${R}●"
            fi
            echo "${DR}(${b##refs/heads/}$r$remote$state)${NONE}"
            fi
    }
    
    bash_prompt() {
        UC=$EMG                     # user's color
        [ $UID -eq "0" ] && UC=$R   # root's color
    
        prompt="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\W\[\033[m\] $(__git_prompt)\$ "
        PS1="$prompt"
    }
    PROMPT_COMMAND=bash_prompt
    
  2. Source the new file in ~/.bash_profile (Mac) or ~/.bashrc (Linux):

    . ~/.git_prompt
    
    if [ -f ~/.git-completion.bash ]; then
        . ~/.git-completion.bash
    fi
    
  3. Open a new terminal window, navigate to a project, git init, and enjoy your new fancy colors...

##Git Subtreeing

From here and here

UPDATE: best reference/explanation

Extracting out a subtree from main project:

  1. pushd [dir/to/main/project]
  2. git subtree split -P [path/to/plugin] -b [new branch name]
  3. popd
  4. mkdir [new-repo]
  5. pushd [new-repo]
  6. git init
  7. git pull [path/to/main/project] [new branch name]
  8. git remote add origin [github address for new-repo]
  9. git push origin -u master
  10. popd

Adding a sub-project to main project:

  1. git remote add [plugin] [plugin git address]
  2. git fetch [plugin]
  3. git read-tree --prefix=wp-content/plugins/[plugin] -u [plugin]/master
  4. git commit -m "add plugin subtree"
  5. git push

or

  1. git subtree add --prefix=[path/to/plugin] [plugin repo] master <- BEST

Get update from subtree remote:

  1. git fetch [plugin]
  2. git merge -s subtree --squash [plugin]/master
  3. git commit -m "update the plugin"

or

  1. git pull -s subtree [plugin] master

Updating a subtree from within the main project:

  1. git checkout [plugin_branch]
  2. git merge –squash –s subtree –no-commit master
  3. git commit
  4. git push [plugin_remote] [plugin_branch]

Removing a subtree

  1. git rm -r [path/to/plugin]
  2. git commit -m "remove plugin"

##Git Tagging

###Tagging something in history

git tag -a <tag_name> <commit_hash> -m "Message"