「Gitでよくやること」の版間の差分

提供: オレッジベース
ナビゲーションに移動 検索に移動
ページの作成:「=== リポジトリの作成 === <syntaxhighlight lang="bash"> $ mkdir <DIR> $ cd <DIR> $ git init --bare --shared </syntaxhighlight> === clone === <syntaxhighlight lang...」
 
編集の要約なし
 
(同じ利用者による、間の25版が非表示)
7行目: 7行目:


=== clone ===
=== clone ===
==== とりあえずclone ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git clone <REPOSITORY_PATH>
$ git clone <REPOSITORY_PATH>
</syntaxhighlight>
==== ディレクトリ指定 ====
<syntaxhighlight lang="bash">
$ git clone <REPOSITORY_PATH> <DIR>
</syntaxhighlight>
=== remote ===
==== 一覧 ====
<syntaxhighlight lang="bash">
$ git remote
</syntaxhighlight>
==== 追加 ====
<syntaxhighlight lang="bash">
$ git remote add <REMOTE_NAME> <REPOSITORY>
</syntaxhighlight>
==== 削除 ====
<syntaxhighlight lang="bash">
$ git remote rm <REMOTE_NAME>
</syntaxhighlight>
==== 名前変更 ====
<syntaxhighlight lang="bash">
$ git remote rename <OLOD_NAME> <NEW_NAME>
</syntaxhighlight>
=== fetch ===
==== とりあえずfetch ====
<syntaxhighlight lang="bash">
$ git fetch
</syntaxhighlight>
==== remoteで削除されたブランチをローカルでも削除 ====
<syntaxhighlight lang="bash">
$ git fetch --prune
</syntaxhighlight>
</syntaxhighlight>


=== pull ===
=== pull ===
==== とりあえずpull ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git pull
$ git pull
</syntaxhighlight>
==== remoteで削除されたブランチをローカルでも削除 ====
<syntaxhighlight lang="bash">
$ git pull --prune
</syntaxhighlight>
==== Git2.27.0以降でのwarning ====
<syntaxhighlight lang="text">
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint:
hint:  git config pull.rebase false  # merge (the default strategy)
hint:  git config pull.rebase true  # rebase
hint:  git config pull.ff only      # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
</syntaxhighlight>
===== fetch + merge(デフォルト) =====
<syntaxhighlight lang="bash">
$ git config pull.rebase false
</syntaxhighlight>
or
<syntaxhighlight lang="bash">
$ git config --global pull.rebase false
</syntaxhighlight>
===== fetch + rebase =====
<syntaxhighlight lang="bash">
$ git config pull.rebase true
</syntaxhighlight>
or
<syntaxhighlight lang="bash">
$ git config --global pull.rebase true
</syntaxhighlight>
===== fast-forwardのみ =====
<syntaxhighlight lang="bash">
$ git config pull.ff only
</syntaxhighlight>
or
<syntaxhighlight lang="bash">
$ git config --global pull.ff only
</syntaxhighlight>
</syntaxhighlight>


22行目: 99行目:


=== add ===
=== add ===
とりあえず全部
==== とりあえず全部 ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git add -A
$ git add -A
</syntaxhighlight>
</syntaxhighlight>
新規作成 or 変更されたもの
==== 新規作成 or 変更されたもの ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git add .
$ git add .
</syntaxhighlight>
</syntaxhighlight>
変更があったもののみ
==== 変更があったもののみ ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git add -u
$ git add -u
36行目: 113行目:


=== commit ===
=== commit ===
==== とりあえずcommit ====
<syntaxhighlight lang="bash">
$ git commit
</syntaxhighlight>
==== メッセージ付きでcommit ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git commit -m "<MESSAGE>"
$ git commit -m "<MESSAGE>"
</syntaxhighlight>
==== 無言でcommit ====
<syntaxhighlight lang="bash">
$ git commit --allow-empty-message -m ""
</syntaxhighlight>
==== 空のcommit ====
<syntaxhighlight lang="bash">
$ git commit --allow-empty
</syntaxhighlight>
==== commitの修正 ====
<syntaxhighlight lang="bash">
$ git commit --amend
</syntaxhighlight>
==== dry run ====
<syntaxhighlight lang="bash">
$ git commit --dry-run
</syntaxhighlight>
</syntaxhighlight>


=== push ===
=== push ===
==== とりあえずpush ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git push <REMOTE> <BRANCH_NAME>
$ git push
</syntaxhighlight>
==== 別名でpush ====
<syntaxhighlight lang="bash">
$ git push origin <LOCAL_BRANCH>:<REMOTE_BRANCH>
</syntaxhighlight>
==== dry run ====
<syntaxhighlight lang="bash">
$ git push --dry-run
</syntaxhighlight>
or
<syntaxhighlight lang="bash">
$ git push -n
</syntaxhighlight>
</syntaxhighlight>


60行目: 171行目:


