1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【覚書き】Gitのoriginってなんなんだ

Last updated at Posted at 2024-09-21

初めに

GitをCLI操作し始めてから、originって単語をよく目にするようになりました。
最近ようやく意味を知ったので備忘としてアウトプットします。

originとは

Gitでブランチをチェックアウトする時、git checkoutコマンドを使用します。

$ git checkout -b testBranch origin/testBranch

-bの後が「作成するローカルブランチの名前」と「リモートブランチの名前」なのですが、ここで指定するoriginがよく分かっていませんでした。

前提:ローカルリポジトリは複数のリモートリポジトリと紐づけることが可能

前提としてローカルリポジトリは複数のリモートリポジトリを紐づけることが可能です。

git remote -vのコマンドで現在紐づいている一覧を見ることができます。

$ git remote -v
another https://github.com/username/gitTest2.git (fetch)
another https://github.com/username/gitTest2.git (push)
origin  https://github.com/username/gitTest.git (fetch)
origin  https://github.com/username/gitTest.git (push)

この例では、gitTest.gitgitTest2.gitという二つのリポジトリが紐づいていますね。

originとはデフォルトのリモートリポジトリのエイリアス

もうお分かりの方もいるかと思いますが、originとはデフォルトのリモートリポジトリのエイリアスです。

ローカルリポジトリをリモートリポジトリに紐づける際、各リモートリポジトリにエイリアスを設定することができます。そのデフォルトがoriginというわけです。

通常のgit cloneをした場合、originというエイリアスで登録されます。

$ git clone https://github.com/username/gitTest.git
$
$ git remote -v
origin https://github.com/username/gitTest.git (fetch)
origin https://github.com/username/gitTest.git (push)

clone時に --originオプションを利用することで、エイリアスを指定することもできます。

$  git clone --origin another https://github.com/username/gitTest2.git
$
$ git remote -v
another https://github.com/username/gitTest2.git (fetch)
another https://github.com/username/gitTest2.git (push)

ちなみに、git remote addでリモートを紐づける際は、必ずエイリアスの指定が必要です。originの場合も同様です。

$ git remote add origin https://github.com/username/gitTest.git
$
$ git remote -v
origin  https://github.com/username/gitTest.git (fetch)
origin  https://github.com/username/gitTest.git (push)

originは何に使うの

前述の通り、originはデフォルトのリモートブランチを指定するためのエイリアスです。
リポジトリの指定が必要なコマンドを使用する際にその効果を発揮します。

例えば、fetchなどですね。

$ git fetch           ← git fetch originと同義
$
$ git fetch another   ← 通常はリモートリポジトリのエイリアスを指定する

終わりに

ということで本日はGitoriginについて書きました。
GUI使ってると意識しなくてもできたので気にしてなかったですが、コマンドで操作する際は気になりますよね。
今後も気になったものは調べていこうと思います。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?