LoginSignup
3
3

More than 5 years have passed since last update.

git リポジトリの利用方法

Last updated at Posted at 2016-05-29

リクルートさんではgitが扱えないと仕事にならないという話をウェブで見たりするのでgitの扱いをまとめておきましょう。
GitHubはコードのバージョン管理が出来る便利なサイトで基礎的な部分を抑えた後プログラミングを書くならここを見るに限ります。

環境はMacOSX 10.8

アカウント作成

まずアカウントがないと始まらないのでアカウントを作成しましょう
https://github.com/

gitのコマンドはMacの場合はhomebrewで落とせます。

gitインストール

$ brew install git

homebrewがない場合は事前にインストール

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

バージョン確認

$ git --version
git version 1.8.5.2 (Apple Git-48)

ローカルリポジトリ作成

使っているPC上で開発中のコード置き場を作ります

ディレクトリ作成

home directory下等に専用のディレクトリを作り
配下に移動します。

$ mkdir git_dev
$ cd git_dev

ローカルリポジトリとして初期化します。

$ git init
Initialized empty Git repository in /Users/*****/git_dev/.git/

.gitディレクトリが作成されていれば成功

$ ls -la
total 0
drwxr-xr-x   3 keigo  staff  102  5 29 10:46 .
drwxr-xr-x  18 keigo  staff  612  5 29 10:43 ..
drwxr-xr-x  10 keigo  staff  340  5 29 10:46 .git

対象ファイルをステージングエリアへ記録

test.jsを対象ファイルとしてgit_dev配下へ移動させ
ステージングエリアへ記録

$ git add test.js
# 変更したファイル全てをステージングエリアへ記録する時
$ git add --all
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   test.js

リポジトリのコミット

コミットメッセージの入力は -m オプションで短縮出来ます。

$ git commit -m "test commit"
[master (root-commit) e48a96e] test commit
 1 file changed, 15 insertions(+)

ログの確認

$ git log
commit e48a96ef2140df1feb028d7bb66305afb8b6b891
Author: JohnKeigo <**********@gmail.com>
Date:   Sun May 29 11:46:10 2016 +0900

    test commit

リモートリポジトリの登録

下記コマンドでリモートリポジトリを登録出来ます。

$ git remote add <name> <URL>

今回は私のテスト用のリポジトリを登録します

$ git remote add JohnKeigo https://github.com/JohnKeigo/hello-world

リモートリポジトリの確認

登録されているリモートリポジトリの確認はgit remoteで出来ます。
JohnKeigoの登録が確認出来ました。

$ git remote
JohnKeigo

詳細なリモートリポジトリ登録情報の確認

$ git remote show JohnKeigo
* remote JohnKeigo
  Fetch URL: https://github.com/JohnKeigo/hello-world
  Push  URL: https://github.com/JohnKeigo/hello-world
  HEAD branch: master
  Remote branches:
    master        new (next fetch will store in remotes/JohnKeigo)
    python-branch new (next fetch will store in remotes/JohnKeigo)
  Local ref configured for 'git push':
    master pushes to master (local out of date)

リモートリポジトリ操作

fetchでリモートトラッキングブランチ作成

$ git fetch JohnKeigo
warning: no common commits
remote: Counting objects: 14, done.
remote: Total 14 (delta 0), reused 0 (delta 0), pack-reused 14
Unpacking objects: 100% (14/14), done.
From https://github.com/JohnKeigo/hello-world
 * [new branch]      master     -> JohnKeigo/master
 * [new branch]      python-branch -> JohnKeigo/python-branch
 * 
localhost:git_dev keigo$ git branch -r
  JohnKeigo/master
  JohnKeigo/python-branch

fetchはこの後mergeしなければローカルのmasterは更新されません。

git pull

fetchしてMergeするより直感的で楽に感じます。
名前のごとくリモートリポジトリの情報をローカルへ
持って来ます。
ただしmergeまで一括で実行します。

temp.js をgithubのJohnKeigo/hello-worldリポジトリへアップロードしコミット後にpullしてみます。

$ git pull JohnKeigo master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/JohnKeigo/hello-world
 * branch            master     -> FETCH_HEAD
   ce56dd3..81c1ae9  master     -> JohnKeigo/master
Updating ce56dd3..81c1ae9
Fast-forward
 temp.js | 13 +++++++++++++
 1 file changed, 13 insertions(+)
 create mode 100644 temp.js

ローカルに同期されました。

git push

コミットされたローカルのリポジトリを
リモートのリポジトリへ書き込むコマンドです。

$ git push JohnKeigo master:master
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 698 bytes | 0 bytes/s, done.
Total 5 (delta 1), reused 0 (delta 0)
To https://github.com/JohnKeigo/hello-world
   cd5319e..ce56dd3  master -> master

本来複数人でコードのバージョン管理する事が主眼のサイトだと思います。次の記事で複数人での管理操作の手順も
確認して上げたいと思います。

参考ページ
Gitではじめるバージョン管理 〜ローカルリポジトリでファイルを管理してみよう〜
Git 再入門 リモートリポジトリを使った作業

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