LoginSignup
1

More than 5 years have passed since last update.

VSCode/Git 意図しないブランチにpushしてしまった件

Posted at
  • Windows10
  • VSCode 1.29.1

やったこと

  • ブランチ作成
    • git checkout -b new-branch origin/other-branch
  • VSCodeにて
    • ファイル修正
    • commit
    • push
      • ソース管理(Ctrl + Shift + G) ⇒ 三つポチ ⇒ プッシュ
  • new-branchにpushされると思ったが、other-branchにpushされてる :sob:

調査

VSCodeのプッシュで、どんなコマンドが発行されたのか?

  • ソース管理(Ctrl + Shift + G) ⇒ 三つポチ ⇒ Git 出力の表示
実際に発行されたコマンド
> git push origin new-branch:other-branch

このコマンドの意味って?

git push <リモートのレポジトリ名> <ローカルのブランチ名>:<リモートのブランチ名>

  • 意図した操作じゃなーい :sob:

なぜこのようなコマンドが発行されるのか?

どう操作すればnew-branchにpushできたのか?

パターン1 (「やったこと」の場合)

  • :warning: pushの前に (ブランチ作成直後がベター) :warning:
  • VSCodeのソース管理(Ctrl + Shift + G) ⇒ 三つポチ ⇒ ブランチの発行
実際に発行されたコマンド
> git push -u origin new-branch

パターン2 (VSCodeで完結)

  • F1 + Git Branchと入力 + 作成したいブランチ名を入力
    • 追跡ブランチは作成されない
実際に発行されたコマンド
> git checkout -q -b new-branch
  • VSCodeのソース管理(Ctrl + Shift + G) ⇒ 三つポチ ⇒ ブランチの発行 もしくは、画面左下の雲矢印マーク
  • ファイル修正
  • commit
  • push
    • 仮にブランチの発行を忘れても、警告ダイアログが開く
      • 'new-branch'ブランチに上流ブランチはありません。このブランチを公開しますか?
実際に発行されたコマンド
> git push

fatal: The current branch new-branch has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin new-branch

> git push -u origin new-branch

所感 (言い訳)

  • いつもはVSCodeからGit操作をすることが無いため、この現象に陥ることは無かった
  • とはいえ、Gitについて分かっていなかったので、そのことに気づけて良かった・・・ :v:

補足

Gitのお勉強

This used to be the default, but not since Git 2.0 (simple is the new default).

これからのデフォルト。upstreamが設定されていて、それが同名のブランチ名であるときのみpushする。初心者でも安心して使える。初心を忘れないあなたと、デフォルトを愛するあなた向き。

  • push.default=simplegit pushを実行すると以下のようになる
$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:master

To push to the branch of the same name on the remote, use

    git push origin hogehoge

To choose either option permanently, see push.default in 'git help config'.
  • 追跡ブランチを確認してみる
    • 「やったこと」のブランチ作成後だと、追跡ブランチが[origin/other-branch]となっている
    • この辺りが VSCodeの動作と関係ありそうだが・・
$ git branch -vv
* new-branch   1234567 [origin/other-branch] hage

シンプルな方法としては、git checkout -b [branch] [remotename]/[branch] を実行します。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1