Top/VersionCtl/git

Links

Git について

Downloads

使い方

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

  1. 空リポジトリの作成
    $ mkdir hoge.git         # bare repository 用の空ディレクトリを作成
    $ cd hoge.git
    $ 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 で指定されたファイルが登録される 

--shared= option

git checkout

分岐元となる remote branch を指定して local branch を切る

$ git checkout -b <local-branch-name> <remote-repository>/<remote-branch-name>

branch 作成

branch 切り替え

tag 指定で branch を切る

$ git checkout -b <new-local-branch-name> refs/tags/<tag-name>

特定のファイルだけ revert したいとき

  1. 変更を戻したいファイル、そのファイルの戻したい状態の commit-ID を指定して checkout する。
    $ git checkout <その状態に戻したい commit-ID> <revertしたいファイル>
  2. 差分を確認する。このとき、指定したファイルは HEAD と checkout した commit-ID の状態との差分になっているので、戻したくない部分があれば手動による差分編集で修正する。
  3. 想定通りの差分になったことを確認したら、 git add, git commit する。

ファイル名に NTFS の禁則文字が含まれるブランチを、 Windows 環境下で checkout するとき

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

git branch : branch 操作

リポジトリの branch 名を表示

branch 名を変更する

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

branch 切り替え

不要になった branch の削除

branch の複製

現在の branch を知る

git add : 変更差分を commit 対象に登録する

$ git add <変更を加えたファイル>

git add -p : 変更をファイル単位ではなく差分単位で add する

git commit : 変更差分を commit する

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

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

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

branch を明示して push する

$ git push <src-branch-name>:<dst-branch-name>

local で新規作成した branch を remote へ push する

$ git push <remote-repository> <local-new-branch-name>

push 出来ない

commit の取り消しをリモートに push する

push 出来る容量を変更する

.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 restore : ワークツリーファイルを復元

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

$ git reset [commit-ID]

commit を取り消すには

git switch :

git merge : 他の branch に commit を反映する

$ git merge <反映先 branch>

git cherry-pick

取り込む commit ID を自動的にログに残して cherry-pick する

git stash drop で消してしまった stash を復元する

merge commit を cherry-pick する

git tag

tag を remote repository に push する

tag の削除

特定の commit を含む全ての tag を検索する

tag の命名規則

git describe : 最新の tag からの commit 情報を表示する

最新の tag を取得する

$ git describe --tags --abbrev=0

git clean : リポジトリで管理されていないファイルの表示・削除

git fetch リモートリポジトリのデータを取得する

リモートリポジトリで削除されたブランチの情報を反映する

$ git fetch --prune ( git fetch -p )

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

conflict への対処

conflict を抑えるために

git pull --rebase した時の conflict への対処

$ git pull --rebase

高度なマージ手法 (git-scm.com)

git pull や git merge 時に発生した conflict

ファイル単位で現在の branch の内容にするか、merge する branch の内容にするのかを決める

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

--date : 日付の書式を指定する

git log -S <keyword> : <keyword> を差分に持つ commit の log を抽出する

git log --no-merges : branch 間の差分を commit 単位で表示する

git log --pretty=<format> : 指定したフォーマットでログを出力する

色々組み合わせてみる

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

