LoginSignup
2
1

More than 3 years have passed since last update.

Github練習環境で1人2役を演じる方法

Last updated at Posted at 2020-12-03

やりたいこと

Githubの練習環境がほしい。
特に複数人で開発するときの挙動を1人で確かめることができる環境がほしい。

例)mergeした際にコンフリクトが発生した場合の挙動

結論

ローカルリポジトリを複数用意して、2人以上での開発を擬似的に再現するのが一番お手軽。

やり方

準備するもの

  • 練習用のGithubリポジトリ ( git-practice.git )
  • 上記リポジトリ上に編集用のテキストファイル ( sample.txt )
  • Git初期設定済みのローカル環境

手順

実際に2人で編集作業をしていてコンフリクトが起こったケースを再現してみる。

1. 1つ目のローカルリポジトリを作成

$ cd ~/doodling
$ mkdir multiuser-practice
$ cd multiuser-practice
$ mkdir main
$ cd main

$ git clone git@github.com:github-username/git-practice.git
$ cd git-practice

2. 2つ目のローカルリポジトリを作成

$ cd ~/doodling/multiuser-practice
$ mkdir sub
$ cd sub

$ git clone git@github.com:github-username/git-practice.git
$ cd git-practice

3. 1つ目のローカルリポジトリで編集作業

$ cat sample.txt
Hello Git
$ echo "i am main account." >> sample.txt
$ cat sample.txt
Hello Git
i am main account.
$ git add sample.txt
$ git commit -m "(main) add text"

リモートリポジトリへ push しておく

$ git push
...
To github.github-username/git-practice.git
   12df34f..6d4e2fa  main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

4. 2つ目のローカルリポジトリで編集作業

※1つ目のローカルリポジトリでの編集作業とほぼ同じ

$ cat sample.txt
Hello Git
$ echo "i am sub account." >> sample.txt
$ cat sample.txt
Hello Git
i am sub account.
$ git add sample.txt
$ git commit -m "(sub) add text"
$ git push

リモートリポジトリに push すると reject される。

$ git push
To github.com:github-username/git-practice.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'git@github.com:github-username/git-practice.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

5. 2つ目のローカルリポジトリで merge 作業

$ git fetch origin
$ git merge origin/main
Auto-merging sample.txt
CONFLICT (content): Merge conflict in sample.txt
Automatic merge failed; fix conflicts and then commit the result.

$ vi sample.txt
# resolve conflict
$ git add sample.txt
$ git commit
[main a607e9a] Merge remote-tracking branch 'origin/main' into main
$ git log --oneline
a607e9a (HEAD -> main) Merge remote-tracking branch 'origin/main' into main
ae23d99 (sub) add text
6d4e2fa (origin/main, origin/HEAD) (main) add text
12df34f add sample.txt

$ git push
...
To github.com:github-username/git-practice.git
   6d4e2fa..a607e9a  main -> main

$ git log --oneline
a607e9a (HEAD -> main, origin/main, origin/HEAD) Merge remote-tracking branch 'origin/main' into main
ae23d99 (sub) add text
6d4e2fa (main) add text
12df34f add sample.txt

その他のやり方

複数のGithubアカウントを切り替える

複数のGithubアカウントを作って切り替える方法。
プライベートと仕事で複数アカウントを使い分ける人には良さそうだが、今回のように練習用の環境を作りたいだけだと少し準備が大変そう。

参考:
https://qiita.com/yamataku29/items/4744c9c70ad793c83b82
https://qiita.com/0084ken/items/f4a8b0fbff135a987fea

複数の開発環境を用意する

  • 別のPC上に開発環境をつくる
  • OSのゲストアカウントを利用する
  • クラウド開発環境を利用する

など開発環境ごと2つ作ってしまうパターン。

使っていないパソコンがある人やクラウド開発環境を試して見たい場合には良さそう。

ただし、
- 仕事用のパソコンなのでOSのゲストアカウントを作れない
- クラウド開発環境にお金を払ってまで練習用環境を作るのはちょっと…

など個人の事情によっては難しいことも。

諦めて2人2役でやる

実際に開発仲間を探してきてコンフリクトなどを再現してもらう。
再現度が高いが、仲間を見つかられるかが鬼門。

まとめ

意外と簡単に1人2役での練習ができる。

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