Skip to content

Instantly share code, notes, and snippets.

@itokami1123dev
Last active December 18, 2015 15:01
Show Gist options
  • Save itokami1123dev/964165222083163df87f to your computer and use it in GitHub Desktop.
Save itokami1123dev/964165222083163df87f to your computer and use it in GitHub Desktop.
git初心者勉強会 2015/9/11(金)19:30 福岡

git初心者勉強会

概要

内容

1.リポジトリを作ろう

コマンド

# 作業ディレクトリを作成します。
mkdir study_git

# 移動します
cd study_git

# gitの状態を確認
git status
## fatal: Not a git repository
## (or any of the parent directories): .git

# 初期化コマンド
git init
## Initialized empty Git repository in
##  /Users/xxx/Documents/work/gitstudy/study_git/.git/

# 確認
ls -a
## .    ..   .git
## (.gitが出来てる!)

# gitの状態を確認
git status

## gitの管理が始まったよ
## On branch master
##
## Initial commit
##
## nothing to commit (create/copy files and use "git add" to track)


# 一旦 gitの管理を外れてもう一回作り直りしてみる
rm -rf .git/

# gitの管理から外れることを確認しよう
git status
## fatal: Not a git repository (or any of the parent directories): .git

# 次章にそなえてもう一回作成する
git init
git status

まとめ

  • gitは .git があるディレクトリ(サブディレクトリも)以下を監視します。
  • git status で 状態を確認しましょう

2.ファイルを追加しよう

コマンド

# hello.txtを追加(テキストエディタで編集)
- gedit hello.txt
- vim hello.txt
- Windowsのメモ帳などで hello.txt 開く

# 下記テキストを記入
## konchiha sekai


# 確認
git status

# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# 	hello.txt
#
# nothing added to commit but untracked files
# present (use "git add" to track)

## ☆ Untracked files とは ..

# 新しく追加したファイルをgitに教える
## ファイルの追跡を開始する
git add hello.txt

# 確認
git status

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
# 	new file:   hello.txt  

## ☆ unstage とは..

# 変更履歴の登録
## ファイルのスナップショットを歴史に書き込む
git commit -m "hajimete commit"
# [master (root-commit) a2c18d9] hejimete commit
#  1 file changed, 1 insertion(+)
#  create mode 100644 hello.txt

# 確認
git status

# On branch master
# nothing to commit, working directory clean

# 確認
git log

# commit 履歴の位置を示す番号
# Author: コミットした人の名前 <メール>
# Date:   Mon Sep 7 06:48:35 2015 +0900
#
#    hejimete commit
#

# 詳細に確認する
git log -p

# ざっくり確認する
git log --oneline

###
### 復習
###

# 追加してみよう
aiueo.txt
## aiueo aiueo

git add aiueo.txt
git status
git commit -m "add aiueo.txt"
git status
git log

# 追加してみよう2
test.txt
## test test

git add test.txt
git status
git commit -m "add test.txt"
git status
git log

まとめ

  • コミット対象にする為に git add する
  • add されたファイルは stage の上に登録される
  • stage の上に登録されているものが commit対象になる
  • git commit で addされたものをコミットする
  • git log でコミット履歴を表示する

3.ファイルを更新しよう

コマンド

# hello.txt の末尾に1行追加します
# 何かテキストエィデタで足す

## konchiha sekai
## konbanha

# 変更内容を確認する
git diff
# diff --git a/hello.txt b/hello.txt
# index f9f5e77..11e5cfd 100644
# --- a/hello.txt
# +++ b/hello.txt
# @@ -1 +1,2 @@
#  konchiha sekai
# +konbanha
# (END)

## 追加内容の左横に + がある

# 状態を確認する
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:   hello.txt
#
# no changes added to commit (use "git add" and/or "git commit -a")
#

## ☆ modified ってなってる

# 変更を登録
git add hello.txt
git commit -m "update hello.txt"
git log

### 復習 ###
hello.txt に何か加えてコミットを2回
## example hogehoge
## example fugafuga

git add hello.txt
git commit -m "update2 hello.txt"
git log

まとめ

  • 修正もコミット対象にする為に git add する
  • add されたファイルは stage の上に登録される
  • stage の上に登録されているものが commit対象になる
  • git commit で addされたものをコミットする
  • git log でコミット履歴を表示する

4.別の作業場所(別のブランチ)で更新してみよう

コマンド

# 作業場所(ブランチ)一覧を確認しよう
git branch
# * master

