目次
[git]remote addでリポジトリを追加しよう!
今回は、git remoteコマンドの使い方について説明します。gitはリポジトリを使ってソースなどのファイルのバージョン管理をします。リポジトリにはローカルリポジトリとリモートリポジトリが存在します。ここでは、リモートリポジトリを管理する方法を紹介します。gitのバージョンは以下とします。
1
2
|
~$ git --version
git version 2.25.1
|
興味のある方はぜひご覧ください。
ヘルプ表示
まず、GitHubからgit cloneコマンドでcloneします。ここでは、「test」というリポジトリを利用します。
1
2
3
4
5
6
7
8
9
|
~$ git clone git@github.com:{GitHubユーザ名}/test.git
Cloning into 'test'...
The authenticity of host 'github.com (52.192.72.89)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,52.192.72.89' (RSA) to the list of known hosts.
Enter passphrase for key '/home/user/.ssh/id_rsa':
warning: You appear to have cloned an empty repository.
~$ cd test/
|
git remoteまたは、git remote addコマンドはhオプションでヘルプが表示できます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
~/test$ git remote -h
usage: git remote [-v | --verbose]
or: git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>
or: git remote rename <old> <new>
or: git remote remove <name>
or: git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
or: git remote [-v | --verbose] show [-n] <name>
or: git remote prune [-n | --dry-run] <name>
or: git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
or: git remote set-branches [--add] <name> <branch>...
or: git remote get-url [--push] [--all] <name>
or: git remote set-url [--push] <name> <newurl> [<oldurl>]
or: git remote set-url --add <name> <newurl>
or: git remote set-url --delete <name> <url>
-v, --verbose be verbose; must be placed before a subcommand
|
1
2
3
4
5
6
7
8
9
10
11
|
~/test$ git remote add -h
usage: git remote add [<options>] <name> <url>
-f, --fetch fetch the remote branches
--tags import all tags and associated objects when fetching
or do not fetch any tag at all (--no-tags)
-t, --track <branch> branch(es) to track
-m, --master <branch>
master branch
--mirror[=(push|fetch)]
set up remote as a mirror to push to or fetch from
|
リモートリポジトリの一覧表示
git remoteコマンドで、現在登録されているリモートの一覧を表示できます。
1
2
3
4
5
6
|
~/test$ git remote
origin
~/test$ git remote -v
origin git@github.com:{GitHubユーザ名}/test.git (fetch)
origin git@github.com:{GitHubユーザ名}/test.git (push)
~/test$
|
リモートリポジトリの追加
git remote addコマンドで、リモートリポジトリを追加できます。
1
2
3
4
5
6
7
8
9
10
|
~/test$ git remote add develop git@github.com:{GitHubユーザ名}/test.git
~/test$ git remote
develop
origin
~/test$ git remote -v
develop git@github.com:{GitHubユーザ名}/test.git (fetch)
develop git@github.com:{GitHubユーザ名}/test.git (push)
origin git@github.com:{GitHubユーザ名}/test.git (fetch)
origin git@github.com:{GitHubユーザ名}/test.git (push)
~/test$
|
リモートリポジトリの削除
git remote removeコマンドで、リモートリポジトリを削除できます。
1
2
3
4
5
6
7
|
~/test$ git remote remove develop
~/test$ git remote
origin
~/test$ git remote -v
origin git@github.com:{GitHubユーザ名}/test.git (fetch)
origin git@github.com:{GitHubユーザ名}/test.git (push)
~/test$
|
リモートリポジトリの変更
git remote renameコマンドで、リモートリポジトリを変更できます。変更前は以下の状態とします。
1
2
3
4
5
6
7
8
9
|
~/test$ git remote
develop
origin
~/test$ git remote -v
develop git@github.com:{GitHubユーザ名}/test.git (fetch)
develop git@github.com:{GitHubユーザ名}/test.git (push)
origin git@github.com:{GitHubユーザ名}/test.git (fetch)
origin git@github.com:{GitHubユーザ名}/test.git (push)
~/test$
|
以下のコマンドでリモートリポジトリを変更できます。
1
2
3
4
5
6
7
8
9
10
|
~/test$ git remote rename develop feature
~/test$ git remote
feature
origin
~/test$ git remote -v
feature git@github.com:{GitHubユーザ名}/test.git (fetch)
feature git@github.com:{GitHubユーザ名}/test.git (push)
origin git@github.com:{GitHubユーザ名}/test.git (fetch)
origin git@github.com:{GitHubユーザ名}/test.git (push)
~/test$
|
リモートリポジトリの情報表示
git remote showコマンドで、リモートリポジトリの情報が表示できます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
~/test$ git remote show origin
Warning: Permanently added the RSA host key for IP address '52.69.186.44' to the list of known hosts.
Enter passphrase for key '/home/user/.ssh/id_rsa':
* remote origin
Fetch URL: git@github.com:{GitHubユーザ名}/test.git
Push URL: git@github.com:{GitHubユーザ名}/test.git
HEAD branch: (unknown)
Local branch configured for 'git pull':
master merges with remote master
~/test$ git remote show feature
Warning: Permanently added the RSA host key for IP address '13.114.40.48' to the list of known hosts.
Enter passphrase for key '/home/user/.ssh/id_rsa':
* remote feature
Fetch URL: git@github.com:{GitHubユーザ名}/test.git
Push URL: git@github.com:{GitHubユーザ名}/test.git
HEAD branch: (unknown)
~/test$
|
リモートリポジトリのURL取得・設定
git remote get-urlコマンドで、リモートリポジトリのURLを取得できます。
1
2
3
|
~/test$ git remote get-url origin
git@github.com:{GitHubユーザ名}/test.git
~/test$
|
git remote set-urlコマンドで、リモートリポジトリのURLを設定できます。
1
|
~/test$ git remote set-url origin git@github.com:{GitHubユーザ名}/test.git
|
まとめ
いかがでしたでしょうか。gitリポジトリにはローカルリポジトリとリモートリポジトリが存在します。ここでは、リモートリポジトリを管理する方法を紹介しました。
ぜひご自身でコマンドを打って、理解を深めてください。
インフラエンジニア専門の転職サイト「FEnetインフラ」
FEnetインフラはサービス開始から10年以上『エンジニアの生涯価値の向上』をミッションに掲げ、多くのエンジニアの就業を支援してきました。
転職をお考えの方は気軽にご登録・ご相談ください。