$ mkdir hoge.git # bare repository 用の空ディレクトリを作成 $ cd hoge $ git init --bare --shared=true # bare repository として初期化。管理ファイル群が生成される。共有にするには --shared=true にする
$ cd .. $ git clone hoge # hoge.git から hoge という名前で bare ではないリポジトリとして複製する
$ cd hoge /* 何かしらの方法で新しいファイル fuga を置く */ $ git add fuga # ファイル "fuga" が登録対象になる(まだ登録されていない) $ git commit # 前の git add で指定されたファイルが登録される
$ git clone [options...] <repository> [<directory>]
option | description |
---|---|
--recursive | リポジトリ内に含まれる submodule も同時に複製する。 |
--bare | bare repository として clone する |
-o (--origin) <origin-repository> | 複製先で使用する、複製元のリポジトリ名を <oritin-repository> として指定する 指定しなかった場合は "origin" が使われる |
-b (--branch) <branch-name> | 複製先の HEAD が指すブランチの名前を <branch-name> で指定する。 指定しない場合は、複製元の HEAD が指すブランチが使われれる。 |
--morror | ミラーリポジトリとして複製元リポジトリを複製する。 複製元リポジトリ自体もミラーリポジトリだった場合は、その追跡ブランチなどの参照情報も、複製元から完全にコピーされる。 |
$ git add <変更を加えたファイル> $ git commit
(1)再度 commit する。履歴は全て別々に残る (2)commit をやり直す。履歴はやり直した分だけが残るの2つの選択肢がある。やり直しの対象となる commit のログを残したくない場合、やり直し時に "--amend" オプションをつけることで実現できる。
$ git add hoge $ git commit --amend
$ ls -l .git/ 合計 60 -rw-r--r-- 1 user1 user1 24 11月 22 18:13 COMMIT_EDITMSG -rw-r--r-- 1 user1 user1 293 10月 10 14:19 FETCH_HEAD -rw-r--r-- 1 user1 user1 48 10月 28 02:13 GITGUI_MSG -rw-r--r-- 1 user1 user1 23 5月 4 2015 HEAD -rw-r--r-- 1 user1 user1 41 10月 10 14:19 ORIG_HEAD drwxr-xr-x 2 user1 user1 6 5月 4 2015 branches -rw-r--r-- 1 user1 user1 315 10月 28 02:14 config -rw-r--r-- 1 user1 user1 73 5月 4 2015 description drwxr-xr-x 2 user1 user1 4096 5月 4 2015 hooks -rw-r--r-- 1 user1 user1 19357 11月 22 18:12 index drwxr-xr-x 2 user1 user1 20 5月 4 2015 info drwxr-xr-x 3 user1 user1 28 5月 4 2015 logs drwxr-xr-x 186 user1 user1 4096 9月 20 19:08 objects drwxr-xr-x 5 user1 user1 43 5月 4 2015 refs
name | description |
---|---|
HEAD | 現在チェックアウトされているブランチ(HEAD)がどういう参照情報化が書かれている |
config | この git リポジトリの設定内容 |
description | この git リポジトリの説明が記述される。 GitWeb でリポジトリを HTTP 公開したり、メールでコミットメッセージを送信するときなどに利用される。 |
hooks/ | このディレクトリは如何にフックスクリプトを配置する |
info/ | この git リポジトリで無視したいファイルを info/exclude に記述する |
objects/ | この git リポジトリで管理する git オブジェクトの実体がここに配置される |
refs/ | ブランチやタグが指す git オブジェクトがどれかという参照情報がここに置かれる |
$ 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 ...
$ git revert bdf1da8aecdfbe5a83ce93e78e2ad8a241b1530b
$ git reset [commit-hash]
$ git reset --hard AAAA^ # AAAA の1個前の commit まで戻る
$ git branch * work
$ git branch -m <元の名前> <新しい名前>
$ git push <反映先> <反映元>
option | description |
---|---|
-u, --set-upstream | 新規に作成したリポジトリやブランチを push する際に、ローカルリポジトリと push 先のリポジトリとのブランチ対応付けを行う。 対応付けを行うことにより、次回以降 push 時にブランチ名を省略できる。 |
--all | ブランチを全て push する |
--tags | 全ての tag を push する(*) |
--mirror | リモートリポジトリに、ローカルリポジトリの内容をそのまま複製したいときに使用する。ローカルで削除されたブランチはリモートでも削除される |
--delete | リモートリポジトリのブランチを削除する |
-n, --dry-run | 変更情報の送信とリモートリポジトリへの反映結果を確認する。実際にはリモートリポジトリへの反映は行われない |
-f, --force | リモートブランチがローカルブランチの派生元でない場合でも、強制的に上書きする。 リモートブランチのコミット情報を失う場合があるので、注意が必要 |
--progress | 標準エラーに進捗状況を出力する |
<repository> | push 対象となるリポジトリを指定する。リモート名または URI を指定する |
<refspec> | <src>:<dst> のフォーマットで送信元:送信先ブランチを指定する。例えば、branch-a から branch-b に push する場合、次のように指定する: branch-a:branch-b |
$ git push <dst-branch> <local-tag-name>:<remote-tag-name>
$ git config --add branch.<local-branch-name>.remote <remote-name> $ git config --add branch.<local-branch-name>.merge <remote-branch-name> $ git push -u <remote-branch-name> <local-branch-name>
$ git checkout work1
$ git checkout work1 Switched to branch 'work1'
$ git branch -d work1
$ git merge <反映先 branch>
$ git tag <tag-name>
$ git tag <tag-name> <commit-hash>
$ git push --tags # 全ての tag 情報を push する
$ git tag -d <tag-name>
$ git diff
$ git clean [-n]
$ git status
$ git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From /home/kazu/work/git_test/a 1413a9c..ac702ad master -> origin/master Auto-merging 1.txt CONFLICT (content): Merge conflict in 1.txt Automatic merge failed; fix conflicts and then commit the result.
AAAAA <<<<<<< HEAD BBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCC DDDD EEEEEEEEEEEEE ======= 111111111111111111 22222222222 33333333333333333333 4444444 >>>>>>> ac702ad3f09388ac676c926b7a03c2a97a77f94f
<<<<<<< HEAD (手元での修正) ======= (<commit-name> によって変更された修正) >>>>>>> <commit-name>
Merge branch 'master' of /path/repos-dir Conflicts: 1.txt # # It looks like you may be committing a merge. # If this is not correct, please remove the file # .git/MERGE_HEAD # and try again. ...
commit 772a23b56ce10fd0b986d84a265ed25d5723cee8 Author: user-name <hoge@example.com> Date: Sun Jul 5 12:16:20 2015 +0900 [mod] disc.shtml: modify link URI
commit 772a23b56ce10fd0b986d84a265ed25d5723cee8 Author: user-name <hoge@example.com> Date: Sun Jul 5 12:16:20 2015 +0900 [mod] disc.shtml: modify link URI disc.shtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
commit 772a23b56ce10fd0b986d84a265ed25d5723cee8 Author: user-name <hoge@example.com> Date: Sun Jul 5 12:16:20 2015 +0900 [mod] disc.shtml: modify link URI M disc.shtml
commit 772a23b56ce10fd0b986d84a265ed25d5723cee8 Author: user-name <hoge@example.com> Date: Sun Jul 5 12:16:20 2015 +0900 [mod] disc.shtml: modify link URI 1 1 disc.shtml
commit 772a23b56ce10fd0b986d84a265ed25d5723cee8 Author: user-name <hoge@example.com> Date: Sun Jul 5 12:16:20 2015 +0900 [mod] disc.shtml: modify link URI disc.shtml
$ git reflog fff7841 HEAD@{9}: pull: Merge made by the 'recursive' strategy. 9fdb195 HEAD@{10}: commit: Modify Link URI e56109d HEAD@{11}: reset: moving to HEAD^ d5df2c7 HEAD@{12}: reset: moving to d5df2c7858f198496fca335d22dec36a50bb91b0 69ab6a1 HEAD@{13}: pull: Fast-forward d5df2c7 HEAD@{14}: reset: moving to d5df2c7858f198496fca335d22dec36a50bb91b0 69ab6a1 HEAD@{15}: pull: Fast-forward d5df2c7 HEAD@{16}: reset: moving to d5df2c7858f198496fca335d22dec36a50bb91b0 69ab6a1 HEAD@{17}: commit: 再修正 d5df2c7 HEAD@{18}: commit: リンク集 URI 修正
$ git stash
$ git stash list
$ git stash pop
$ git stash apply
$ git stash frop
$ git remote add [-t <branch>] [-f] [--tags|--no-tags] [--mirror] <name> <url>
option | description | remark |
---|---|---|
-t <branch> | pull で更新内容を取得するリモートブランチを限定出来る。 | ブランチは複数指定可能 |
-f | リモートを設定した後、リモートリポジトリから fetch する | |
--tags | --no-tags | remote コマンドでは、デフォルトではリモートブランチのHEAD から参照出来る tag のみ取得する。 このオプションが指定されると、tag を取得しない。 | |
--mirror | リモートリポジトリがベアリポジトリの場合、push 実行時にローカルリポジトリの内容を常にリモートリポジトリに複製するようになる。 | バックアップ用途、他のサーバにリポジトリのコピーを置きたい場合に使用する。 |
rm <name> | リモート名 <name> の設定とローカルリポジトリに取得されたリモートブランチ(リモート追跡ブランチ)を削除する |
$ git bundle create <file> <git-rev-list-args>
$ git bundle create <file> hoge-tag..fuga
$ git bundle verify <file> [refname]
$ git bundle list-heads <file> [<refname>...]
$ ls hoge.bundle $ git clone hoge bundle hoge Cloning into 'hoge'... Receiving objects: 100% (1616/1616), 15.62 MiB | 0 bytes/s, done. Resolving deltas: 100% (629/629), done. Checking connectivity... done. warning: remote HEAD refers to nonexistent ref, unable to checkout.
$ cd hoge $ls -a . .. .git
$ git checkout master Branch master set up to track remote branch master from origin. Already on 'master' $ ls archive chat index.shtml menu.html pukiwiki-plugins wiki bbs img linux.css menu.shtml test
foo: http://foobar.com/foo.git # aa.txt が commit されている bar: http://foobar.com/bar.git # ps-ax.log が commit されている
$ git clone http://foobar.com/foo.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 は初期化されていない。 # この場合は次のように手動で初期化する必要がある
$ 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 #
$ git commit $ git push
$ 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 に "-" が付いており、初期化されていないことが分かる
$ git submodule init baz Submodule 'baz' (http://foobar.com/bar.git) registered for path 'baz' $ ls -a baz . .. # この段階でも、まだ baz は空ディレクトリのままになっている
$ 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 --no-prefixとする必要がある。
$ git config --global user.name "UserName" $ git config --global user.email "account@example.com"
$ git config --global alias.ci "commit" # "git commit" を "git ci" でも実行できるようにする $ git config --global alias.co "checkout" $ git config --global alias.st "status"
$ git config <--local|--global|--system> <-e|--edit>
$ git config <-l|--list>
$ git config [--local|global|--system] --unset <key>
$ git config [--local|global|--system] --unset-all
*.bak *.o tmp
$ git add .gitignore $ git ci -m "[add] .gitignore" $ git push
Git | Subversion | Description |
---|---|---|
git init | svnadmin create | リポジトリ作成 |
git clone | svn checkout | リポジトリのコピー |
git add <file> | svn add <file> | ファイルの追加 |
git add <file>; git commit | svn commit [file] | ファイルの更新をリポジトリに反映 |
git rm <file> | svn rm <file> | ファイルの削除(別途、要 commit) |
git status | svn status | |
git log [file] | svn log [file] | ログを表示する |
git log --name-status | svn log -v | 追加・変更されたファイル名を含めたログを表示する |
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 -f | svn revert [dir] | |
git pull <remote> <local> | svn up[date] | <remote> の最新状態を <local> に merge する |
$ gitk
$ git gui