LoginSignup
59
51

More than 3 years have passed since last update.

上流ブランチがなくてエラーになった時の対応方法

Last updated at Posted at 2019-07-09

事象 : プルしたらThere is no tracking information for the current branch.

  • 環境
    • Windows10 Pro バージョン1909

自分で作ったリポジトリを一人でプッシュばっかりしていたところへ他の人がプッシュし始めた。
他の人の分をプルしたら怒られた。

# ふぇっちしーーてーーー
$ git fetch --all --prune
Fetching origin
#...省略...
   234.....2edd2b4  master     -> origin/master

# プルしたら怒られた
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
    git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
    git branch --set-upstream-to=origin/<branch> master

原因 : 上流ブランチがないから

$ git branch -vv
* master 234... 手順書を追加した。

対応 : リモートブランチを指定する

参考 : git pullをもっと楽に - ゆずめも

方法は3種類
# 1. 毎回プルするときにリモートブランチを指定する
$ git pull origin master

# 2. 1回リモートブランチを設定して、ブランチ指定なしでプルする(リモートとローカルのブランチ名は同じ)
$ git push -u origin master
$ git pull
#...省略...
$ git branch -vv
* master 567.. [origin/master] 他の人のコミットコメント

# 3. 1回リモートブランチを設定して、ブランチ指定なしでプルする(リモートとローカルのブランチ名を変えられる)
$ git branch --set-upstream-to=origin/master master
$ git pull

アホな対応

リモートブランチを指定してプルしよう!と思って間違えまくる。
しかもなぜかメッセージを読まないわ、ググないわで、力技に出てしまった。

# ブランチを指定し漏れる・・・
$ git pull origin
You asked to pull from the remote 'origin', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

# リモートじゃなくてoriginを指定しても・・・
$ git pull origin/master master
fatal: 'origin/master' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

# logから適当なコミットを選んで・・・
$ git log
commit 234... (HEAD -> master)
Author: ponsuke <ponsuke@example.com>
Date:   Thu Nov 19 11:21:15 2020 +0900
    手順書を追加した。
commit 0e2...
Author: ponsuke <ponsuke@example.com>
# ...省略...

# 適当なコミットにチェックアウトして・・・
$ git checkout 0e2...
Note: switching to '0e2...'.
# ...微妙なとことをチェックアウトするからGitに心配されつつ・・・
You are in 'detached HEAD' state. You can look around, make experimental...省略...

# HEADをローカルブランチ(master)から外して・・・
$ git branch
* (HEAD detached at 0e2...)
  master

# ローカルブランチ(master)を強制削除して・・・
$ git branch -D master
Deleted branch master (was 234...).

# リモートブランチをチェックアウトするという力技・・・プルというかチェックアウトできるけどアホっぽい
$ git checkout master
Previous HEAD position was 0e2... 頑張ってるコミット
Switched to a new branch 'master'
Branch 'master' set up to track remote branch 'master' from 'origin'.

事象 : プッシュしたらfatal: The current branch ブランチ名 has no upstream branch.

  • 環境
    • macOS Mojave バージョン10.14.5
    • git version 2.21.0

新規ブランチを作ってプッシュしたら怒られた

# masterブランチの状態で
$ git branch --contains=HEAD
* master

# branch-aブランチを新規作成して
$ git checkout -b branch-a
M fileName.md
Switched to a new branch 'branch-a'

# プッシュしたら怒られた
$ git push
fatal: The current branch branch-a has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin branch-a

原因 : 上流ブランチがないから

ローカルで作成したbranch-aの上流ブランチ(upstream branch)は origin/branch-a となってほしかったのだが、branch-aにそもそも上流ブランチがないために怒られた。
上流ブランチの意味はGit で「追跡ブランチ」って言うのやめましょうにとてもわかり易く書いてあった。

# branch-aの上流ブランチが・・・ない
$ git branch -vv
* branch-a 85c84de masterブランチ最後のコミットコメント
  master   85c84de [origin/master] masterブランチ最後のコミットコメント

# --set-upstreamって何?
$ git push --help
# 省略
       -u, --set-upstream
           For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by
           argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).
# (頑張った訳:日本語が難しいので[]とか{}とかを区切れ目に使っている)
# [最新の状態である]または[正常にプッシュされた]ブランチごとに、{引き数なしのgit-pull(1)や他のコマンドで使われる[上流ブランチ]}を追加します。
# 詳細については、git-config (1)にあるbranch.<name>.mergを参照してください。

怒られた時に対応 : オプションを付けてプッシュと一緒に上流ブランチを追加する