消してしまった commit 履歴を元に戻す

  1. 現在の最新状況
    $ git log
    commit 47aea4296981269c1f3e4a779602ec0f649cfe5f
    Author: hoge <hoge@localhost>
    Date:   Sat Dec 24 00:28:19 2016 +0900
    
        Modify email page
        
        - input type of email-form (input type="email").
        - visual in confirm page.
    
    commit fe4cdb4c96822ab2ea5418b0627795b5bd6f83ce
    Author: hoge <hoge@localhost>
    Date:   Fri Dec 23 15:54:32 2016 +0900
    
        Modify comment.
    
    commit be940a58ff8b33e33e8211b390f4538a06b1803c
    Author: hoge <hoge@localhost>
    Date:   Fri Dec 23 15:51:12 2016 +0900
    
        Modify Design, remove unnesessary CSS definition.
    
    commit 85af578464c613ade32420a25c267dd2f77a31c4
    Author: hoge <hoge@localhost>
    Date:   Thu Dec 22 01:08:21 2016 +0900
    
        Modify design.
  2. git reset で履歴を消す。
    $ git reset --hard HEAD^^
    $ git log
    commit be940a58ff8b33e33e8211b390f4538a06b1803c
    Author: hoge <hoge@localhost>
    Date:   Fri Dec 23 15:51:12 2016 +0900
    
        Modify Design, remove unnesessary CSS definition.
    
    commit 85af578464c613ade32420a25c267dd2f77a31c4
    Author: hoge <hoge@localhost>
    Date:   Thu Dec 22 01:08:21 2016 +0900
    
        Modify design.
  3. やはり消した履歴を復旧したくなったので、reflog から戻すことにする。そこで reflog を確認
    $ git reflog
    be940a5 HEAD@{0}: reset: moving to HEAD^^
    47aea42 HEAD@{1}: reset: moving to 47aea42
    dc324eb HEAD@{2}: checkout: moving from 057c6eb522986eb442cfcb5a968c8467271af319 to dc324eb8481e1b0f9dd3f1ffc1b19177653bec4b
    057c6eb HEAD@{3}: checkout: moving from 49e4a499eb67f0eac4898241c52a6b7287d35d86 to 057c6eb522986eb442cfcb5a968c8467271af319
    49e4a49 HEAD@{4}: checkout: moving from ba052fa7aaf00cd0186cfb6b367d909f7f0f773b to 49e4a499eb67f0eac4898241c52a6b7287d35d86
    ba052fa HEAD@{5}: checkout: moving from 85af578464c613ade32420a25c267dd2f77a31c4 to ba052fa7aaf00cd0186cfb6b367d909f7f0f773b
    85af578 HEAD@{6}: checkout: moving from master to 85af578464c613ade32420a25c267dd2f77a31c4
    47aea42 HEAD@{7}: commit: Modify email page
    fe4cdb4 HEAD@{8}: commit: Modify comment.
    be940a5 HEAD@{9}: commit: Modify Design, remove unnesessary CSS definition.
    85af578 HEAD@{10}: commit: Modify design.
  4. 元々の最新の commit を上記から特定する。ここでは下記に復旧することとする:
    47aea42 HEAD@{7}: commit: Modify email page
  5. reflog から履歴を復旧する
    $ git reset --hard 47aea42
  6. 結果を確認。
    $ git log
    commit 47aea4296981269c1f3e4a779602ec0f649cfe5f
    Author: hoge <hoge@localhost>
    Date:   Sat Dec 24 00:28:19 2016 +0900
    
        Modify email page
        
        - input type of email-form (input type="email").
        - visual in confirm page.
    
    commit fe4cdb4c96822ab2ea5418b0627795b5bd6f83ce
    Author: hoge <hoge@localhost>
    Date:   Fri Dec 23 15:54:32 2016 +0900
    
        Modify comment.
    
    commit be940a58ff8b33e33e8211b390f4538a06b1803c
    Author: hoge <hoge@localhost>
    Date:   Fri Dec 23 15:51:12 2016 +0900
    
        Modify Design, remove unnesessary CSS definition.
    
    commit 85af578464c613ade32420a25c267dd2f77a31c4
    Author: hoge <hoge@localhost>
    Date:   Thu Dec 22 01:08:21 2016 +0900
    
        Modify design.
    • 元に戻ったことを確認できた
  7. 以上。

git reflog のその他の利用方法

git show : 詳細を表示する

git show-branch

その commit が属する branch / tag の情報を表示する

git blame

$ git blame <file-name>

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 drop

git rebase

分岐元のコミットを変更

分岐元の branch を変更する

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

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

git rebase -i 履歴の順番を入れ替える

  1. このコマンドを実行すると、まずテキストエディタによる履歴編集画面が表示される。
     pick 028c868 [Process] Fix Unit build error.
     pick d4dbaa3 Modify for fix build error (In progress)
     pick 9581c48 add static
     
     # Rebase 60802d1..9581c48 onto 60802d1
     #
     # Commands:
     #  p, pick = use commit
     #  r, reword = use commit, but edit the commit message
     #  e, edit = use commit, but stop for amending
     #  s, squash = use commit, but meld into previous commit
     #  f, fixup = like "squash", but discard this commit's log message
     #  x, exec = run command (the rest of the line) using shell
     #
     # These lines can be re-ordered; they are executed from top to bottom.
     #
     # If you remove a line here THAT COMMIT WILL BE LOST.
     #
     # However, if you remove everything, the rebase will be aborted.
     #
     # Note that empty commits are commented out
    ここで、
    • "pick" で始まる行が、各 commit のエントリになる
    • "pick" で始まる行は、上から下へ行くに従い、新しい履歴という扱いになっている。これらの行を入れ替えることにより、履歴順を操作することになる。
  2. これを編集し終了すると、実際の履歴入れ替え処理が始まる。
  3. 必要に応じて conflict の解消、git rebase --continue (途中で止める場合は --continue ではなく --abort を指定する)を実行する。

