LoginSignup
414
400

More than 5 years have passed since last update.

git を初めて使う時 コミット→プッシュ→マージ の流れ

Last updated at Posted at 2018-05-13

gitを使って開発する基本の流れ

ポイント:gitは怖くない

  • コミット、プッシュ、マージができれば作業可能
  • IDEやGUIのツールからも使えますが覚えるまではまずはコマンドラインでやった方が理解しやすい
  • ブランチは最初はmasterしかない。あとはどういうフローで進めたいかで自分で決める。

あるタスクに着手する際、ブランチをmasterから切って作業する流れ

※この例ではmasterからブランチを切っていますが、どのブランチから開発ブランチを切るかは、チーム開発の場合はルールを決めて行うのが一般的です。

今回の作業イメージ
image.png

0.準備 ローカル環境にgitのリモートリポジトリからソースを取得

コマンド
git clone [clone_url]
cd [cloneして生成されたディレクトリ]

1.自分のローカル環境にあるブランチの確認

コマンド
git branch
git branch

# ブランチの一覧が表示される
# *がついているブランチが今いるブランチ
    develop
    feature/16
  * master

2.ブランチをきる(ブランチを作る)

コマンド
git branch xxxx
git branch feature/58
git branch     #  ブランチが作成されたか確認のため実施
    develop
    feature/16
  * master
    feature/58 # ブランチが作成されていることが確認できる

3.ブランチに移動(チェックアウト)

コマンド
git checkout xxx
git checkout feature/58
    Switched to branch 'feature/58'

git branch # branch移動できたか確認のため実施
    develop
    feature/16
    master
  * feature/58 # 現在のブランチが作成したブランチになっていることを確認


4.タスクの対応を行う :sake:

ソースの改修を行う

5.変更したファイルの一覧を確認

コマンド
git status
git status
    On branch feature/58
    Changes not staged for commit:
    (use "git add/rm <file>..." to update what will be committed)
    (use "git checkout -- <file>..." to discard changes in working    directory)

    modified:   app/index.html
    modified:   app/scripts/ :innocent: app.js
    deleted:    app/scripts/controllers/detail.jsbk
    modified:   app/scripts/controllers/searchctrl.js

    Untracked files:
    (use "git add <file>..." to include in what will be committed)

    app/scripts/controllers/kakugenctrl.js
    app/views/kakugen.html

    no changes added to commit (use "git add" and/or "git commit -a")

直前のコミットからの変更があったファイルが表示されます
・modified:修正したファイル
・deleted:削除したファイル
・Untracked files:新規作成したファイル

6.変更したファイルのソースコードを確認

コマンド
git diff

7.変更点に問題なければコミット

変更点でタイポやデバッグで使用していたコメントなどを解消したらコミットします。
コミットするのは以下の2ステップです。

  1. コミットしたいファイルを選択
  2. コミット
1.コミットしたいファイルを選択
コマンド
git add xxx
git add app/scripts/controllers/kakugenctrl.js

# 消す対象のファイルをコミット対象とする時
git rm app/scripts/controllers/detail.jsbk

# ディレクトリ単位でコミットしたい時は以下のようにディレクトリ指定も可能
# git add ./ 
コミット対象を確認する
git status
    On branch feature/58
    Changes to be committed:`
    modified:   app/index.html
    modified:   app/scripts/app.js
    deleted:    app/scripts/controllers/detail.jsbk
    new file:   app/scripts/controllers/kakugenctrl.js
    modified:   app/scripts/controllers/searchctrl.js
    new file:   app/views/kakugen.html

2.コミットする

コマンド
git commit
# m オプションを使用するとコメントを同時にセットできる
# git commit -m"feature/58" -m"格言画面へ飛ぶように修正"`
git commit -m"feature/58" -m"格言画面へ飛ぶように修正"`
    [feature/58 a566530] feature/58
     6 files changed, 17 insertions(+), 90 deletions(-)
     delete mode 100644 app/scripts/controllers/detail.jsbk
     create mode 100644 app/scripts/controllers/kakugenctrl.js
     create mode 100644 app/views/kakugen.html
コミットログを確認する
コマンド
git log
git log
      commit a56653076dbc7eaff968f76f40862b47ec7037bf
      Author: yukiyoshimura <xxxx@gmail.com>
      Date:   Sun Apr 5 22:01:03 2015 +0900

      feature/58

      格言画面へ飛ぶように修正

      commit 72f452d5414fe23e76f510f15b046015f54a9b1e
      Author: ikouya <xxxx@gmail.com>
      Date:   Thu Apr 2 00:58:59 2015 +0900

          Initial commit

githubへpush

コマンド
git push origin xxx
git push origin feature/58`
    Counting objects: 11, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (10/10), done.
    Writing objects: 100% (11/11), 1.22 KiB | 0 bytes/s, done.
    Total 11 (delta 5), reused 0 (delta 0)
    To https://github.com/ikouya/dailyweb.git
    * [new branch]      feature/58 -> feature/58

8.マージしたい時

masterブランチにマージするとき

コマンド

# masterブランチに移動
git checkout master
    Switched to branch 'master'
    Your branch is up-to-date with 'origin/master'.

# masterブランチにfeatureブランチをマージします
git merge feature/58`

    Updating e070127..a566530
    Fast-forward
     app/scripts/controllers/detail.jsbk    | 82 ----------------------------------------------------------
     app/scripts/controllers/kakugenctrl.js |  7 +++++++
     2 files changed, 17 insertions(+), 90 deletions(-)
     delete mode 100644 app/scripts/controllers/detail.jsbk
     create mode 100644 app/scripts/controllers/kakugenctrl.js
    yukiyoshimura /Applications/develop/GitHub/dailyweb $ git push origin master
    Total 0 (delta 0), reused 0 (delta 0)
    To https://github.com/ikouya/dailyweb.git
       e070127..a566530  master -> master

# リモートブランチを更新
git push origin master
414
400
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
414
400