Top/VersionCtl/git

Links

Git について

Downloads

使い方

git init : 新規リポジトリ作成

  1. 空リポジトリの作成
    $ mkdir hoge.git         # bare repository 用の空ディレクトリを作成
    $ cd hoge
    $ git init --bare --shared=true   # bare repository として初期化。管理ファイル群が生成される。共有にするには --shared=true にする
  2. 作業用リポジトリの作成
    $ cd ..
    $ git clone hoge          # hoge.git から hoge という名前で bare ではないリポジトリとして複製する
  3. 作業用リポジトリでファイルを登録
    $ cd hoge
    /* 何かしらの方法で新しいファイル fuga を置く */
    $ git add fuga           # ファイル "fuga" が登録対象になる(まだ登録されていない)
    $ git commit             # 前の git add で指定されたファイルが登録される 

git clone : git リポジトリの複製

git commit : 登録済みのファイルに変更が加えられた場合

git commmit --amend : commit をやり直す

.git

git revert : commit を取り消す

  1. まず、git log で対象 commit の hash を確認
    $ git log
    commit bdf1da8aecdfbe5a83ce93e78e2ad8a241b1530b
    Author: hogehoge <hogehoge@fuga.com>
    Date:   Mon May 4 13:33:13 2015 +0900
    
        fix build break for System
    
    commit 1072326e20c9dd482d5e1db46419b0412317d693
    Author: hogehoge <hogehoge@fuga.com>
    Date:   Mon May 4 13:12:33 2015 +0900
    
        delete Makefile.bak
    
    ...
  2. 対象を確認したら、 hash を指定して revert する
    $ git revert bdf1da8aecdfbe5a83ce93e78e2ad8a241b1530b
    • 即座に commit log 編集画面になる。

git reset: git commit や git add をなかったことにする

$ git reset [commit-hash]

commit を取り消すには

branch の名前

リポジトリの現在の branch 名を表示

$ git branch
* work

branch 名を変更する

$ git branch -m <元の名前> <新しい名前>

リポジトリの変更を他のリポジトリに反映する

$ git push <反映先> <反映元>

push 出来ない

branch 操作

branch 作成

branch 切り替え

不要になった branch の削除

他の branch に commit を反映する

$ git merge <反映先 branch>

tag

commit されていない変更点の確認

conflict への対処

git log : ログの表示(変更履歴の閲覧)

git reflog : リポジトリ上での git 作業履歴を表示する

git show : 詳細を表示する

git stash : ローカルの変更内容を隠蔽する

git stash : ローカルの変更を隠蔽する

$ git stash

git stash list : 隠蔽したローカルの変更一覧を表示する

$ git stash list

git stash pop : stash の最新を1つ適用し、stash のリストから削除する

$ git stash pop

git stash apply : stash を適用する

$ git stash apply

git stash drop : stash のリストからエントリを削除する

$ git stash frop

git rebase

分岐元のコミットを変更

分岐元の branch を変更する

rebase の再開・スキップ・中止

$ git rebase --continue | --skip | --abort
optiondescription
--continue競合解決中に、 rebase を再開したい場合に指定する
--skip競合解決中に、 その commit を skip したいときに指定する。skip した commit は適用されない
--abort競合解決中に、rebase を中止し、 rebase 開始前の状態に戻りたい場合に指定する

git remote : remote の repository を指定する

git bundle : 直接通信できないリポジトリと同期を取る

fetch と pull

git submodule : リポジトリに他のリポジトリを組み込む

submodule の組み込み

  1. foo.git の clone
    $ git clone http://foobar.com/foo.git
  2. foo 内にディレクトリ baz を作り、外部リポジトリの http://foobar.com/bar.git を組み込む
    $ cd foo
    $ git submodule add http://foobar.com/bar.git baz  # ここでディレクトリ foo/baz が作成される
    Cloning into 'baz'...
    done.
    $ ls -a
    .  ..  .git  .gitmodules  aa.txt  baz  # .gitmodules と baz が追加された
    
    # ここで、baz が初期化される場合とされない場合がある。
    # git submodule とtype したときに表示される hash に "-" がついていれば、 baz は初期化されていない。
    # この場合は次のように手動で初期化する必要がある
  3. baz の初期化
    $ git submodule init baz
    Submodule 'baz' (http://foobar.com/bar.git) registered for path 'baz'
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #	new file:   .gitmodules
    #	new file:   baz
    #
  4. 組み込んだ submodule を commit & push する。この時 git add は不要。
    $ git commit
    $ git push
    

submodule を組み込んだリポジトリを clone する

  1. foo を clone する
    $ git clone http://foobar.com/foo.git new-foo
    $ cd new-foo
    $ ls
    aa.txt  baz  # 先ほど追加し、 commmit & push した baz が出来ている
    $ ls -a baz
    .  ..     # 単にからディレクトリが出来ただけ。
    $ git submodule
    -386b630fd974b3f3147d462cda2c1f50813131fc baz    # hash に "-" が付いており、初期化されていないことが分かる
  2. submodule を初期化する
    $ git submodule init baz
    Submodule 'baz' (http://foobar.com/bar.git) registered for path 'baz'
    $ ls -a baz
    .  ..     # この段階でも、まだ baz は空ディレクトリのままになっている
  3. submodule の中身を最新にする
    $ git submodule update
    Cloning into 'baz'...
    done.
    Submodule path 'baz': checked out '386b630fd974b3f3147d462cda2c1f50813131fc'
    $ ls -a baz/
    .  ..  .git  ps-ax.log    # bar.git の中身が baz に clone される

git diff : 差分を表示する

git config : 各種設定

ユーザアカウントに関する設定

alias の設定

設定をエディタで編集する

$ git config <--local|--global|--system> <-e|--edit>

現在の設定を確認する

$ git config <-l|--list>

設定を削除する

.gitignore : git が無視するファイルを指定する

Subversion とのコマンド対比

GitSubversionDescription
git initsvnadmin createリポジトリ作成
git clonesvn checkoutリポジトリのコピー
git add <file>svn add <file>ファイルの追加
git add <file>; git commitsvn commit [file]ファイルの更新をリポジトリに反映
git rm <file>svn rm <file>ファイルの削除(別途、要 commit)
git statussvn status
git log [file]svn log [file]ログを表示する
git log --shortstat --name-statussvn log -v追加・変更されたファイル名を含めたログを表示する
git log -<number>svn log -l <number>最新から <number> 個のログエントリを表示する
git blame <file>svn blame <file><file> の各行がいつ誰によって変更されたかを表示する
git checkout <branch>svn switch <branch>
git merge <branch>svn merge <branch>
git checkout <file>svn revert <file>ファイルに対して行った(リポジトリに未反映の)変更を元に戻す
git clean -fsvn revert [dir]
git pull <remote> <local>svn up[date]<remote> の最新状態を <local> に merge する

tools

git-sh

tools on emacsen

magit

git-gutter+

git-gutter

GUI tools

gitk

git-gui


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS