├── README.md ├── github-functions.sh ├── gitlab-functions.sh ├── git-cmd-helpers-usage-demo.bash ├── git-functions.sh ├── bitbucket-functions.sh └── git-aliases.sh /README.md: -------------------------------------------------------------------------------- 1 | # git-cmd-helpers 2 | 3 | ## What is this? 4 | a bundle of aliases/functions wrapping some commonly used/useful git command using some humanize,directviewing name 5 | 6 | ## How to use? 7 | 1. `git clone git@github.com:rainchen/git-cmd-helpers.git` 8 | 2. edit `~/.bash_profile`, append: 9 | `for f in ~/git-cmd-helpers/*.sh; do source $f; done` 10 | 3. reload the bash_profile: `source ~/.bash_profile` or open new terminal window 11 | 12 | ## Usage Demo 13 | 14 | [git-cmd-helpers-usage-demo.bash](git-cmd-helpers-usage-demo.bash) 15 | 16 | ## Summary for each file 17 | * `git-aliases.sh`: some `alias` wrapping some commonly used/useful git command using humanize,directviewing name 18 | * `git-functions.sh`: commands wrapping using `function`, support arguments 19 | * `github-functions.sh`: some shortcuts for accessing github.com 20 | * `gitlab-functions.sh`: some shortcuts for accessing a gitlab website 21 | * `bitbucket-functions.sh`: some shortcuts for accessing a bitbucket website 22 | 23 | ## Other popular git helpers 24 | 25 | * `git-autocomplete`: this is a *MUST INSTALL* helper, bash-completion contains git-autocomplete, install: `brew install bash-completion` 26 | * [hub](https://hub.github.com/): hub is a command-line wrapper for git that makes you better at GitHub. 27 | * [github-gem](https://github.com/defunkt/github-gem): `github` command line helper for simplifying your GitHub experience. 28 | -------------------------------------------------------------------------------- /github-functions.sh: -------------------------------------------------------------------------------- 1 | # open github page for current repo 2 | function github-open { 3 | github_repo_url=`git ls-remote --get-url` 4 | github_user=`echo $github_repo_url|cut -d : -f 2|cut -d / -f 1` 5 | github_repo=`echo $github_repo_url|cut -d : -f 2|cut -d / -f 2|cut -d . -f 1` 6 | current_branch=`git rev-parse --abbrev-ref HEAD` 7 | 8 | github_pr_url="https://github.com/${github_user}/${github_repo}" 9 | open $github_pr_url 10 | } 11 | 12 | 13 | # use current branch to send pull request on github 14 | # will open url like: 15 | # "https://github.com/${github_user}/${github_repo}/compare/${base_branch}...${current_branch}" 16 | # example for using develop as base branch: $ github-send-pull-request develop 17 | function github-send-pull-request { 18 | default_base_branch="master" 19 | base_branch=$1 20 | # use default_base_branch 21 | if [[ $base_branch == "" ]]; then 22 | base_branch=$default_base_branch 23 | fi 24 | 25 | github_repo_url=`git ls-remote --get-url` 26 | github_user=`echo $github_repo_url|cut -d : -f 2|cut -d / -f 1` 27 | github_repo=`echo $github_repo_url|cut -d : -f 2|cut -d / -f 2|cut -d . -f 1` 28 | current_branch=`git rev-parse --abbrev-ref HEAD` 29 | 30 | github_pr_url="https://github.com/${github_user}/${github_repo}/compare/${base_branch}...${current_branch}" 31 | # echo $github_pr_url 32 | open $github_pr_url 33 | } 34 | 35 | alias github-send-pull-request-to-develop='github-send-pull-request develop' 36 | -------------------------------------------------------------------------------- /gitlab-functions.sh: -------------------------------------------------------------------------------- 1 | # open gitlab page for current repo 2 | function gitlab-open { 3 | gitlab_repo_url=`git ls-remote --get-url` # git@git.example.com:repo-name.git 4 | gitlab_user=`echo $gitlab_repo_url|cut -d @ -f 1` 5 | gitlab_host=`echo $gitlab_repo_url|cut -d : -f 1|cut -d @ -f 2` 6 | gitlab_repo=`echo $gitlab_repo_url|cut -d : -f 2|cut -d . -f 1` 7 | gitlab_web_url="http://${gitlab_host}/${gitlab_repo}" 8 | open $gitlab_web_url 9 | } 10 | 11 | 12 | # use current branch to send pull request on gitlab website 13 | # will open url like: 14 | # # http://gitlab.exapmle.com/repo-name/merge_requests/new?merge_request[source_branch]=current_branch&merge_request[target_branch]=develop&merge_request[title]=title" 15 | # example for using develop as base branch: $ gitlab-send-merge-request develop 16 | function gitlab-send-merge-request { 17 | default_target_branch="master" 18 | target_branch=$1 19 | # use default_target_branch for target_branch 20 | if [[ $target_branch == "" ]]; then 21 | target_branch=$default_target_branch 22 | fi 23 | 24 | gitlab_repo_url=`git ls-remote --get-url` # git@git.example.com:repo-name.git 25 | gitlab_user=`echo $gitlab_repo_url|cut -d @ -f 1` 26 | gitlab_host=`echo $gitlab_repo_url|cut -d : -f 1|cut -d @ -f 2` 27 | gitlab_repo=`echo $gitlab_repo_url|cut -d : -f 2|cut -d . -f 1` 28 | gitlab_web_url="http://${gitlab_host}/${gitlab_repo}" 29 | 30 | current_branch=`git rev-parse --abbrev-ref HEAD` 31 | 32 | # String.titleize(){ 33 | # echo "$1" | sed -E -e "s/-|_/ /g" -e 's/\b(.)/\U\1/g' 34 | # } 35 | # gitlab_mr_title=`String.titleize $current_branch` 36 | 37 | git_last_commit_message=`git log -1 --pretty=%B` 38 | gitlab_mr_title=$git_last_commit_message 39 | 40 | gitlab_mr_url="${gitlab_web_url}/merge_requests/new?merge_request[source_branch]=${current_branch}&merge_request[target_branch]=${target_branch}&merge_request[title]=${gitlab_mr_title}" 41 | open "$gitlab_mr_url" 42 | } 43 | alias gitlab-send-merge-request-to-develop='gitlab-send-merge-request develop' 44 | -------------------------------------------------------------------------------- /git-cmd-helpers-usage-demo.bash: -------------------------------------------------------------------------------- 1 | # usage demo for https://github.com/rainchen/git-cmd-helpers 2 | 3 | # create new branch 4 | gb # list local branches 5 | git-new-branch "demo: Create a new Branch" # create a branch with valid format "demo-create-a-new-branch" 6 | gbv # list local branches with last commit 7 | 8 | --- 9 | 10 | # commit a new file 11 | echo use git-new-branch to create a new branch > CHANGES 12 | git add CHANGES 13 | gits # git status 14 | git commit -am "add CHANGES" 15 | gll # show git last log 16 | 17 | --- 18 | 19 | # push current branch to remote 20 | gbr # list remote branches 21 | git-push-current-branch-to-remote 22 | gbr # should see a new remote branch 23 | 24 | --- 25 | 26 | # send a new pull request on GitHub 27 | github-send-pull-request 28 | # github-send-pull-request-to-develop # or using "develop" as base branch 29 | github-open # open current repo's github repo url using web browser 30 | 31 | # or send a new merge request on GitLab 32 | gitlab-send-merge-request 33 | gitlab-open 34 | 35 | # or send a new pull request on Bitbucket 36 | bitbucket-send-pull-request 37 | bitbucket-open 38 | 39 | --- 40 | 41 | # undo last commit 42 | git-undo-last-commit 43 | 44 | # fix last commit message or files 45 | git commit --amend # # strongly recommend to use GitX to amend changes 46 | # check Amend and commit 47 | gits # you should see: 48 | # Your branch and 'origin/demo-create-a-new-branch' have diverged, 49 | # and have 1 and 1 different commit each, respectively. 50 | git push -f # force push your feature branch, BUT DONT DO THIS ON YOUR BASE BRANCH 51 | 52 | --- 53 | 54 | # merge the feature branch into master but not deleting the feature branch on GitHub/GitLab/Bitbucket 55 | 56 | --- 57 | 58 | # delete a remote branch after PR get merged 59 | 60 | gbr # list remote branches 61 | git-delete-remote demo-create-a-new-branch 62 | # - [deleted] demo-create-a-new-branch 63 | gbr # confirm it has been deleted 64 | 65 | --- 66 | 67 | # delete all branches that are already merged 68 | gb # check local branches 69 | git-checkout-master 70 | git-up # fetch and rebase local branch 71 | git-delete-local-branches-deleted-from-remote 72 | gb # confirm local are clean 73 | 74 | --- 75 | 76 | # remove remote branches that have been merged into master 77 | git-new-branch "new-master" 78 | git-push-current-branch-to-remote 79 | gbr 80 | git-delete-remote-branches-mereged-into-master 81 | gbr 82 | 83 | --- 84 | 85 | # revert commit 86 | git log 87 | # backup master 88 | git-new-branch "master-backup" 89 | git-commits-first # get first commit hash, e.g.: 90 | # a05dce35923a5e960a3cb258a670b34b43bf07ae 91 | git-revert-to a05dce35923a5e960a3cb258a670b34b43bf07ae 92 | git log 93 | git push -f # override your remote branch 94 | 95 | --- 96 | 97 | # resolve conflict 98 | 99 | # step 1: change README in feature1 branch 100 | git-new-branch feature1 101 | echo add feature1 > README.md 102 | git commit -am "add feature1" 103 | 104 | 105 | # step 2: change README in master branch 106 | git checkout master 107 | echo add feature2 > README.md 108 | git commit -am "add feature2" 109 | 110 | # step 3: create conflict: merge master with feature1 branch 111 | git merge feature1 112 | # should see: 113 | # CONFLICT (content): Merge conflict in README.md 114 | # Automatic merge failed; fix conflicts and then commit the result. 115 | 116 | # use opendiff as merge.tool 117 | git config --global merge.tool opendiff 118 | 119 | # call mergetool to fix conflicts 120 | git mergetool # git will run opendiff to assist you to resolve conflicts, press cmd+s & cmd+q in opendiff to quit 121 | # Normal merge conflict for 'README.md': 122 | # {local}: modified file 123 | # {remote}: modified file 124 | 125 | # after resolving conflicts, commit the changes 126 | git commit 127 | # [master f497858] Merge branch 'feature1' 128 | -------------------------------------------------------------------------------- /git-functions.sh: -------------------------------------------------------------------------------- 1 | # here are commands wrapping using `function`, support arguments 2 | 3 | # ==== for git checkout ==== 4 | 5 | function git-checkout-remote { git checkout -b $1 origin/$1; } 6 | 7 | function git-delete-remote { git push --delete origin $1; } 8 | 9 | function git-fetch-remote { git fetch origin :$1; git checkout $1; } 10 | 11 | 12 | 13 | 14 | # ==== for git remote ==== 15 | 16 | # git remote add origin git@example.com:myrepo 17 | function git-remote-add { git remote add $1 $2; }; 18 | function git-remote-add-origin { git remote add origin $1; }; 19 | 20 | # How to change a remote repository URI using Git? (http://stackoverflow.com/questions/2432764/how-to-change-a-remote-repository-uri-using-git) 21 | # git remote set-url origin git://new.url.here 22 | function git-remote-set-url-origin { git remote set-url origin $1; }; 23 | 24 | 25 | 26 | 27 | # ==== for git branch ==== 28 | 29 | # convert a string to parameterize format 30 | # for example: 31 | # $ String.parameterize ""this s a LongString" 32 | # this-s-a-longstring 33 | # requirement: ruby gem active_support 34 | function String.parameterize (){ 35 | ruby -e "require 'active_support/core_ext/string'; puts '$1'.parameterize;" 36 | } 37 | 38 | # usage example: 39 | # $ git-new-branch "56856748 daily statistics for count of leads for each sales_person" 40 | # Switched to a new branch '56856748-daily-statistics-for-count-of-leads-for-each-sales_person' 41 | function git-new-branch { 42 | String.parameterize "$1" > __tmp__ 43 | cat __tmp__ |xargs -L1 git checkout -b 44 | rm __tmp__ 45 | } 46 | 47 | 48 | 49 | 50 | # ==== for git push ==== 51 | 52 | function git-push-current-branch-to-remote { 53 | branch_name=`git-current-branch` 54 | git push origin $branch_name 55 | git branch --set-upstream-to=origin/$branch_name $branch_name 56 | } 57 | 58 | # tips: git push to different remote branch 59 | # git push : 60 | 61 | # push local "test" to remote "master" 62 | # $ git push origin test:master 63 | # git-push-local-to-remote test master 64 | function git-push-local-to-remote { 65 | git push origin $1:$2 66 | } 67 | 68 | 69 | 70 | 71 | # ==== for git config ==== 72 | 73 | function git-author { 74 | gituser=`git config user.name` 75 | gitemail=`git config user.email` 76 | echo "$gituser <$gitemail>" 77 | } 78 | 79 | 80 | 81 | 82 | # ==== for git tag ==== 83 | 84 | # delete a tag from local 85 | function git-tag-delete-local { git tag -d $1; }; 86 | 87 | # delete a tag from remote 88 | # function git-tag-delete-remote { git push origin :refs/tags/$1; }; 89 | function git-tag-delete-remote { git push origin :$1; }; 90 | 91 | 92 | # ==== for git commit ==== 93 | 94 | # delete a commit and rebase the branch 95 | # usage: git-commits-delete 96 | function git-commits-delete { 97 | local target_commit_id=$1 98 | if [[ $target_commit_id == '' ]]; then 99 | echo "Please pass the commit id" 100 | else 101 | local commit_info=`git log --pretty=oneline | grep $target_commit_id` 102 | local commit_num=`git log --pretty=oneline | grep -n $target_commit_id | cut -d: -f 1` 103 | # echo "target_commit_id:$target_commit_id, commit_num: $commit_num" 104 | if [[ $commit_num == '' ]]; then 105 | echo "Not found commit for $target_commit_id" 106 | else 107 | echo "Going to remove: $commit_info" 108 | local pre_commit_num=`expr $commit_num - 1` 109 | local current_branch=`git-current-branch` 110 | local confirm_message="This will rebase your current branch \"$current_branch\", are you sure?(type 'y' to continue, any else to abort): " 111 | read -p "$confirm_message" -n 1 -r 112 | echo # move to a new line 113 | if [[ $REPLY =~ ^[Yy]$ ]]; then 114 | git rebase --onto $current_branch~$commit_num $current_branch~$pre_commit_num $current_branch 115 | echo "Done" 116 | else 117 | echo "Aborted" 118 | fi 119 | fi 120 | fi 121 | } 122 | -------------------------------------------------------------------------------- /bitbucket-functions.sh: -------------------------------------------------------------------------------- 1 | # open bitbucket page for current repo 2 | function bitbucket-open { 3 | bitbucket_repo_url=`git ls-remote --get-url` 4 | bitbucket_user=`echo $bitbucket_repo_url|cut -d : -f 2|cut -d / -f 1` 5 | bitbucket_repo=`echo $bitbucket_repo_url|cut -d : -f 2|cut -d / -f 2|cut -d . -f 1` 6 | current_branch=`git rev-parse --abbrev-ref HEAD` 7 | 8 | bitbucket_pr_url="https://bitbucket.org/${bitbucket_user}/${bitbucket_repo}" 9 | open $bitbucket_pr_url 10 | } 11 | 12 | # create a new branch which name is using dasherize style, e.g.: "the-branch-name 13 | # usage example: 14 | # $ bitbucket-new-branch "MyProject-1234 [docs] fix typos in README." 15 | # Switched to a new branch 'MyProject-1234-docs-fix-typos-in-readme' 16 | # keeping the begging "MyProject-1234" unchanged because Jira will use it reference to Bitbucket 17 | # more naming examples: 18 | # $ bitbucket-new-branch "[docs] fix typos in README." 19 | # $ docs-fix-typos-in-readme 20 | # $ bitbucket-new-branch "[refactor]log 放入logger里面" 21 | # $ refactor-log-logger 22 | function bitbucket-new-branch { 23 | 24 | # conver input to a dasherize style branch name 25 | function format_branch_name { 26 | local input=$1 27 | # convert none-alpha to "-", keep one "-" only, remove first "-", remove last "-" 28 | local branch_name=` echo $input | sed -e 's/[^a-zA-Z0-9]/-/g' -e 's/--*/-/g' -e 's/^-//' -e 's/-$//'` 29 | local project_key="" 30 | if [[ $branch_name =~ ^[A-Za-z]+-[0-9]+.+ ]]; then 31 | branch_name=` echo $branch_name | sed -e 's/\(^[A-Za-z]*-[0-9]*\)\(.*\)/\1:\2/' ` 32 | project_key=` echo $branch_name | cut -d : -f 1 ` 33 | branch_name=` echo $branch_name | cut -d : -f 2 ` 34 | fi 35 | # conver branch_name to lowercase, BSD version of sed doesn't support "\L" but GNU version works 36 | branch_name=` echo $branch_name | sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' ` 37 | branch_name="$project_key$branch_name" 38 | echo $branch_name 39 | } 40 | 41 | function confirm_branch_name { 42 | local branch_name=$1 43 | echo -n "Going to create a branch \"$branch_name\", ok? [y/n]: " 44 | read answer 45 | if [ "$answer" != "${answer#[Yy]}" ] ;then 46 | create_branch $branch_name 47 | else 48 | ask_for_branch_name 49 | fi 50 | } 51 | 52 | function ask_for_branch_name { 53 | echo -n "Please enter a new branch name using dasherize style, e.g.: \"the-branch-name\" (blank to abort): " 54 | read answer 55 | if [[ $answer = *[!\ ]* ]]; then 56 | create_branch $answer 57 | else 58 | echo "Aborted" 59 | fi 60 | } 61 | 62 | function create_branch { 63 | local branch_name=$1 64 | git checkout -b $branch_name 65 | } 66 | 67 | # main 68 | local input=$1 69 | branch_name=` format_branch_name \"$input\" ` 70 | confirm_branch_name $branch_name 71 | } 72 | 73 | function bitbucket-push-current-branch { 74 | current_branch=`git rev-parse --abbrev-ref HEAD` 75 | git push -u origin $current_branch 76 | } 77 | 78 | function bitbucket-delete-remote-branch { 79 | git push --delete origin $1; 80 | } 81 | 82 | # use current branch to send pull request on bitbucket 83 | # will open url like: 84 | # "https://bitbucket.org/${bitbucket_user}/${bitbucket_repo}/pull-requests/new?source=${current_branch}&dest=${base_branch}" 85 | # example for using develop as base branch: $ bitbucket-send-pull-request develop 86 | function bitbucket-send-pull-request { 87 | default_base_branch="master" 88 | base_branch=$1 89 | # use default_base_branch 90 | if [[ $base_branch == "" ]]; then 91 | base_branch=$default_base_branch 92 | fi 93 | 94 | bitbucket_repo_url=`git ls-remote --get-url` 95 | bitbucket_user=`echo $bitbucket_repo_url|cut -d : -f 2|cut -d / -f 1` 96 | bitbucket_repo=`echo $bitbucket_repo_url|cut -d : -f 2|cut -d / -f 2|cut -d . -f 1` 97 | current_branch=`git rev-parse --abbrev-ref HEAD` 98 | 99 | bitbucket_pr_url="https://bitbucket.org/${bitbucket_user}/${bitbucket_repo}/pull-requests/new?source=${current_branch}&dest=${base_branch}" 100 | echo "Prepare sending Pull Request from ${current_branch} to ${base_branch}" 101 | echo "$bitbucket_pr_url" 102 | open $bitbucket_pr_url 103 | } 104 | 105 | alias bitbucket-send-pull-request-to-develop='bitbucket-send-pull-request develop' 106 | -------------------------------------------------------------------------------- /git-aliases.sh: -------------------------------------------------------------------------------- 1 | # here are some alias wrapping some commonly used/useful git command using humanize,directviewing name 2 | 3 | # ==== for git clone ==== 4 | # copy only the latest revision in the repository, userful for trying opensource project 5 | alias git-shallow-clone='git clone --depth 1' 6 | # to backfill history: git pull --unshallow 7 | 8 | # fetch and rebase all locally-tracked remote branches 9 | alias git-up='git pull --rebase --autostash' 10 | # add an git alias: `git up` 11 | alias git-up-alias='git config --global alias.up "pull --rebase --autostash"' 12 | 13 | # ==== for git branch ==== 14 | alias gb='git branch' 15 | alias gbv='git branch -v' 16 | alias gbr='git branch -r' 17 | 18 | alias git-current-branch='git rev-parse --abbrev-ref HEAD' 19 | # In Git 1.8.1 you can use the git symbolic-ref command with the "--short" option: 20 | # alias git-current-branch='git symbolic-ref --short HEAD' 21 | 22 | # usage: git-delete-remote-branch the_remote_branch_name 23 | alias git-delete-remote-branch='git push origin --delete' 24 | 25 | # http://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-are-already-merged 26 | # To delete all branches that are already merged into the currently checked out branch(skip master and develop): 27 | alias git-delete-remote-branches-merged-into-current='git branch --merged | grep -v "\*" | grep -v master | grep -v develop | xargs -n 1 git branch -d' 28 | alias git-delete-local-branches-deleted-from-remote="git fetch --all -p; git branch -vv | grep ': gone]' | awk '{ print \$1 }' | xargs -n 1 git branch -d" 29 | 30 | # show all remote branches that have already been merged into master 31 | alias git-remote-branches-merged='git branch -r --merged' 32 | 33 | # Using git-sweep you can safely remove remote branches that have been merged into master. 34 | # https://github.com/arc90/git-sweep 35 | # install: pip install git-sweep || easy_install git-sweep 36 | # $ git-sweep cleanup 37 | # after clean up, Tell everyone to run `git fetch --prune` to sync with this remote. 38 | alias git-delete-remote-branches-mereged-into-master='git-sweep cleanup --skip develop' 39 | alias git-delete-remote-branches-mereged-into-develop='git-sweep cleanup --master develop --skip master' 40 | 41 | # git branch -m 42 | # If you want to rename the current branch, you can simply do: 43 | # git branch -m 44 | alias git-branch-rename-to='git branch -m ' 45 | 46 | # show all remote branches, with branch name, commit date, author name, commit id, commit message 47 | alias git-branch-list-remote="git for-each-ref --format='%(refname:short)|%(committerdate:short) (%(committerdate:relative))|%(authorname)|%(objectname:short)|%(contents:subject)' --sort=committerdate refs/remotes/ | column -t -s '|'" 48 | 49 | # show all local branches, with branch name, commit date, author name, commit id, commit message 50 | alias git-branch-list-local="git for-each-ref --format='%(refname:short)|%(committerdate:short) (%(committerdate:relative))|%(authorname)|%(objectname:short)|%(contents:subject)' --sort=committerdate refs/heads/ | column -t -s '|'" 51 | 52 | 53 | # ==== for git log ==== 54 | # git log graph 55 | # more tips: http://book.git-scm.com/3_reviewing_history_-_git_log.html 56 | alias glg="git log --pretty=format:'%h : %s' --topo-order --graph" 57 | 58 | # show git last log 59 | alias gll='git log -n 1' 60 | 61 | alias git-log-for-author='eval "git log --author=\"`git-author`\""' 62 | alias git-log-oneline='git log --pretty=oneline' 63 | alias git-log-graph='git log --graph --decorate' 64 | 65 | alias git-log-last='git log -n 1' 66 | alias git-log-first='git log --pretty=format:%H|tail -1 | xargs git log' 67 | 68 | # shows commits from start. 69 | alias git-log-reverse="git log --reverse" 70 | 71 | # display git log with browser at http://localhost:1234 72 | alias git-log-webview='git instaweb --local --httpd=webrick' 73 | alias git-log-webview-stop='git instaweb --stop' 74 | 75 | alias git-log-treeview='git log --graph --oneline --all' 76 | alias git-log-treeview-pretty="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" 77 | 78 | # In case you were curious what the different options were: %h = abbreviated commit hash, %x09 = tab (character for code 9), %an = author name, %ad = author date (format respects --date= option), %s = subject. From kernel.org/pub/software/scm/git/docs/git-log.html (PRETTY FORMATS section) 79 | # alias git-log-listview="git log --pretty=format:'%C(yellow)%h|%Cred%ad|%Cblue%an|%Cgreen%d %Creset%s' --date=short | column -ts'|' | less -r" 80 | # https://stackoverflow.com/a/9463536/130353 81 | alias git-log-listview="git log --pretty=format:'%C(auto,yellow)%h%C(auto,magenta) %C(auto,blue)%>(12,trunc)%ad %C(auto,green)%<(12,trunc)%aN%C(auto,reset)%s%C(auto,red)% gD% D' --date=short" 82 | 83 | # this is useful for writting changes logs 84 | # e.g.: git-log-listview-no-merges v0.1..v0.2 --author="MyName" 85 | alias git-log-listview-no-merges="git-log-listview --no-merges" 86 | 87 | # show commit messages(in reverse order) in current branch 88 | # useful for writing changes in a PR 89 | # alias git-changes-in-current-branch="git log --reverse --pretty=format:'%s' $(git merge-base master HEAD)..HEAD" 90 | alias git-changes-in-current-branch="git merge-base master HEAD | xargs -I @ git log --reverse --pretty=format:'%s' @..HEAD" 91 | alias git-changes-in-current-branch-v="git cherry -v master" 92 | 93 | # ==== for git status ==== 94 | alias gs='git status' 95 | alias gits='git status' 96 | 97 | 98 | 99 | 100 | # ==== for git checkout ==== 101 | alias git-update-develop='git checkout develop; git pull' 102 | alias git-update-master='git checkout master ; git pull' 103 | alias git-checkout-develop='git checkout develop' 104 | alias git-checkout-master='git checkout master' 105 | 106 | # create and switch to a new branch 107 | alias git-checkout-b='git checkout -b' 108 | # tips: create a local branch from remote 109 | # git checkout -b test origin/test 110 | 111 | alias git-checkout-deleted='git ls-files -d | xargs git checkout' 112 | 113 | # discard all your modified files 114 | alias git-discard-modified='git ls-files -m | xargs git checkout --' 115 | 116 | 117 | # ==== for git remote config ==== 118 | # http://www.git-scm.com/docs/git-remote 119 | # http://stackoverflow.com/questions/4089430/how-can-i-determine-the-url-that-a-local-git-repo-was-originally-cloned-from/16880000#16880000 120 | alias git-remote-url='git ls-remote --get-url' 121 | alias git-config-get-remote-origin-url='git config --get remote.origin.url' 122 | alias git-config-set-remote-origin-url='echo old remote.origin.url:; git config remote.origin.url; git remote set-url origin' 123 | 124 | 125 | 126 | 127 | # ==== for git rm ==== 128 | alias git-rm-cached='git rm --cached' 129 | 130 | 131 | 132 | 133 | # ==== for git pull/push ==== 134 | alias git-pull='git pull' 135 | alias gitpull='git pull' 136 | alias git-push='git push' 137 | alias gitpush='git push' 138 | 139 | alias git-push-develop='git checkout develop;git push;' 140 | alias git-pull-master='git checkout master; git pull;' 141 | 142 | 143 | 144 | 145 | # ==== for git commit ==== 146 | alias git-last-commit-id='git rev-parse --short HEAD' 147 | # a longer version 148 | # alias git-last-commit-id='git log -1 --pretty=oneline --abbrev-commit|cut -c1-7' 149 | alias git-last-commit-id='git rev-parse HEAD' 150 | alias git-last-commit-id-short='git rev-parse --short HEAD' 151 | 152 | alias git-last-commit-message='git log -1 --pretty=%B' 153 | 154 | alias git-commits-count="git rev-list HEAD | wc -l | awk '{print \$1}'" 155 | 156 | # http://stackoverflow.com/questions/927358/git-undo-last-commit 157 | # Git undo last commit 158 | alias git-undo-last-commit='git reset --soft HEAD^' 159 | 160 | # commits per author 161 | # how many commits you’ve contributed to a project 162 | # http://gitready.com/intermediate/2009/01/22/count-your-commits.html 163 | alias git-commits-top='git shortlog -s -n' 164 | 165 | # Show number of commits by developer 166 | # http://zanshin.net/2012/06/08/showing-git-commit-counts/ 167 | alias git-commits-list="git shortlog | grep -E '^[^ ]'" 168 | 169 | alias git-commits-first="git log --pretty=oneline --reverse | head -1" 170 | alias git-commits-last="git log -1" 171 | alias git-commits-last-time="git log -1 --format=%cd" 172 | 173 | 174 | 175 | 176 | # ==== for git diff ==== 177 | alias git-diff-cached='git diff --cached' 178 | 179 | # some tips 180 | # All new files not yet committed 181 | alias git-diff-new-files-tobecommitted='git diff --name-only --diff-filter=A HEAD' 182 | # 'git diff --name-only --diff-filter=A --cached' # All new files in the index 183 | # 'git diff --name-only --diff-filter=A' # All files that are not staged 184 | 185 | 186 | 187 | # ==== for Untracked files ==== 188 | alias git-list-untracked-files='git ls-files -o --exclude-standard' 189 | alias git-list-staged-untracked-files='git diff --name-only --diff-filter=A HEAD' 190 | alias git-add-untracked-files='git add `git ls-files -o --exclude-standard`; git status' 191 | alias git-del-untracked-files='del `git ls-files -o --exclude-standard`; git status' 192 | alias git-unstage-untracked-files='git reset HEAD `git diff --name-only --diff-filter=A HEAD`; git status' 193 | # http://stackoverflow.com/questions/3801321/git-list-only-untracked-files-also-custom-commands 194 | # NOTICE: this will also delete files marked in .gitignore, e.g.: config/database.yml 195 | # NOT RECOMMEND TO USE 196 | # alias git-clear-untracked-files='git clean -df' 197 | 198 | 199 | 200 | 201 | # ==== for git flow ==== 202 | # brew install git-flow 203 | # https://github.com/nvie/gitflow/wiki/Mac-OS-X 204 | alias git-flow-cheatsheet='open https://danielkummer.github.io/git-flow-cheatsheet/' 205 | alias git-flow-init-push='git flow init;git push -u origin develop;' 206 | 207 | 208 | 209 | 210 | # ==== for git patch ==== 211 | # alias git-patch-build='git diff --no-prefix > patchfile.diff; ls patchfile.diff' 212 | # show the changes which have been staged? 213 | alias git-build-patch='git diff --cached --no-prefix > patchfile.diff; ls patchfile.diff' 214 | 215 | # usage: $ git-apply-patch ../vendors/local_production_patch.diff 216 | alias git-apply-patch='patch -p0 <' 217 | 218 | # Generate a git patch(with committing info) for a specific commit 219 | # usage: git-build-patch-for 220 | alias git-build-patch-for='git format-patch -1' 221 | # apply patch(with committing info) 222 | # usage: git-apply-patch-from 0001-fix-bug.patch 223 | alias git-apply-patch-from='git am <' 224 | 225 | 226 | 227 | # ==== for git tag ==== 228 | # list tags 229 | alias git-tag='git tag' 230 | alias git-tag-ref='git show-ref --tags' 231 | alias git-tag-verbose='git log --oneline --decorate --tags --no-walk' 232 | alias git-tag-with-message='git tag -n1' 233 | # delete all local tags and fetch from remote 234 | alias git-tag-reset-with-remote='git tag -l | xargs git tag -d;git fetch --tags' 235 | # delete local tags deleted from remote 236 | alias git-tag-prune='git fetch --prune origin "+refs/tags/*:refs/tags/*"' 237 | alias git-tag-push-all='git push --tags' 238 | 239 | # get latest tag 240 | alias git-tag-latest-commit-id='git rev-list --tags --max-count=1' 241 | # get latest tag name (Notes: This command returns the "newest" tag even this tag is on another branch) 242 | alias git-tag-latest='git-tag-latest-commit-id | xargs git describe --tags' 243 | alias git-tag-latest-verbose='git-tag-latest-commit-id | xargs git log --oneline --decorate --no-walk' 244 | alias git-tag-latest-checkout='git-tag-latest-verbose; git-tag-latest | xargs -I @ git checkout tags/@ -b @' 245 | 246 | # tips for adding new tag 247 | # $ git tag -a v1.2 -m 'tag: v1.2' 248 | # $ git push origin v1.2 249 | 250 | # tips for editing a tag 251 | # How do I edit an existing tag message in git? 252 | # refs: https://stackoverflow.com/questions/7813194/how-do-i-edit-an-existing-tag-message-in-git 253 | # git tag ^{} -f -m "" 254 | # .e.g.: 255 | # git tag v0.1.0 v0.1.0^{} -f -m "tag: v0.1.0" 256 | # or using: 257 | # git tag -f -a 258 | # This will open an editor with the contents of your old tag message. 259 | 260 | # tips for deleting a tag 261 | # If you have a tag named '12345' then you would just do this: 262 | # git tag -d 12345 # delete from local 263 | # git push origin :refs/tags/12345 # delete from remote 264 | 265 | 266 | 267 | 268 | # ==== for git reset ==== 269 | # http://stackoverflow.com/questions/1616957/how-do-you-roll-back-reset-a-git-repository-to-a-particular-commit 270 | # git reset --hard 271 | alias git-revert-to='git reset --hard ' 272 | 273 | 274 | # tips for how to cancel a remote commit 275 | # useful for changing commit message or other 276 | # 1. git-cancel-commit 277 | # 2. git-push-force 278 | alias 'git-cancel-commit'='git reset --soft HEAD~1' # cancel last commit 279 | alias 'git-push-force'='git push origin HEAD --force' # force to push to remote 280 | 281 | 282 | 283 | 284 | # ==== with vendor tools ==== 285 | 286 | 287 | ## ==== gitx helper for mac ==== 288 | alias gitx-quit="killall GitX" 289 | alias gitx="open /Applications/GitX.app -n" 290 | alias gitx.="open /Applications/GitX.app ." 291 | 292 | 293 | ## ==== for PlistBuddy ==== 294 | # alias update-plist-version-with-git-last-commit='/usr/libexec/PlistBuddy -c "Set :BuildRevision `git log -1 --pretty=oneline --abbrev-commit|cut -c1-7`" ${TARGET_BUILD_DIR}/${INFOPLIST_PATH}' 295 | 296 | 297 | ## ==== for gitstats ==== 298 | # [DEPRECATED] keep for reference only 299 | # gitstats is a statistics generator for git repositories. Currently it produces only HTML output with tables and graphs. 300 | # git://repo.or.cz/gitstats.git 301 | # usage: $ git-stats 302 | # Requirement 'gnuplot': $ brew install gnuplot 303 | # demo: http://gitstats.sourceforge.net/examples/gitstats/ 304 | # alias git-stats="gitstats . git-stats" 305 | 306 | # GitStats is a git repository statistics generator. It browses the repository and outputs html page with statistics. 307 | # https://github.com/tomgi/git_stats 308 | # demo: http://tomgi.github.io/git_stats/examples/rails/general.html 309 | # install git_stats: gem install git_stats 310 | # git_stats usage: 311 | # $ git_stats generate 312 | # $ open git_stats/index.html 313 | alias git-stats='git_stats generate;open git_stats/index.html' 314 | 315 | 316 | ## ==== for resolving conflicts ==== 317 | 318 | # Resolve Git merge conflicts in favor of their changes during a pull 319 | alias git-pull-using-theirs='git pull -s recursive -X theirs' 320 | 321 | # Resolve Git merge conflicts in favor of their changes 322 | # e.g.: git-merge-using-theirs other_branch 323 | alias git-merge-using-theirs='git merge -X theirs' 324 | 325 | 326 | ## ==== for stash ==== 327 | # This command will create a stash with ALL of your changes (staged and unstaged), but will leave the staged changes in your working directory (still in state staged). 328 | alias git-stash-unstaged='git stash --keep-index' 329 | 330 | 331 | ## ==== for gitignore config ==== 332 | 333 | # For more on global gitignores: https://help.github.com/articles/ignoring-files/#create-a-global-gitignore 334 | # $ git config --global core.excludesfile ~/.gitignore_global 335 | 336 | alias gitignore-for-macos='curl https://raw.githubusercontent.com/github/gitignore/master/Global/macOS.gitignore -o macOS.gitignore; ls -lah macOS.gitignore' 337 | --------------------------------------------------------------------------------