Git
#Git
##Table of Contents
###Setup .git_prompt
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
Source the new file in ~/.bash_profile
(Mac) or ~/.bashrc
(Linux):
. ~/.git_prompt
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
Open a new terminal window, navigate to a project, git init
, and enjoy your new fancy colors...
UPDATE: best reference/explanation
Extracting out a subtree from main project:
pushd [dir/to/main/project]
git subtree split -P [path/to/plugin] -b [new branch name]
popd
mkdir [new-repo]
pushd [new-repo]
git init
git pull [path/to/main/project] [new branch name]
git remote add origin [github address for new-repo]
git push origin -u master
popd
Adding a sub-project to main project:
git remote add [plugin] [plugin git address]
git fetch [plugin]
git read-tree --prefix=wp-content/plugins/[plugin] -u [plugin]/master
git commit -m "add plugin subtree"
git push
or
git subtree add --prefix=[path/to/plugin] [plugin repo] master
<- BESTGet update from subtree remote:
git fetch [plugin]
git merge -s subtree --squash [plugin]/master
git commit -m "update the plugin"
or
git pull -s subtree [plugin] master
Updating a subtree from within the main project:
git checkout [plugin_branch]
git merge –squash –s subtree –no-commit master
git commit
git push [plugin_remote] [plugin_branch]
Removing a subtree
git rm -r [path/to/plugin]
git commit -m "remove plugin"
##Git Tagging
###Tagging something in history
git tag -a <tag_name> <commit_hash> -m "Message"