$ git push --set-upstream origin branch-a
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'branch-a' on GitHub by visiting:
remote:      https://github.com/username/repository/pull/new/branch-a
remote:
To https://github.com/username/repository.git
 * [new branch]      branch-a -> branch-a
Branch 'branch-a' set up to track remote branch 'branch-a' from 'origin'.

# branch-aの上流ブランチが・・・できた
$ git branch -vv
* branch-a 85c84de [origin/branch-a] masterブランチ最後のコミットコメント
  master   85c84de [origin/master] masterブランチ最後のコミットコメント

ブランチを作るときに対応

① ブランチを作る時に親ブランチを指定する

参考 : git pushとブランチの追跡 - Qiita
この方法は結果が「なんだかよくわからない状態」になってしまった・・・

# この方法は新規ブランチ専用の上流ブランチを作成できるわけではなかった
$ git checkout -b branch-b origin/branch-b
fatal: 'origin/branch-b' is not a commit and a branch 'branch-b' cannot be created from it

# masterの上流ブランチであるorigin/masterを親ブランチに指定すると
$ git checkout -b branch-b origin/master
M fileName.md
Branch 'branch-b' set up to track remote branch 'master' from 'origin'.
Switched to a new branch 'branch-b'

# 新規ブランチの上流ブランチは当然origin/masterとなり
$ git branch -vv
  branch-a 85c84de [origin/branch-a] masterブランチ最後のコミットコメント
* branch-b 85c84de [origin/master] masterブランチ最後のコミットコメント
  master   85c84de [origin/master] masterブランチ最後のコミットコメント

# プッシュすると怒られて、選択肢を3つ提示される
$ 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 HEAD

To choose either option permanently, see push.default in 'git help config'.

# 「リモート上の同じ名前のブランチにプッシュする」をやると
$ git push origin HEAD
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'branch-b' on GitHub by visiting:
remote:      https://github.com/username/repository/pull/new/branch-b
remote:
To https://github.com/username/repository.git
 * [new branch]      HEAD -> branch-b

# 上流ブランチはorigin/masterのままだけど
$ git branch -vv
  branch-a 85c84de [origin/branch-a] masterブランチ最後のコミットコメント
* branch-b 85c84de [origin/master] masterブランチ最後のコミットコメント
  master   85c84de [origin/master] masterブランチ最後のコミットコメント

# リモートブランチとしてはbranch-bができる
$ git branch -r
  origin/HEAD -> origin/master
  origin/branch-a
  origin/branch-b
  origin/master

# リモートの情報をもっと見るとよくわからない状態になっていた
$ git remote show origin | grep -5 pull
  Remote branches:
    branch-a tracked
    branch-b tracked
    master   tracked
  Local branches configured for 'git pull':
    branch-a merges with remote branch-a
    branch-b merges with remote master
    master   merges with remote master
  Local refs configured for 'git push':
    branch-a pushes to branch-a (up to date)

② .gitconfigにローカルブランチと同じ名前の上流ブランチを使うように設定する

参考 : gitのpush.defaultに関するノウハウ - Qiita

# .gitconfigにcurrentを指定すれば
$ git config --global push.default current
$ cat ~/.gitconfig | grep -A 2 push
[push]
    default = current

# 新規にブランチを作成して
$ git checkout -b branch-c
M   fileName.md
Switched to a new branch 'branch-c'

# 上流ブランチはなくても
$ git branch -vv
  branch-a 85c84de [origin/branch-a] masterブランチ最後のコミットコメント
  branch-b 85c84de [origin/master] masterブランチ最後のコミットコメント
* branch-c 85c84de masterブランチ最後のコミットコメント
  master   85c84de [origin/master] masterブランチ最後のコミットコメント

# リモートブランチに同じ名前のブランチがなくても
$ git branch -r
  origin/HEAD -> origin/master
  origin/branch-a
  origin/branch-b
  origin/master

# プッシュできる!
$ git push
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'branch-c' on GitHub by visiting:
remote:      https://github.com/username/repository/pull/new/branch-c
remote:
To https://github.com/username/repository.git
 * [new branch]      branch-c -> branch-c

# プッシュ後も上流ブランチはないけど
$ git branch -vv
  branch-a 85c84de [origin/branch-a] masterブランチ最後のコミットコメント
  branch-b 85c84de [origin/master] masterブランチ最後のコミットコメント
* branch-c 85c84de masterブランチ最後のコミットコメント
  master   85c84de [origin/master] masterブランチ最後のコミットコメント

# リモートブランチはちゃんとできる
$ git branch -r
  origin/HEAD -> origin/master
  origin/branch-a
  origin/branch-b
  origin/branch-c
  origin/master
59
51
2

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
59
51