git remote : リモートリポジトリ操作

git remote add : リモートリポジトリを追加する

git remote -v : リモートリポジトリ一覧を表示する

git remote rm : リモートリポジトリを削除する

git remote prune

git remote rename : リモートリポジトリに付けた名前を変更する

git remote set-url : リモートリポジトリの URL を変更する

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

git pull

git pull --rebase

branch を指定した pull

--all

--no-commit

--no-ff

git pull も git push も出来ない場合の対処

fetch と pull

git notes : commit に注釈を付ける

git notes add : 注釈がまだ付けられていない commit に対して注釈を追加

git notes : リポジトリに保存されている全ての注釈の hash 一覧を表示

git notes edit : commit の注釈をエディタで編集

git notes remove : commit に付けられた注釈を削除

git notes append : 注釈を追記する

notes の同期

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 は初期化されていない。
    # この場合は次のように手動で初期化する必要がある
    • git submodule add で以下のようなエラーが出ることがある:
      $ git submodule add http://foobar.com/bar.git
      Cloning into 'http://foobar.com/bar'...
      fatal: transport 'file' not allowed
      fatal: clone of 'http://foobar.com/bar.git' into submodule path '<path>/bar' failed
      • この場合は、以下を実行することで解決する:
        $ git config --global protocol.file.allow always
      • 上記 git config コマンドにより、${HOME}/.gitconfig には以下のように追記される:
        [protocol "file"]
        	allow = always
  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  # 先ほど追加し、 commit & 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 lfs : 大容量ファイルを扱う

使い方

参考リンク

git bisect

参考リンク

git config : 各種設定

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

alias の設定

defalut の設定を無効にする

設定項目

configuration itemdescriptionremark
user.nameユーザ名を指定する
user.emailユーザのメールアドレスを指定する
receive.denyNonFastFowardstrue に設定すると、 push 時に repository 上の commit が含まれない commit を送信しようとしたときに branch の更新を拒否するshared repository で設定する
receive.denyDeletestrue に設定すると、ブランチの削除を拒否するshared repository で設定する
receive.fsckObjectstrue にすると、push されたときにデータ一貫性のチェックを行う。信頼性が向上するが、 push 処理に時間が掛かるようになる。shared repository で設定する
core.pagerpager を指定する例えばless, lv など
alias.<alias-name>任意の git コマンド(commit, checkoutなど)を <alias-name> でも呼び出せるようにする
fetch.prune [#git-config-prune]true にすると、リモートリポジトリで削除されたブランチ情報が fetch 時に反映される

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

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

現在の設定を確認する

$ git config <-l|--list>

メール送信の設定

設定を削除する

git gc : リポジトリを最適化する

optiondescription
--aggressive最適化の度合いを大きくする。
処理時間がかかるが、その分最適化された状態にする。
--prune=<date><date> で指定されたものより古いオブジェクトを破棄する。
右辺に何も指定しなかった場合のデフォルト値は "2 weeks ago" となる。
また、右辺に "now" を指定すると、全ての不要なオブジェクトが削除される。

reflog の削除

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

git fsck : git リポジトリを検査する

"missing blob <HSA1>" のようなエラーが出た場合の対処

dangling object を削除する

リポジトリからファイルを完全削除する

  1. まず、 git filter-branch を実行
    $ git filter-branch --tree-filter 'git rm --ignore-unmatch <file-to-be-deleted>' --prune-empty -- --all
    • <file-to-be-deleted> の対象になるファイルが複数あり、パターンマッチで実行する場合は下記のようにする(例は *.o を消す場合)
      $ git filter-branch --tree-filter 'git rm --cached --ignore-unmatch "*.o"' --prune-empty -- --all
    • <file-to-be-deleted> がディレクトリの場合は、その下の階層も対象になるため、クォーテーションしている git rm に -r オプションが必要になる。
  2. reflog にも残っているので、これも削除する
    $ git reflog expire --expire=now --all
    $ git gc --prune=now
    $ git gc --aggressive --prune=now

エラーになる場合の対処

git worktree : 1つのローカルリポジトリで複数のブランチを同時使用する

別のブランチを新たな作業ツリーとしてチェックアウトする

状態を見る

不要になった作業ツリーを削除する

未確認コマンド

git worktree lock
git worktree move
git worktree prune
git worktree repair
git worktree unlock

用語

Fast-forward

<refspec>

tools

git-sh

tools on emacsen

magit

git-gutter+

git-gutter

GUI tools

gitk

qgit

git-gui

リポジトリのホスト間の移動


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2024-04-14 (日) 09:26:35