git

Remote Repositoryの設定と変更

How to use git command 8/N

公開日: 2021-01-07
更新日: 2023-06-16

  Table of Contents

Rename an existing Git remote?

既存のremote repositoryの名前を変更するためのコマンドをここでは紹介します. 名称変更に必要なのは,

  • 現在のremote repositoryの名前: <current-name>
  • これからのremote repositoryの名前: <target-name>

How to rename the remote?

Step 1: Confirm the name of your current remote

まず, 現在のremote repositoryの名前を確認します;

1
2
3
% git remote -v
origin	https://github.com/RyoNakagami/github_sandbox.git (fetch)
origin	https://github.com/RyoNakagami/github_sandbox.git (push)

現在のremote repositoryの名前が origin であることが確認できます.

Step 2: Change the remote name

Remoteの名前変更のコマンドは以下となります:

1
% git remote rename <current-name> <target-name> 

今回は, originからupstreamへ変更してみます

1
2
% git remote rename upstream origin
Renaming remote references: 100% (6/6), done.

remote repository nameが意図通り変更されているか確認します:

1
2
3
% git  remote -v
upstream	https://github.com/RyoNakagami/github_sandbox.git (fetch)
upstream	https://github.com/RyoNakagami/github_sandbox.git (push)

Change a Remote Repository’s URL

まずどのrepositoryもremote urlの設定から入ります

1
2
3
4
5
6
7
% git remote add origin https://github.com/OWNER/REPOSITORY.git
# Set a new remote

% git remote -v
# Verify new remote
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)

ここからremote repository’s urlを変更する場合は, git remote set-url コマンドを用います. remote repository nameがoriginの場合の変更コマンドは以下:

1
% git remote set-url origin <target-url>

REMARKS

  • sshとHTTPSで設定するURL構造の違いがあることに留意
  • 接続方式に合わせたurlを指定すること
1
2
3
4
5
# SSH
git@github.com:OWNER/REPOSITORY.git

# HTTPS
https://github.com/OWNER/REPOSITORY.git

Checkout a remote Git Branch

localにてgit branchが以下のような構成になっているとします.

1
2
3
4
5
% git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/hoge

localにはないがremoteに存在するhogeブランチへcheckoutしたい場合は以下の手順を踏みます:

  • git fetchでremote branchの情報を取り込む
  • git branch -vaでbranch情報を確認する
  • git switchを用いてcheckoutする
1
2
3
4
5
6
7
8
9
10
11
## git fetch
% git fetch

## check branch info
% git branch -va

## checkout the branch
% git switch hoge

## this can work too
% git switch -c hoge origin/hoge

checkoutする場合はorigin/hogeではなく, hogeというbranch nameだけで十分です. ただし, 内部挙動的には git commandがbranch nameからcheckoutしたいブランチを推測してcheckoutしていることに留意が必要です.

References



Share Buttons
Share on:

Feature Tags
Leave a Comment
(注意:GitHub Accountが必要となります)