=== checkout ===
=== checkout ===
==== 特定のコミットをcheckout ====
<syntaxhighlight lang="bash">
$ git checkout <COMMIT_ID>
</syntaxhighlight>
==== ブランチ切り替え ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git checkout <BRANCH_NAME>
$ git checkout <BRANCH_NAME>
</syntaxhighlight>
</syntaxhighlight>
or 新しくブランチを持ってくる場合
==== 新しくブランチをremoteから持ってくる ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git checkout -b <BRANCH_NAME> <REMOTE>/<BRANCH_NAME>
$ git checkout -b <BRANCH_NAME> <REMOTE>/<BRANCH_NAME>
69行目: 185行目:
e.g
e.g
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git checkout -b develop origen/develop
$ git checkout -b develop origin/develop
</syntaxhighlight>
</syntaxhighlight>
or 以下のようなエラー出る場合
 
<pre>
==== checkoutできないとき ====
エラー内容
<syntaxhighlight lang="text">
fatal: git checkout: updating paths is incompatible with switching branches.
fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote-name' which can not be resolved as commit?
Did you intend to checkout 'origin/remote-name' which can not be resolved as commit?
</pre>
</syntaxhighlight>
こうする
こうする
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
85行目: 203行目:


=== stash ===
=== stash ===
とりあえず面倒なとき
==== とりあえず面倒なとき ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git stash
$ git stash
</syntaxhighlight>
</syntaxhighlight>
メッセージ付きでstash
==== メッセージ付きでstash ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git stash save "<MESSAGE>"
$ git stash save "<MESSAGE>"
</syntaxhighlight>
</syntaxhighlight>
一覧
==== 一覧 ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git stash list
$ git stash list
</syntaxhighlight>
</syntaxhighlight>
=== log ===
==== 1行表示 ====
<syntaxhighlight lang="bash">
$ git log --oneline
</syntaxhighlight>
==== グラフィカル表示 ====
<syntaxhighlight lang="bash">
$ git log --graph
</syntaxhighlight>
==== 追加/削除行数 ====
<syntaxhighlight lang="bash">
$ git log --numstat
</syntaxhighlight>
==== 変更したファイル ====
<syntaxhighlight lang="bash">
$ git log --name-status
</syntaxhighlight>
==== 絞り込み ====
===== 期間(相対値) =====
<syntaxhighlight lang="bash">
$ git log --since="10 days ago" --until="2 days ago"
</syntaxhighlight>
===== 期間(絶対値) =====
<syntaxhighlight lang="bash">
$ git log --since="2019/03/01" --until="2019/03/03"
</syntaxhighlight>
===== 件数 =====
<syntaxhighlight lang="bash">
$ git log -1
</syntaxhighlight>
===== ファイル =====
<syntaxhighlight lang="bash">
$ git log ./dokokanoDIR/nanikanoFILE
</syntaxhighlight>
===== 人 =====
<syntaxhighlight lang="bash">
$ git log --author='dareka'
</syntaxhighlight>
===== コミットログ =====
<syntaxhighlight lang="bash">
$ git log --grep='コメント'
</syntaxhighlight>
===== マージコミット =====
<syntaxhighlight lang="bash">
$ git log --merges
</syntaxhighlight>
===== マージコミットを除く =====
<syntaxhighlight lang="bash">
$ git log --no-merges
</syntaxhighlight>
==== format ====
<syntaxhighlight lang="bash">
$ git log --pretty=format:"[%ad] %h %an : %s"
</syntaxhighlight>
{| class="wikitable"
!|パラメーター
!|説明
|-
|%H
|コミットのハッシュ
|-
|%h
|コミットのハッシュ (短縮版)
|-
|%T
|ツリーのハッシュ
|-
|%t
|ツリーのハッシュ (短縮版)
|-
|%P
|親のハッシュ
|-
|%p
|親のハッシュ (短縮版)
|-
|%an
|Author の名前
|-
|%ae
|Author のメールアドレス
|-
|%ad
|Author の日付 (-date= オプションに従った形式)
|-
|%ar
|Author の相対日付
|-
|%cn
|Committer の名前
|-
|%ce
|Committer のメールアドレス
|-
|%cd
|Committer の日付
|-
|%cr
|Committer の相対日付
|-
|%s
|件名
|}
<syntaxhighlight lang="bash">
$ git log --oneline --decorate=full --graph --branches --tags --remotes --date=iso --format='%C(yellow)%H%C(reset) %C(magenta)[%ad]%C(reset) %C(cyan)@%an%C(reset) %C(auto)%d%C(reset) %s'
</syntaxhighlight>
=== config関連 ===
==== Author ====
<syntaxhighlight lang="bash">
$ git config --global user.name <NAME>
$ git config --global user.email <EMAIL>
</syntaxhighlight>
or
<syntaxhighlight lang="bash">
$ git config user.name <NAME>
$ git config user.email <EMAIL>
</syntaxhighlight>
==== permission変更を無視 ====
<syntaxhighlight lang="bash">
$ git config core.filemode false
</syntaxhighlight>
==== コメント入力をvimに変更 ====
<syntaxhighlight lang="bash">
$ git config --global core.editor vim
</syntaxhighlight>
or
<syntaxhighlight lang="bash">
$ git config core.editor vim
</syntaxhighlight>
[[Category:Git]]
[[Category:よくやること]]

