Gitでよくやること

提供: オレッジベース
移動先: 案内検索

目次

リポジトリの作成

$ 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