DRYな備忘録

Don't Repeat Yourself.

リモートに無いブランチはローカル&トラッキングからも消そうではないか

tl;dr

さいきんは、fetchのついでにします。

git fetch origin --prune

あるいは、目的のことのみしたい場合は、

git remote prune origin

追跡ブランチ

前回エントリで、追跡ブランチ(tracking branch)の削除方法を知りました。

git branch -r -d REMOTE-NAME/BRANCH-NAME

おーし、じゃあリモートに無いブランチはローカルブランチもトラッキングブランチも消すスクリプトおじさん書いちゃうぞー!と思ったらそんな便利コマンド既にあったよねーそりゃあるよねー、って話。

とりあえず

git remote show origin

して、originの状態確認して、

git remote prune origin

すると、REMOTE-NAME=originなレポジトリに無いもので、トラッキングにあるブランチを削除してくれるっぽい

Log

# ローカルブランチの状態を確認
[10:01:34] % git branch
  about518/backup
  about518/develop
* develop
  feature/feedback-ui
  feature/upload-local-file
  master
  refactor

# トラッキングの状態を確認
[10:01:27] % git branch -r
  about518/develop
  about518/feature-judge-chrome-version
  about518/feature/achievement-record
  about518/feature/play-sound-at-alert
  about518/feature/screen-shot
  about518/feature/upload-local-file
  about518/master
  about518/patch/fix-notification-img-bug
  about518/refactor
  origin/HEAD -> origin/master
  origin/develop
  origin/feature-judge-chrome-version
  origin/feature/feedback-ui     # 今回のターゲットはキミに決めた!
  origin/feature/input-tracking
  origin/feature/notification-nyukyo
  origin/feature/testem
  origin/master
  origin/refactor
  zep/develop
  zep/feature-local-storage
  zep/feature-notification
  zep/master

# REMOTE名 'origin' で設定されているリポジトリの状態を確認
[10:03:50] % git remote show origin
* remote origin
  Fetch URL: git@github.com:otiai10/kanColleWidget.git
  Push  URL: git@github.com:otiai10/kanColleWidget.git
  HEAD branch: develop
  Remote branches:
    develop                                 tracked
    feature-judge-chrome-version            tracked
    feature/input-tracking                  tracked
    feature/notification-nyukyo             tracked
    feature/testem                          tracked
    master                                  tracked
    refactor                                tracked
    refs/remotes/origin/feature/feedback-ui stale (use 'git remote prune' to remove)
  Local branch configured for 'git pull':
    master merges with remote master
  Local refs configured for 'git push':
    develop  pushes to develop  (up to date)
    master   pushes to master   (up to date)
    refactor pushes to refactor (up to date)

ここで、github.com上でマージ済みのブランチfeature/feedback-uiをdeleteした

f:id:otiai10:20130907101217p:plain

今のfeature/feedback-uiの状態は

なはず...

[10:04:26] % git branch -r
  about518/develop
  about518/feature-judge-chrome-version
  about518/feature/achievement-record
  about518/feature/play-sound-at-alert
  about518/feature/screen-shot
  about518/feature/upload-local-file
  about518/master
  about518/patch/fix-notification-img-bug
  about518/refactor
  origin/HEAD -> origin/master
  origin/develop
  origin/feature-judge-chrome-version
  origin/feature/feedback-ui     # ← あるよね
  origin/feature/input-tracking
  origin/feature/notification-nyukyo
  origin/feature/testem
  origin/master
  origin/refactor
  zep/develop
  zep/feature-local-storage
  zep/feature-notification
  zep/master

# ぷるーんしましょ?
[10:04:28] % git remote prune origin
Pruning origin
URL: git@github.com:otiai10/kanColleWidget.git
 * [pruned] origin/feature/feedback-ui

# トラッキングから...
[10:04:50] % git branch -r
  about518/develop
  about518/feature-judge-chrome-version
  about518/feature/achievement-record
  about518/feature/play-sound-at-alert
  about518/feature/screen-shot
  about518/feature/upload-local-file
  about518/master
  about518/patch/fix-notification-img-bug
  about518/refactor
  origin/HEAD -> origin/master
  origin/develop
  origin/feature-judge-chrome-version
  origin/feature/input-tracking
  origin/feature/notification-nyukyo
  origin/feature/testem
  origin/master
  origin/refactor
  zep/develop
  zep/feature-local-storage
  zep/feature-notification
  zep/master