2025年10月25日 (土) 19:21時点における最新版

リポジトリの作成

$ mkdir <DIR>
$ cd <DIR>
$ git init --bare --shared

clone

とりあえずclone

$ git clone <REPOSITORY_PATH>

ディレクトリ指定

$ git clone <REPOSITORY_PATH> <DIR>

remote

一覧

$ git remote

追加

$ git remote add <REMOTE_NAME> <REPOSITORY>

削除

$ git remote rm <REMOTE_NAME>

名前変更

$ git remote rename <OLOD_NAME> <NEW_NAME>

fetch

とりあえずfetch

$ git fetch

remoteで削除されたブランチをローカルでも削除

$ git fetch --prune

pull

とりあえずpull

$ git pull

remoteで削除されたブランチをローカルでも削除

$ git pull --prune

Git2.27.0以降でのwarning

hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint:
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fetch + merge(デフォルト)
$ git config pull.rebase false

or

$ git config --global pull.rebase false
fetch + rebase
$ git config pull.rebase true

or

$ git config --global pull.rebase true
fast-forwardのみ
$ git config pull.ff only

or

$ git config --global pull.ff only

変更の確認

$ git status

add

とりあえず全部

$ git add -A

新規作成 or 変更されたもの

$ git add .

変更があったもののみ

$ git add -u

commit

とりあえずcommit

$ git commit

メッセージ付きでcommit

$ git commit -m "<MESSAGE>"

無言でcommit

$ git commit --allow-empty-message -m ""

空のcommit

$ git commit --allow-empty

commitの修正

$ git commit --amend

dry run

$ git commit --dry-run

push

とりあえずpush

$ git push

別名でpush

$ git push origin <LOCAL_BRANCH>:<REMOTE_BRANCH>

dry run

$ git push --dry-run

or

$ git push -n

merge

$ git merge <BRANCH_NAME>

branch確認

$ git branch

or

$ git branch -a

checkout

特定のコミットをcheckout

$ git checkout <COMMIT_ID>

ブランチ切り替え

$ git checkout <BRANCH_NAME>

新しくブランチをremoteから持ってくる

$ git checkout -b <BRANCH_NAME> <REMOTE>/<BRANCH_NAME>

e.g

$ git checkout -b develop origin/develop

checkoutできないとき

エラー内容

fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote-name' which can not be resolved as commit?

こうする

$ git remote show <REMOTE>
$ git remote update
$ git fetch
$ git checkout -b <BRANCH_NAME> <REMOTE>/<BRANCH_NAME>

stash

とりあえず面倒なとき

$ git stash

メッセージ付きでstash

$ git stash save "<MESSAGE>"

一覧

$ git stash list

log

1行表示

$ git log --oneline

グラフィカル表示

$ git log --graph

追加/削除行数

$ git log --numstat

変更したファイル

$ git log --name-status

絞り込み

期間(相対値)
$ git log --since="10 days ago" --until="2 days ago"
期間(絶対値)
$ git log --since="2019/03/01" --until="2019/03/03"
件数
$ git log -1
ファイル
$ git log ./dokokanoDIR/nanikanoFILE
$ git log --author='dareka'
コミットログ
$ git log --grep='コメント'
マージコミット
$ git log --merges
マージコミットを除く
$ git log --no-merges

format

$ git log --pretty=format:"[%ad] %h %an : %s"
パラメーター 説明
%H コミットのハッシュ
%h コミットのハッシュ (短縮版)
%T ツリーのハッシュ
%t ツリーのハッシュ (短縮版)
%P 親のハッシュ
%p 親のハッシュ (短縮版)
%an Author の名前
%ae Author のメールアドレス
%ad Author の日付 (-date= オプションに従った形式)
%ar Author の相対日付
%cn Committer の名前
%ce Committer のメールアドレス
%cd Committer の日付
%cr Committer の相対日付
%s 件名
$ git log --oneline --decorate=full --graph --branches --tags --remotes --date=iso --format='%C(yellow)%H%C(reset) %C(magenta)[%ad]%C(reset) %C(cyan)@%an%C(reset) %C(auto)%d%C(reset) %s'

config関連

Author

$ git config --global user.name <NAME>
$ git config --global user.email <EMAIL>

or

$ git config user.name <NAME>
$ git config user.email <EMAIL>

permission変更を無視

$ git config core.filemode false

コメント入力をvimに変更

$ git config --global core.editor vim

or

$ git config core.editor vim