LoginSignup
3
3

More than 5 years have passed since last update.

gitでpush.defaultがupstreamでも違うブランチにpushされてしまった話

Posted at

何が起こったか

あるプロジェクトで、追加したい機能があるからmasterブランチからfeatureブランチ切って実装。よしじゃあpull request投げるかーということでpushしたらなんとmasterブランチにpushされてしまった。焦った。

なぜmasterブランチにpushされてしまったのか

featureブランチの切りかたに原因があった

git fetch -p
git checkout -b feature origin/master

git checkout -bで派生元ブランチにリモートブランチを指定することで、追跡ブランチも指定したリモートブランチに設定されてしまうみたいだった。
この状態でgit pushしてしまうとorigin/masterにpushされちゃう。こわい。

どうやって防ぐか

branch.autosetupmergeをfalseに設定

この設定を行っておくことで、派生元ブランチにリモートブランチが指定されていても追跡ブランチに設定されなくなる

git config --global branch.autosetupmerge false

ちなみに--no-trackオプションをつけることでも上記設定と同じ動きをしてくれる。

git checkout -b --no-track feature origin/master

個人的に一番いいと思う方法

ちゃんとgit push origin featureとpush先を指定する。多分一番シンプルで間違いにも気づきやすいし。

3
3
1

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
3
3