# 無くなっておる

# ローカルには...
[10:05:02] % git branch
  about518/backup
  about518/develop
* develop
  feature/feedback-ui     # ← ある
  feature/upload-local-file
  master
  refactor

# ローカルからも消すわ
[10:17:19] % git branch -d feature/feedback-ui
Deleted branch feature/feedback-ui (was da3fb88).

雑感

  • pruneしてもローカルからは消してくれないんですね
    • まあローカルブランチは目の前ちらちら見えるから多分意識的に消すし
    • ラッキングブランチは意識しないと視野に入らないので、つまりpruneコマンド便利れす
  • 注意すんのは
    • まだローカルにpushしてない作業ブランチがあった場合にpruneしちゃうと...
    • と思ったけどpruneはトラッキング消すけどローカル消さないから大丈夫か!よくできてんなー

以下、雑多なログ

[10:17:19] % git branch -d feature/feedback-ui
Deleted branch feature/feedback-ui (was da3fb88).
[10:17:48] %

# この間、github.comでfeature/input-trackingとfeature/testemを削除した

[10:37:32] %
[10:37:33] %
[10:37:33] % git remote show origin
* remote origin
  Fetch URL: git@github.com:otiai10/kanColleWidget.git
  Push  URL: git@github.com:otiai10/kanColleWidget.git
  HEAD branch: develop
  Remote branches:
    develop                                    tracked
    feature-judge-chrome-version               tracked
    feature/notification-nyukyo                tracked
    master                                     tracked
    refactor                                   tracked
    refs/remotes/origin/feature/input-tracking stale (use 'git remote prune' to remove)  # ぉーこれだー
    refs/remotes/origin/feature/testem         stale (use 'git remote prune' to remove)   # ぉーひなもpruneするー
  Local branch configured for 'git pull':
    master merges with remote master
  Local refs configured for 'git push':
    develop  pushes to develop  (up to date)
    master   pushes to master   (up to date)
    refactor pushes to refactor (up to date)

# github.comでじゃなくて、push REMOTE-NAME :BRANCH-NAME で消して見るとどうなるのか?
[10:37:44] % git push origin :feature/notification-nyukyo
To git@github.com:otiai10/kanColleWidget.git
 - [deleted]         feature/notification-nyukyo
[10:37:57] % git push origin :feature-judge-chrome-version
To git@github.com:otiai10/kanColleWidget.git
 - [deleted]         feature-judge-chrome-version

# push REMOTE-NAME :BRANCH-NAME で消すとトラッキングからも消してるわけか
[10:38:17] % git remote show origin
* remote origin
  Fetch URL: git@github.com:otiai10/kanColleWidget.git
  Push  URL: git@github.com:otiai10/kanColleWidget.git
  HEAD branch: develop
  Remote branches:
    develop                                    tracked
    master                                     tracked
    refactor                                   tracked
    refs/remotes/origin/feature/input-tracking stale (use 'git remote prune' to remove)
    refs/remotes/origin/feature/testem         stale (use 'git remote prune' to remove)
  Local branch configured for 'git pull':
    master merges with remote master
  Local refs configured for 'git push':
    develop  pushes to develop  (up to date)
    master   pushes to master   (up to date)
    refactor pushes to refactor (up to date)

# ぉーひなpruneおぼえたー
[10:39:31] % git remote prune origin
Pruning origin
URL: git@github.com:otiai10/kanColleWidget.git
 * [pruned] origin/feature/input-tracking
 * [pruned] origin/feature/testem

[10:39:55] % git branch -r
  about518/develop
  about518/feature-judge-chrome-version
  about518/feature/achievement-record
  about518/feature/play-sound-at-alert
  about518/feature/screen-shot
  about518/feature/upload-local-file
  about518/master
  about518/patch/fix-notification-img-bug
  about518/refactor
  origin/HEAD -> origin/master
  origin/develop
  origin/master
  origin/refactor
  zep/develop
  zep/feature-local-storage
  zep/feature-notification
  zep/master
[10:40:26] %