e8609b857caf730c626ab27910ece30420bc6693

Author
Ujwal Dhakal <kevin.ujwal@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

git: add `grename` to rename a local branch and in the origin remote (#8622)

Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/git/README.md b/plugins/git/README.md
  2index 42a583de89965d73b8e063f2cc35f42dc959bec6..009a740165a8cbbd3cbab3908ef6c46e1aa0df54 100644
  3--- a/plugins/git/README.md
  4+++ b/plugins/git/README.md
  5@@ -10,195 +10,196 @@ plugins=(... git)
  6 
  7 ## Aliases
  8 
  9-| Alias                | Command                                                                                                                       |
 10-|:---------------------|:------------------------------------------------------------------------------------------------------------------------------|
 11-| g                    | git                                                                                                                           |
 12-| ga                   | git add                                                                                                                       |
 13-| gaa                  | git add --all                                                                                                                 |
 14-| gapa                 | git add --patch                                                                                                               |
 15-| gau                  | git add --update                                                                                                              |
 16-| gav                  | git add --verbose                                                                                                             |
 17-| gap                  | git apply                                                                                                                     |
 18-| gb                   | git branch                                                                                                                    |
 19-| gba                  | git branch -a                                                                                                                 |
 20-| gbd                  | git branch -d                                                                                                                 |
 21+| Alias                | Command                                                                                                                          |
 22+|:---------------------|:---------------------------------------------------------------------------------------------------------------------------------|
 23+| g                    | git                                                                                                                              |
 24+| ga                   | git add                                                                                                                          |
 25+| gaa                  | git add --all                                                                                                                    |
 26+| gapa                 | git add --patch                                                                                                                  |
 27+| gau                  | git add --update                                                                                                                 |
 28+| gav                  | git add --verbose                                                                                                                |
 29+| gap                  | git apply                                                                                                                        |
 30+| gb                   | git branch                                                                                                                       |
 31+| gba                  | git branch -a                                                                                                                    |
 32+| gbd                  | git branch -d                                                                                                                    |
 33 | gbda                 | git branch --no-color --merged \| command grep -vE "^(\+|\*\|\s*(master\|develop\|dev)\s*$)" \| command xargs -n 1 git branch -d |
 34-| gbD                  | git branch -D                                                                                                                 |
 35-| gbl                  | git blame -b -w                                                                                                               |
 36-| gbnm                 | git branch --no-merged                                                                                                        |
 37-| gbr                  | git branch --remote                                                                                                           |
 38-| gbs                  | git bisect                                                                                                                    |
 39-| gbsb                 | git bisect bad                                                                                                                |
 40-| gbsg                 | git bisect good                                                                                                               |
 41-| gbsr                 | git bisect reset                                                                                                              |
 42-| gbss                 | git bisect start                                                                                                              |
 43-| gc                   | git commit -v                                                                                                                 |
 44-| gc!                  | git commit -v --amend                                                                                                         |
 45-| gcn!                 | git commit -v --no-edit --amend                                                                                               |
 46-| gca                  | git commit -v -a                                                                                                              |
 47-| gca!                 | git commit -v -a --amend                                                                                                      |
 48-| gcan!                | git commit -v -a --no-edit --amend                                                                                            |
 49-| gcans!               | git commit -v -a -s --no-edit --amend                                                                                         |
 50-| gcam                 | git commit -a -m                                                                                                              |
 51-| gcsm                 | git commit -s -m                                                                                                              |
 52-| gcb                  | git checkout -b                                                                                                               |
 53-| gcf                  | git config --list                                                                                                             |
 54-| gcl                  | git clone --recurse-submodules                                                                                                |
 55-| gclean               | git clean -id                                                                                                                 |
 56-| gpristine            | git reset --hard && git clean -dfx                                                                                            |
 57-| gcm                  | git checkout master                                                                                                           |
 58-| gcd                  | git checkout develop                                                                                                          |
 59-| gcmsg                | git commit -m                                                                                                                 |
 60-| gco                  | git checkout                                                                                                                  |
 61-| gcount               | git shortlog -sn                                                                                                              |
 62-| gcp                  | git cherry-pick                                                                                                               |
 63-| gcpa                 | git cherry-pick --abort                                                                                                       |
 64-| gcpc                 | git cherry-pick --continue                                                                                                    |
 65-| gcs                  | git commit -S                                                                                                                 |
 66-| gd                   | git diff                                                                                                                      |
 67-| gdca                 | git diff --cached                                                                                                             |
 68-| gdcw                 | git diff --cached --word-diff                                                                                                 |
 69-| gdct                 | git describe --tags $(git rev-list --tags --max-count=1)                                                                      |
 70-| gds                  | git diff --staged                                                                                                             |
 71-| gdt                  | git diff-tree --no-commit-id --name-only -r                                                                                   |
 72-| gdv                  | git diff -w $@ \| view -                                                                                                      |
 73-| gdw                  | git diff --word-diff                                                                                                          |
 74-| gf                   | git fetch                                                                                                                     |
 75-| gfa                  | git fetch --all --prune                                                                                                       |
 76-| gfg                  | git ls-files \| grep                                                                                                          |
 77-| gfo                  | git fetch origin                                                                                                              |
 78-| gg                   | git gui citool                                                                                                                |
 79-| gga                  | git gui citool --amend                                                                                                        |
 80-| ggf                  | git push --force origin $(current_branch)                                                                                     |
 81-| ggfl                 | git push --force-with-lease origin $(current_branch)                                                                          |
 82-| ggl                  | git pull origin $(current_branch)                                                                                             |
 83-| ggp                  | git push origin $(current_branch)                                                                                             |
 84-| ggpnp                | ggl && ggp                                                                                                                    |
 85-| ggpull               | git pull origin "$(git_current_branch)"                                                                                       |
 86-| ggpur                | ggu                                                                                                                           |
 87-| ggpush               | git push origin "$(git_current_branch)"                                                                                       |
 88-| ggsup                | git branch --set-upstream-to=origin/$(git_current_branch)                                                                     |
 89-| ggu                  | git pull --rebase origin $(current_branch)                                                                                    |
 90-| gpsup                | git push --set-upstream origin $(git_current_branch)                                                                          |
 91-| ghh                  | git help                                                                                                                      |
 92-| gignore              | git update-index --assume-unchanged                                                                                           |
 93-| gignored             | git ls-files -v \| grep "^[[:lower:]]"                                                                                        |
 94-| git-svn-dcommit-push | git svn dcommit && git push github master:svntrunk                                                                            |
 95-| gk                   | gitk --all --branches                                                                                                         |
 96-| gke                  | gitk --all $(git log -g --pretty=%h)                                                                                          |
 97-| gl                   | git pull                                                                                                                      |
 98-| glg                  | git log --stat                                                                                                                |
 99-| glgp                 | git log --stat -p                                                                                                             |
100-| glgg                 | git log --graph                                                                                                               |
101-| glgga                | git log --graph --decorate --all                                                                                              |
102-| glgm                 | git log --graph --max-count=10                                                                                                |
103-| glo                  | git log --oneline --decorate                                                                                                  |
104-| glol                 | git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'                        |
105diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh
106index 380ab76d8a7d806f6542a824b2a9abe46c75637f..ffb3e506ac9e4d0f0b3bae8c98471f4350e3e465 100644
107--- a/plugins/git/git.plugin.zsh
108+++ b/plugins/git/git.plugin.zsh
109@@ -256,3 +256,17 @@ alias glum='git pull upstream master'
110 
111 alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
112 alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]"'
113+
114+function grename() {
115+  if [[ -z "$1" || -z "$2" ]]; then
116+    echo "Usage: $0 old_branch new_branch"
117+    return 1
118+  fi
119+
120+  # Rename branch locally
121+  git branch -m "$1" "$2"
122+  # Rename branch in origin remote
123+  if git push origin :"$1"; then
124+    git push --set-upstream origin "$2"
125+  fi
126+}