## ☆ 今いるブランチの左に * がつきます

# 別の作業場所を作ってみましょう
git branch apple

# 一覧で確認
git branch -v

#  apple  8550a2f update3 hello.txt
#* master 8550a2f update3 hello.txt

## * マークの位置でmasterにいること
## コミット番号が同じなので
## masterと appleは同じであることがわかる

# 作業場所(apple)に移動
git checkout apple

git branch
# * apple
#  master

## ☆ appleに移動したことがわかります

# hello.txt を更新しましょう
# konchiha を konbanwa に更新
gedit hello.txt
vim hello.txt

git diff
# diff --git a/hello.txt b/hello.txt
# index bbcf46f..5381f68 100644
# --- a/hello.txt
# +++ b/hello.txt
# @@ -1,4 +1,4 @@
# -konchiha sekai
# +konbanwa sekai
#  konbanha
#  hogehoge
#  fugafuga

git add hello.txt
git commit -m "update hello in apple"
git log

# 最初の作業場所(master)ブランチに戻る
git checkout master
# Switched to branch 'master'

# appleで更新した内容を master に反映する

# まず確認
git diff apple

# diff --git a/hello.txt b/hello.txt
# index 5381f68..bbcf46f 100644
# --- a/hello.txt
# +++ b/hello.txt
# @@ -1,4 +1,4 @@
# -konbanwa sekai
# +konchiha sekai
#  konbanha
#  hogehoge
#  fugafuga

## ☆ appleの修正点と masterの内容の違いがわかる

# appleの変更点をmasterに反映(マージ)する
git merge apple
# Updating 8550a2f..e06850f
# Fast-forward
#  hello.txt | 2 +-
#  1 file changed, 1 insertion(+), 1 deletion(-)

# appleの作業内容がmasterに反映されていることを確認
git log

### 復習 ###
# orange ブランチで作業してmasterに反映させよう

git branch orange
git checkout orange
git branch
# apple
# master
# * orange

# hello.txtを変更
# 3行目4行目を消すなど
vim hello.txt

git diff
# diff --git a/hello.txt b/hello.txt
# index 5381f68..5692d36 100644
# --- a/hello.txt
# +++ b/hello.txt
# @@ -1,4 +1,2 @@
#  konbanwa sekai
#  konbanha
# -hogehoge
# -fugafuga

git add hello.txt
git commit -m "update hello.txt in orange"
git log
git checkout master
git merge orange
git log

5.GitHubにリポジトリをつくろう

GitHubの画面をプロジェクタで写しながら説明する予定 sample という名前のプロジェクトを作成します。

  1. 右下緑の "+ New Repository" ボタンをクリック

  2. Repository name に sample

  3. create repository

  4. "SSH"ボタンを押してください

  5. "…or create a new repository on the command line"通りにコマンドを打ちます

☆ git remote add でリモート(インターネット上のサーバ)の場所を登録してます ☆

  1. GitHubの画面で自分が作ったプロジェクトができてます

  2. プロジェクトにファイルを追加してみましょう

# hello.txt の追加
vim hello.txt

## 例
## konchiha sekai

git add hello.txt
git commit -m "add hello.txt"
git log

## リモートリポジトリ(GitHub)への接続先を確認する
git remote -v

# origin	git@github.com:xxx/sample.git (fetch)
# origin	git@github.com:xxx/sample.git (push)

## リモートリポジトリ(GitHub)への変更を送る
### origin ← git@github.com:xxx/sample.git
### :の左 ローカル(自分のPC)のリポジトリ
### :の右 リモート(GitHub)のリポジトリ
git push origin master:master

# Counting objects: 3, done.
# Delta compression using up to 4 threads.
# Compressing objects: 100% (2/2), done.
# Writing objects: 100% (3/3), 295 bytes | 0 bytes/s, done.
# Total 3 (delta 0), reused 0 (delta 0)
# To git@github.com:xxx/sample.git
#   xxxxxxx..xxxxxxx  master -> master

GitHubの画面でファイルが追加されていることを確認しましょう

## 復習
# hello.txt を変更して GitHubに変更をおくりましょう
vim hello.txt
git add hello.txt
git commit -m "update hello.txt"
git log
git remote -v
git push origin master:master

6.人のプロジェクトに修正(プルリクエスト)を送ってみよう

  1. GitHubの画面で参加したいプロジェクトのページを開きます

  2. 右上の "Fork"ボタンを押します

  3. Where should we fork this repository? と聞かれますので自分を選びます

  4. 自分のリポジトリにForkしたリポジトリができます

