fr-itaya / training

0 stars 0 forks source link

バージョン管理 - Git - コマンドを使う #18

Closed fr-sato closed 10 years ago

fr-sato commented 10 years ago

■目的

Gitの基本的なコマンドを使い一連のバージョン管理の流れを学びます。

■課題

  1. リポジトリを作成して下さい
  2. 作成したリポジトリに新規ファイルを追加して下さい
  3. 現在のリポジトリの状態を確認して下さい(新規ファイル、追加ファイルなど)
  4. 追加したファイルを追加対象から外して下さい
  5. 再度ファイルを追加後、コミットして下さい
  6. 追加したファイルを編集、保存し、差分を表示して下さい
  7. 編集した内容をGitコマンドを用いて編集前に戻して下さい
  8. ファイルをGit管理から削除してコミットして下さい
  9. 直前のコミットを取り消して下さい(ファイルを削除する前の状態に戻す)
  10. 今までのGitのログを表示して下さい
  11. いづれか1つのファイルをGit管理に含めないように設定して下さい
  12. ブランチを作って移動して下さい
  13. 作ったブランチで新たな新規ファイルを追加、コミットした後、前のブランチにマージして下さい

調べた結果はこのIssueのコメントにまとめて下さい。

fr-itaya commented 10 years ago

本課題は17:45開始、工数見積もり合計4時間で行います。

内訳

実施日 作業時間 内容
4/16 45分 課題4まで
4/17 2時間30分 課題5-13
4/17 30分 Markdown記入・確認

以上、よろしくお願いいたします。

fr-itaya commented 10 years ago

4/17の作業時間は、15:15~17:50でした。

fr-itaya commented 10 years ago

4/18 本日の作業開始は14:10といたします。

fr-itaya commented 10 years ago

番号付きリストの番号を途中から始められなかったため、前日分と併せて再提出させていただきます。 ご確認宜しくお願い致します。


  1. リポジトリを作成

    • 使用コマンド:$ git init
    • 実行結果
    $ git init
    Initialized empty Git repository in /home/ *** /.git/
    $ ll
    total 124
    -rw-rw-r-- 1 *** *** 952 Apr 7 06:18 dl.tar.gz
    drwxrwxr-x 2 *** *** 4096 Apr 7 06:17 doc
    -rw-rw-r-- 1 *** *** 44645 Apr 7 06:16 ds.zip
    drwxrwxr-x 2 *** *** 4096 May 26 2010 gist414375-24c6049f621f07db839e8cdd133e396756d642db
    drwxrwxr-x 2 *** *** 4096 Apr 7 06:17 nerdtree_plugin
    drwxrwxr-x 2 *** *** 4096 Apr 7 06:17 plugin
    drwxrwxr-x 3 *** *** 4096 Mar 20 02:49 public_html
    -rw-rw-r-- 1 *** *** 153 Apr 14 07:08 regexp.txt
    -rw-rw-r-- 1 *** *** 43528 Apr 4 07:38 TEST
    drwxrwxr-x 8 *** *** 4096 Apr 11 08:58 tmux-powerline
    drwxrwxr-x 2 *** *** 4096 Apr 8 06:08 work
  2. 作成したリポジトリに新規ファイルを追加

    • 使用コマンド:$ git add [file_name]
    • 実行結果
    $ git add gittest.txt
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       new file:   gittest.txt
    #
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       modified:   .vimrc
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #       .bash_history
    #       .bash_logout
    #       .bash_profile
    #       .bashrc
    #       .composer/
    #       .lesshst
    #       .mysql_history
    #       .ssh/
    #       .swp
    #       .tmux-powerlinerc
    #       .tmux/
    #       .vim/
    #       .viminfo
    #       TEST
    #       dl.tar.gz
    #       doc/
    #       ds.zip
    #       gist414375-24c6049f621f07db839e8cdd133e396756d642db/
    #       gittest2.txt
    #       nerdtree_plugin/
    #       plugin/
    #       public_html/
    #       regexp.txt
    #       test3.txt
    #       tmux-powerline/
    #       work/
  3. 現在のリポジトリの状態を確認(新規ファイル、追加ファイルなど)

    • 使用コマンド:$ git status
    • 実行結果
    $ git add gittest2.txt
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       new file:   gittest.txt
    #       new file:   gittest2.txt
    #
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       modified:   .vimrc
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #       .bash_history
    #       .bash_logout
    #       .bash_profile
    #       .bashrc
    #       .composer/
    #       .lesshst
    #       .mysql_history
    #       .ssh/
    #       .swp
    #       .tmux-powerlinerc
    #       .tmux/
    #       .vim/
    #       .viminfo
    #       TEST
    #       dl.tar.gz
    #       doc/
    #       ds.zip
    #       gist414375-24c6049f621f07db839e8cdd133e396756d642db/
    #       nerdtree_plugin/
    #       plugin/
    #       public_html/
    #       regexp.txt
    #       test3.txt
    #       tmux-powerline/
    #       work/
  4. 追加したファイルを追加対象から外す

    • 使用コマンド:$ git rm --cached [file_name]
    • --cached:indexから外すが、ファイルは残す。
    • 実行結果
    $ git rm --cached gittest2.txt
    rm 'gittest2.txt'
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       new file:   gittest.txt
    #
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       modified:   .vimrc
  5. 再度ファイルを追加後、コミット

    • 使用コマンド:$ git commit -m "comment"
    • 実行結果
    $ git add gittest2.txt
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       new file:   gittest.txt
    #       new file:   gittest2.txt
    #
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       modified:   .vimrc
    (中略)
    $ git commit -m "first commit"
    [master 2d5193d] first commit
    Committer: EC2 Default User < *** *** .ap-northeast-1.compute.internal>
    Your name and email address were configured automatically based
    on your username and hostname. Please check that they are accurate.
    You can suppress this message by setting them explicitly:
    
    git config --global user.name "Your Name"
    git config --global user.email you@example.com
    
    After doing this, you may fix the identity used for this commit with:
    
     git commit --amend --reset-author
    
    2
    After doing this, you may fix the identity used for this commit with:
    
     git commit --amend --reset-author
    
    2 files changed, 2 insertions(+)
    create mode 100644 gittest.txt
    create mode 100644 gittest2.txt
  6. 追加したファイルを編集、保存し、差分を表示

    • 使用コマンド:$ git diff
    • 実行結果
    $ vim gittest2.txt
    $ git diff
    diff --git a/.vimrc b/.vimrc
    index eb43e2b..f0f63ad 100755
    --- a/.vimrc
    +++ b/.vimrc
    @@ -31,8 +31,3 @@
    inoremap <Up> <Nop>
    inoremap <Down> <Nop>
    inoremap <Left> <Nop>
    inoremap <Right> <Nop>
    -
    -"powerlineインストール
    -NeoBundle 'alpaca-tc/alpaca_powertabline'
    -NeoBundle 'https://github.com/Lokaltog/powerline.git'
    -
    diff --git a/gittest2.txt b/gittest2.txt
    index c754988..5bdb67b 100644
    --- a/gittest2.txt
    +++ b/gittest2.txt
    @@ -1 +1 @@
    -tetetest
    +testtesttest
  7. 編集した内容をGitコマンドを用いて編集前に戻す

    • 使用コマンド:$ git checkout -- [file_name]
    • 実行結果
    $ cat gittest2.txt
    testtesttest
    $ git checkout -- gittest2.txt
    $ cat gittest2.txt
    tetetest
  8. ファイルをGit管理から削除してコミット

    • 使用コマンド:$ git rm [file_name]
    • 実行結果
    $ ls -l
    total 136
    -rw-rw-r-- 1 *** ***   952 Apr  7 06:18 dl.tar.gz
    drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 doc
    -rw-rw-r-- 1 *** *** 44645 Apr  7 06:16 ds.zip
    drwxrwxr-x 2 *** ***  4096 May 26  2010 gist414375-24c6049f621f07db839e8cdd133e396756d642db
    -rw-rw-r-- 1 *** ***     9 Apr 17 07:51 gittest2.txt
    -rw-rw-r-- 1 *** ***     5 Apr 17 08:00 gittest.txt
    drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 nerdtree_plugin
    drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 plugin
    drwxrwxr-x 3 *** ***  4096 Mar 20 02:49 public_html
    -rw-rw-r-- 1 *** ***   153 Apr 14 07:08 regexp.txt
    -rw-rw-r-- 1 *** *** 43528 Apr  4 07:38 TEST
    -rw-rw-r-- 1 *** ***     9 Apr 17 05:18 test3.txt
    drwxrwxr-x 8 *** ***  4096 Apr 11 08:58 tmux-powerline
    drwxrwxr-x 2 *** ***  4096 Apr  8 06:08 work
    $ git rm gittest.txt
    rm 'gittest.txt'
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       deleted:    gittest.txt
    $ git commit -m "deleted gittest.txt"
    [master 687a9f3] deleted gittest.txt
    Committer: EC2 Default User < *** .ap-northeast-1.compute.internal>
    Your name and email address were configured automatically based
    on your username and hostname. Please check that they are accurate.
    You can suppress this message by setting them explicitly:
    
    git config --global user.name "Your Name"
    git config --global user.email you@example.com
    
    After doing this, you may fix the identity used for this commit with:
    fter doing this, you may fix the identity used for this commit with:
    git commit --amend --reset-author
    1 file changed, 1 deletion(-)
    delete mode 100644 gittest.txt
    $ git log
    commit 687a9f32b4ae5c1efb13ec588d8a3ce51ee6e2c3
    Author: EC2 Default User < *** .ap-north
    Date:   Thu Apr 17 08:06:25 2014 +0000
    deleted gittest.txt
    commit 2d5193d0e67578f850999c615dc74dd288626ba0
    Author: EC2 Default User < *** .ap-north
    Date:   Thu Apr 17 06:51:58 2014 +0000
    first commit
    commit 67cc9cae1a4276bc07afcff4ce37091d18039158
    Author: EC2 Default User < *** .ap-north
    Date:   Wed Apr 16 09:18:29 2014 +0000
    first commit
    $ ls -l
    total 132
    -rw-rw-r-- 1 *** ***   952 Apr  7 06:18 dl.tar.gz
    drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 doc
    -rw-rw-r-- 1 *** *** 44645 Apr  7 06:16 ds.zip
    drwxrwxr-x 2 *** ***  4096 May 26  2010 gist414375-24c6049f621f07db839e8cdd133e396756d642db
    -rw-rw-r-- 1 *** ***     9 Apr 17 07:51 gittest2.txt
    drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 nerdtree_plugin
    drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 plugin
    drwxrwxr-x 3 *** ***  4096 Mar 20 02:49 public_html
    -rw-rw-r-- 1 *** ***   153 Apr 14 07:08 regexp.txt
    -rw-rw-r-- 1 *** *** 43528 Apr  4 07:38 TEST
    -rw-rw-r-- 1 *** ***     9 Apr 17 05:18 test3.txt
    drwxrwxr-x 8 *** ***  4096 Apr 11 08:58 tmux-powerline
    drwxrwxr-x 2 *** ***  4096 Apr  8 06:08 work
  9. 直前のコミットを取り消し(ファイルを削除する前の状態に戻す)

    • 使用コマンド:$ git reset --hard HEAD^
    • 実行結果
    $ git log
    commit 687a9f32b4ae5c1efb13ec588d8a3ce51ee6e2c3
    Author: EC2 Default User < *** *** .ap-north
    Date:   Thu Apr 17 08:06:25 2014 +0000
    deleted gittest.txt
    commit 2d5193d0e67578f850999c615dc74dd288626ba0
    Author: EC2 Default User < *** *** .ap-north
    Date:   Thu Apr 17 06:51:58 2014 +0000
    first commit
    commit 67cc9cae1a4276bc07afcff4ce37091d18039158
    Author: EC2 Default User < *** *** .ap-north
    Date:   Wed Apr 16 09:18:29 2014 +0000
    first commit
    $ git reset --hard HEAD^
    HEAD is now at 2d5193d first commit
    $ git log
    commit 2d5193d0e67578f850999c615dc74dd288626ba0
    Author: EC2 Default User < *** *** .ap-north
    Date:   Thu Apr 17 06:51:58 2014 +0000
    first commit
    commit 67cc9cae1a4276bc07afcff4ce37091d18039158
    Author: EC2 Default User < *** *** .ap-north
    Date:   Wed Apr 16 09:18:29 2014 +0000
    first commit
    $ ls -l
    total 136
    -rw-rw-r-- 1 *** ***   952 Apr  7 06:18 dl.tar.gz
    drwxrwxr-x 2 *** ***   4096 Apr  7 06:17 doc
    -rw-rw-r-- 1  *** ***  44645 Apr  7 06:16 ds.zip
    drwxrwxr-x 2  *** ***   4096 May 26  2010 gist414375-24c6049f621f07db839e8cdd133e396756d642db
    -rw-rw-r-- 1  *** ***      9 Apr 17 07:51 gittest2.txt
    -rw-rw-r-- 1  *** ***      5 Apr 17 08:22 gittest.txt
    drwxrwxr-x 2  *** ***   4096 Apr  7 06:17 nerdtree_plugin
    drwxrwxr-x 2  *** ***   4096 Apr  7 06:17 plugin
    drwxrwxr-x 3  *** ***   4096 Mar 20 02:49 public_html
    -rw-rw-r-- 1  *** ***    153 Apr 14 07:08 regexp.txt
    -rw-rw-r-- 1  *** ***  43528 Apr  4 07:38 TEST
    -rw-rw-r-- 1  *** ***      9 Apr 17 05:18 test3.txt
    drwxrwxr-x 8  *** ***   4096 Apr 11 08:58 tmux-powerline
    drwxrwxr-x 2  *** ***   4096 Apr  8 06:08 work
  10. 今までのGitのログを表示

    • 使用コマンド:$ git log
    • 実行結果
    $ git log
    commit 2d5193d0e67578f850999c615dc74dd288626ba0
    Author: EC2 Default User < *** *** .ap-north
    Date:   Thu Apr 17 06:51:58 2014 +0000
    first commit
    commit 67cc9cae1a4276bc07afcff4ce37091d18039158
    Author: EC2 Default User < *** *** .ap-north
    Date:   Wed Apr 16 09:18:29 2014 +0000
    first commit
  11. いづれか1つのファイルをGit管理に含めないように設定

    • gitのローカルリポジトリのルートに.gitignore(無視ファイルリスト)を作成
    • 実行結果
    $ vim .gitignore
    $ cat .gitignore
    gittest2.txt
    $ vim gittest2.txt
    $ git status
    # On branch master
    # Changes not staged for commit:
    # (use "git add <file>..." to update what will be committed)
    # (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified: gittest2.txt
    #
    $ git rm --cached gittest2.txt
    rm 'gittest2.txt'
    [ec2-user@ip-10-134-139-29 ~]$ git status
    # On branch master
    # Changes to be committed:
    # (use "git reset HEAD <file>..." to unstage)
    #
    # deleted: gittest2.txt
    #
    $ vim gittest2.txt
    $ git add gittest2.txt
    The following paths are ignored by one of your .gitignore files:
    gittest2.txt
    Use -f if you really want to add them.
    fatal: no files added
  12. ブランチを作って移動

    • 使用コマンド
      • $ git branch:(引数なし)ブランチの表示(引数あり)ブランチを作成
      • $ git checkout [branch_name]:指定したブランチへ移動
    • 実行結果
    $ git branch
    * master
    $ git branch bugfix
    $ git branch
    bugfix
    * master
    $ git checkout bugfix
    D       gittest2.txt
    Switched to branch 'bugfix'
  13. 作ったブランチで新たな新規ファイルを追加、コミットした後、前のブランチにマージ

    • 使用コマンド:$ git merge [branch_name]
    • 実行結果
    $ git commit -m "added test3.txt to bugfix"
    [bugfix 3d23b6b] added test3.txt to bugfix
    Committer: EC2 Default User < *** *** .ap-northeast-1.compute.internal>
    2 files changed, 1 insertion(+), 1 deletion(-)
    delete mode 100644 gittest2.txt
    create mode 100644 test3.txt
    $ git branch
    * bugfix
    master
    $ git checkout master
    Switched to branch 'master'
    $ git branch
    bugfix
    * master
    $ git merge bugfix
    Updating 04c7047..3d23b6b
    Fast-forward
    gittest2.txt | 1 -
    test3.txt    | 1 +
    2 files changed, 1 insertion(+), 1 deletion(-)
    delete mode 100644 gittest2.txt
    create mode 100644 test3.txt
fr-sasaki commented 10 years ago

確認しました! 気になった点があるので下記をもう一度トライしてみてください。

これ以外は大丈夫でした!

fr-itaya commented 10 years ago

ご指摘を基に再チャレンジしました。如何でしょうか。


fr-sasaki commented 10 years ago

確認しました。問題ないです!! :+1: