LoginSignup
7
8

More than 5 years have passed since last update.

【MacOS】他の人のGitHub から clone してリモートリポジトリを追加して自分の GitHub に push する方法

Last updated at Posted at 2018-11-07

友人と共同開発するためGitをゼロから学びなう。
注意: Git学び中なので間違っている点や用語の使い方がおかしいかもです。


1. 友人のGithubから目的のgitをローカルリポジトリにcloneする。

cloneを作成したい任意のディレクトリにcdし
$ git clone [URL]
コマンドを実行する。

cloneが終わったらlsコマンドで目的のディレクトリが作成されているか確認する。

例: KeigoTakahashiのケース
$ pwd //あらかじめcdコマンドで目的のディレクトリに移動しておく
/Users/keigotakahashi/Desktop/Xcode/Practice
$ ls //ディレクトリ内のディレクトリを確認
MyCalc //元からあるプロジェクト
$ git clone https://github.com/tt556/SwiftMousouLINE.git //このセクションのテーマ
Cloning into 'SwiftMousouLINE'...
remote: Enumerating objects: 31, done.
remote: Counting objects: 100% (31/31), done.
remote: Compressing objects: 100% (27/27), done.
remote: Total 31 (delta 2), reused 31 (delta 2), pack-reused 0
Unpacking objects: 100% (31/31), done. //clone完了
$ ls //cloneコマンドによりgitファイルが作られたことを確認
MyCalc SwiftMousouLINE //SwiftMousouLineが今回の目的のgit


2. 自分で編集を加えてコミットする。

ここに関しては、下記参照。
https://qiita.com/doge_kun55/items/f269b89b73f23e6b0cb6


3. コミットしてgitファイルが作成されたか確認する。

クローンを作成したディレクトで
$ git remote -v
コマンドを実行する。私の場合
$ origin https://github.com/tt556/SwiftMousouLINE.git (fetch)
$ origin https://github.com/tt556/SwiftMousouLINE.git (push)
となっていました。予想通りです。

Xcodeで編集し目的のファイルをcommitするまでは
$ git remote -v
コマンドを実行しても何も表示されませんでした。予想外です。てっきりcloneしたやつが表示されると思っていましたが、自分で一度commitしないといけないみたいです。


4. commitしたgitをpushする。

commit(編集)したgitをpushする。
$ git push [remote-name] [branch-name]
このコマンドでpushできます。

例: KeigoTakahashiのケース
[remote-name]と[branch-name]はよくわかりませんが、とりあえず適当にやってみました。
$ git push origin master
remote: Permission to tt556/SwiftMousouLINE.git denied to TKeigo.
fatal: unable to access 'https://github.com/tt556/SwiftMousouLINE.git/': The requested URL returned error: 403

権限ないからpush拒否されました。で友人は寝てるから自分のリモートリポジトリにpushすることにしました。


5. リモートリポジトリを追加(自分のGithubにpush)する方法

セクション4と同じくcommitしたgitをpushします。リモートレポジトリの情報をローカルに追加します。少し考察。

私は友人のリモートレポジトリからgitをcloneしてきました。cloneした段階で[remote-name]と[branch-name]は自動的に作られます。友人のリモートリポジトリからcloneしてきたgitをcommitして自分のリモートリポジトリにpushします。ここでは[remote-name]を弄ることによってそれが可能になります。

$git remote
とだけ打つと現在の[remote-name]の一覧が表示されます。私の場合は友人のGithubの[remote-name]であるoriginだけが表示されました。先ほども言ったように[remote-name]はcloneした時勝手に作られます。[remote-name]であるoriginが持つ情報はoriginという名前情報とどこからcloneされたのかのURL情報が格納されていると考えられます。※あくまでも予想です。

今度は[remote-name]を新たに作成することで、自分のGithubのURL情報とそれに対応する名前情報を持った[remote-name]が作られると仮定しました。やってみたのが下記です。

$ git remote add keigo https://github.com/TKeigo/mysite.git

これでkeigoという名前にTKeigoのGithubのURL情報が紐づけられました。
あとはセクション4と同じくcommitしたgitをpushします。

$ git push keigo master
Username for 'https://github.com':
Password for 'https://TKeigo@github.com':
Counting objects: 36, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (34/34), done.
Writing objects: 100% (36/36), 1.28 MiB | 1015.00 KiB/s, done.
Total 36 (delta 7), reused 0 (delta 0)
remote: Resolving deltas: 100% (7/7), done.
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/TKeigo/mysite/pull/new/master
remote:
To https://github.com/TKeigo/mysite.git
* [new branch] master -> master

これで完了です。


参考にしたサイト: https://git-scm.com/book/ja/v1/Git-%E3%81%AE%E5%9F%BA%E6%9C%AC-%E3%83%AA%E3%83%A2%E3%83%BC%E3%83%88%E3%81%A7%E3%81%AE%E4%BD%9C%E6%A5%AD

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