31
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

git pushとブランチの追跡

Posted at

git pushした時の挙動でちょっと躓いたのでメモ

リモートブランチの追跡有無によるpush時の挙動の違い

カレントブランチがリモートブランチを追跡している場合、
git pushで追跡しているブランチにpushできる。

$ git push

カレントブランチがリモートブランチを追跡していない場合、
git pushだけだと怒られる。

$ git push
fatal: The current branch <branch_name> has no upstream branch.
To push the current branch and set the remote as upstream, use

カレントブランチがリモートブランチを追跡していない場合でもこのコマンドならpushできる。

$ git push origin <branch>

-uオプションをつけることで、
カレントブランチの変更をリモートにpushしつつ、そのブランチを追跡できるようになる。

$ git push -u origin <branch>

-uオプションをつけたあとはgit pushのみでpush可能になる。

ブランチの作成と同時に追跡させる

ローカルのブランチからブランチを作成すると、
リモートブランチとローカルブランチの関連付けが無いため追跡していない。

$ git checkout -b branch-a
Switched to a new branch 'branch-a'

リモートブランチからクローンしてローカルにブランチを作成すると、
自動的に追跡できる。

$ git checkout -b branch-b origin/develop
Branch branch-b set up to track remote branch develop from origin.
Switched to a new branch 'branch-b'

追跡しているリモートブランチを確認するコマンド

$ git branch -vv

参考

31
31
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?