LoginSignup
3
3

More than 3 years have passed since last update.

Githubでソースコードを管理する [マージ編]

Last updated at Posted at 2019-06-18

まずはじめに

もし気に入っていただけたり、役に立ったらぜひ ”いいね!” お待ちしております!
投稿のモチベーションに繋がります^^¥

image.png

前回の記事

Githubでソースコードを管理する [初級編]
https://qiita.com/tnoce/items/22d64f6187d68aa22890

この記事の目的

前回はGithub基本的な使い方(初期設定 ~ add, commit, push)を書きました。
今回は、ブランチの使い方について、書いていきます。

⚠⚠⚠⚠ 自分の理解のためにもアウトプットとしてGithubの使い方を投稿しています。⚠⚠⚠⚠
⚠⚠⚠⚠ 私自身も初心者なので情報が不正確な可能性があるので、ご了承ください ⚠⚠⚠⚠

Githubのブランチとは

ブランチ(branch)とは、1つのプロジェクトからブランチを分岐させることで、
同じリポジトリ中で複数の変更を同時に進めて行くことが可能になる機能です。

分岐したブランチは他のブランチと合流(merge)させることで、1つのブランチにまとめ直すことができます。
合流させるブランチは 明示的に、" master " 名付けることが多いようです。

  • アプリケーションの開発でGithubリポジトリを用意
  • ブランチを3つ切る
    • masterブランチ
    • Aさん専用ブランチ
    • Bさん専用ブランチ
  • それぞれのブランチで開発した内容をmasterブランチにmerge
  • masterブランチはAさんBさんそれぞれが開発したファイルやコードが入る

image.png

ローカルブランチでマージした内容を、リモートリポジトリのブランチに反映させることも可能です。

image.png

兎にも角にも使ってみる

↓↓ゴールイメージ
image.png

開発専用ブランチの内容をmasterブランチに反映させる

dev01で作成したサンプルファイル(aaa.txt)をmasterブランチにも反映させる方法です。

ブランチをdev01に切り替える

git checkout 'ブランチ名'でブランチを切り替えることが可能です。

$ git checkout dev01
Switched to branch 'dev01'
Your branch is up to date with 'origin/dev01'.

自分がどこのブランチにいるか確認する

git statusで確認が可能です。

$ git status
On branch dev01
Your branch is up to date with 'origin/dev01'.

nothing to commit, working tree clean

nothing to commit, working tree clean

gitの優しいところですが、ブランチを切り替えるたびに、commit済みのファイルが有るかどうか知らせてくれます。笑

dev01ブランチでaaa.txtファイルを作成

aaa.txtファイルが存在することを確認します。

$ touch aaa.txt
$ ls -l 
-rw-r--r--  1 SHU  staff  0  6 18 13:44 index.html
-rw-r--r--  1 SHU  staff  0  6 18 13:45 aaa.txt

dev01ブランチにpush

$ git add -A
$ git commit -m "added a new file"
dev01 8f62edc] added a new file
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename test.txt => aaa.txt (100%)

$ git push
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 284 bytes | 284.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
git hTo https://github.com/tnoce/test
   12030df..8f62edc  dev01 -> dev01

リモート側に反映されたことを確認します。
image.png

masterブランチにマージする

masterブランチにマージするには、自身がmasterブランチにいた状態でマージしたいブランチを指定する必要があります。
先ほど "dev01" で "aaa.txt" ファイルを作成したので、dev01をマージソースのブランチとして指定してあげます。

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git status

$ git merge dev01
Merge made by the 'recursive' strategy.
 aaa.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 aaa.txt

merge して満足してはいけません。
あくまでもローカル側だけmergeされただけなので、マージされた内容をpushしましょう。
(commitは不要です)

$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

$ git push
Counting objects: 1, done.
Writing objects: 100% (1/1), 234 bytes | 234.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/tnoce/test
   0fe6078..b118ace  master -> master

リモートのリポジトリ側で、masterブランチにdev01ブランチの内容が反映されました。
image.png

最後に

マージ編について、いかがだったでしょうか。
次回はマージした際のconflictの解消方法についても更新していきたいと思います。

以上

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