自分の名前/プロジェクト名
forked from Forkした相手の名前/プロジェクト名
  1. 右下のURLで自分のPCの中に git のフォルダを作ります
git clone git@github.com:自分の名前/プロジェクト名.git
# Cloning into 'プロジェクト名'...
# remote: Counting objects: 3, done.
# remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
# Receiving objects: 100% (3/3), done.
# Checking connectivity... done.

cd プロジェクト名

# gitのフォルダができていることを確認
git status
# On branch master
# Your branch is up-to-date with 'origin/master'.
# nothing to commit, working directory clean
  1. ブランチを作ってファイルを修正してみましょう
# apple ブランチ(作業場所)を作る
git branch apple

# 作った作業場所に移動する
git checkout apple
# Switched to branch 'apple'

# appleにいることを確認
git branch -v
# * apple  90f75e4 first commit
#   master 90f75e4 first commit

# statusでもわかる
git status
# On branch apple
# nothing to commit, working directory clean

# README.md を修正
vim README.md

# 例)
# # sample999
#
# hello hello

git diff
git add README.md
git commit -m "update README"

git log --oneline
# 5579ff5 update README
# 90f75e4 first commit

# ☆自分のリモートリポジトリに送る☆

# 接続先確認
git remote -v
# origin	git@github.com:xxx/yyyy.git (fetch)
# origin	git@github.com:xxx/yyyy.git (push)

# 自分のPCの中の(ローカルリポジトリの中の)
# appleブランチをリモート先のappleブランチに送る
git push origin apple:apple
  1. GitHubのページに "Compare & pull request" ボタンができるので押す

  2. プルリクエスト(変更内容をFork元の人に送る)画面になりますので "Create pull Request" を押す

  3. Fork元の人に修正が飛んでいきます。

  4. Fork元の人は右側の Pull requestsを押します

  5. 先ほど送ったプルリクエストが Fork元に届いていますので "Merge pull request"ボタンを押します そうすると送られてきた修正が取り込まれます

  6. もう一回修正を送る

# 最新のソースをFork元から取得するため
# Fork元を remote 先に追加する
# Fork元の名前を upstream にすることが多い気がします

git remote add upstream git@github.com:Fork元の人の名前/プロジェクト名.git

# 確認
git remote -v
# origin	git@github.com:自分の名前/プロジェクト名.git (fetch)
# origin	git@github.com:自分の名前/プロジェクト名.git (push)
# upstream	git@github.com:Fork元の人の名前/プロジェクト名.git (fetch)
# upstream	git@github.com:Fork元の人の名前/プロジェクト名.git (push)

# masterブランチ(最初の作業場所)に戻る
git checkout master

# ログがちょっと古い
git log

# Fork元から最新ソースを取得
git fetch upstream
# remote: Counting objects: 1, done.
# remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0
# Unpacking objects: 100% (1/1), done.
# From github.com:Fork元の人/プロジェクト
#  * [new branch]      master     -> upstream/master

# 取ってきた最新ソースと自分のmasterソースを混ぜます
git merge upstream/master

# 最新のソースを撮ってきていることをログで確認
git log

# 最新ソースを自分のリモートリポジトリにも保存
git push origin master:master

### 復習 ###
git branch orange
git checkout orange
git branch -v

# README.md を修正
vim README.md

# 例)
# # sample999
#
# hello hello
# foge foge

git commit -m "update fogefoge"
git push origin orange:orange

## ブラウザで開いてプルリクエストを送る
  1. いらなくなったブランチを消す
git branch -d apple
# Deleted branch apple (was 5579ff5).

git push origin :apple
# To git@github.com:自分の名前/プロジェクト名.git
#  - [deleted]         apple

git初心者への道 - お仕事で困らないレベルまでググっとします。
https://gist.github.com/yatemmma/6486028

Git初心者に捧ぐ!Gitの「これなんで?」を解説します http://kray.jp/blog/git-why-explanation/

git diff の使い方がほんの少し理解できた
http://d.hatena.ne.jp/murank/20110320/1300619118

今さら聞けないgit pushコマンド http://shoma2da.hatenablog.com/entry/2014/03/08/234523

git push -u オプションの意味 http://qiita.com/ironsand/items/6c301fef730d53f35bc3

説明する環境

  • CentOS6.7 (Virtual box/Windows)

  • teminal: Monospace 22

  • 解像度: 1920 x 1080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment