LoginSignup
11
18

More than 1 year has passed since last update.

忘れがちなGitコマンド

Last updated at Posted at 2017-12-11
種別 操作 コマンド
ローカルリポジトリセットアップ リモートリポジトリコピー git clone
リポジトリ構成
リモートリポジトリ操作 リモート一覧表示 git remote -v
リモート最新履歴取得 git fetch [remote name] [branch name]
リモート変更取り込み git pull [remote name] [branch name]
リモートへ反映 git push [remote name] [branch name]
ブランチ操作 ローカルブランチ一覧表示 git branch
リモートブランチ一覧表示 git branch -r
新規リモートブラント取込み git fetch origin [remote branch name]
git checkout [remote branch name]
ローカルブランチ作成 git branch [new branch name] [base branch name] or [commit id]
ローカルブランチ作成&移動 git checkout -b [new branch name] [base branch name] or [commit id]
ローカルブランチ作成&移動
(リモートブランチベース)
git fetch origin [remote branch name]
git checkout -b [local branch name] [origin/remote branch name]
ローカルブランチ切り替え git checkout [branch name]
リモートへブランチ反映 git push [remote name] [branch name]
ブランチマージ
(マージ側ブランチログ付き)
git checkout [base branch name]
git merge [merge branch name] or [commit id]
ブランチマージ
(マージ履歴&マージ側ブランチログ付き)
git checkout [base branch name]
git merge --no-ff [merge branch name] or [commit id]
ブランチマージ
(ログなし&未commit)
git checkout [base branch name]
git merge --squash [merge branch name] or [commit id]
任意のコミット取り込み git cherry-pick [commit id]
任意のコミット取り込み
(コメント修正)
git cherry-pick -e [commit id]
任意のコミット取り込み
(commitスキップ)
git cherry-pick -n [commit id]
任意のコミット取り込み
(範囲指定)
git cherry-pick [commit id]^..[commit id]
ファイル操作 インデックスへ反映 git add [file name1] [file name2]
ローカルリポジトリへ反映 git commit -m "commit message"
リモートリポジトリへ反映 git push [remote name] [branch name]
リモートの状態に戻す git reset --hard [remote name]/[branch name]
状態確認 ステータス確認 git status
差分確認 ローカルリポジトリとの差分表示 git diff HEAD [file name(option)]
コミットIDで差分表示 git diff [commit id] [commit id] [file name(option)]
コミット履歴操作 コミット状況確認 git log --oneline --graph --numstat --name-only --decorate --num-status
タグ操作 タグ作成 git tag [tag name]
リモートへの反映 git push --tags

■ Git設定

設定単位

管理オプション 管理単位 格納ディレクトリ
--system システム /etc/gitconfig
--global ユーザ ~/.gitconfig
不要 リポジトリ .git/config

config -l : 設定確認

全体
$ git config -l
system
$ git config --system -l
global
$ git config --global -l
local
$ git config --local -l

config [オプション] : 個別設定確認

オプション 内容
user.name ユーザ名
user.email メールアドレス
color.ui 出力色づけ
ユーザ名確認
$ git config --global user.name
メールアドレス確認
$ git config --global user.email

config [オプション] : 設定登録

ユーザ名登録
$ git config --global user.name [ユーザ名]
メールアドレス登録
$ git config --global user.email [メールアドレス]
出力色づけ登録
$ git config --global color.ui true

config --unset : 設定削除

出力色づけ削除
$ git config --global --unset color.ui

config --edit : 設定編集

$ git config --edit

■ リモートリポジトリ セットアップ

init --bare : 新規作成

  • サーバー側で実行
$ mkdir /home/username/test.git
$ cd /home/username/test.git
$ git init --bare --shared

■ ローカルリポジトリ セットアップ

init : 新規作成

$ mkdir test
$ cd test
$ git init

rm -rf : 削除

ディレクトリごと削除
$ cd ..
$ rm -rf test
git管理から外す
$ cd test
$ rm -rf .git

remote add : リモートリポジトリ追加(origin指定)

sshデフォルトポート使用
$ git remote add origin username@ipaddress:/home/username/test.git
sshポート変更
$ git remote add origin ssh://username@ipaddress:portno/home/username/test.git

remote add : リモートリポジトリ追加(shortname指定)

sshデフォルトポート使用
$ git remote add shortname username@ipaddress:/home/username/test.git

git config --edit : リモートリポジトリ追加・削除

  • editerで[remote "remoteName"]以下を削除
$ git config --edit

git clone : リモートリポジトリコピー

sshデフォルトポート使用
$ git clone username@ipaddress:/home/username/test.git
sshポート変更
$ git clone ssh://username@ipaddress:portno/home/username/test.git
ディレクトリ名指定
$ git clone username@ipaddress:/home/username/test.git Hello-Wolrd
特定ブランチorTag名指定
$ git clone -b [branch名 or Tag名] --single-branch username@ipaddress:/home/username/test.git

■ リポジトリ構成

管理場所 操作
ワークツリー
add
インデックス
commit
ローカルリポジトリ
push
リモートリポジトリ

■ リモートリポジトリ操作

remote -v : 一覧表示

$ git remote -v

remote rename : 名前変更

$ git remote rename [old name] [new name]

remote show : 詳細情報取得

$ git remote show [remote name]

remote set-url : URL変更

$ git remote set-url [remote name] [url]

fetch : リモートリポジトリ最新履歴取得

$ git fetch [remote name] [branch name]
  • remote name を省略すると追跡しているリモートリポジトリが対象
  • branch name を省略すると全ブランチが対象

pull : リモートリポジトリ変更取り込み

$ git pull [remote name] [branch name]   # fetch + merge = pull
  • remote name を省略すると追跡しているリモートリポジトリが対象
  • branch name を省略すると現在のブランチが対象

push : リモートリポジトリブランチ反映

$ git push [remote name] [branch name]
追跡ブランチに設定
$ git push -u [remote name] [branch name]
  • remote name を省略すると追跡しているリモートリポジトリが対象
  • branch name を省略するとリモートリポジトリとローカルリポジトリの双方に存在するブランチが対象

push --delete : リモートリポジトリブランチ削除

$ git push --delete [remote name] [branch name]

■ ブランチ操作

branch : ブランチ一覧表示

ローカルブランチ表示
$ git branch
リモートブランチ表示
$ git branch -r
ローカル/リモートブランチ表示
$ git branrh -a
作業中ブランチのマージ済みブランチ一覧
$ git branch --merged
作業中ブランチの未マージブランチ一覧
$ git branch --no-merged

fetch/branch : 新規リモートブランチ取り込み

$ git fetch origin [remote branch name]
$ git checkout [remote branch name]

branch : ローカルブランチ作成

$ git checkout [base branch name] or [commit id]
$ git branch [new branch name]
$ git branch [new branch name] [base branch name] or [commit id]

checkout -b : ローカルブランチ作成&移動

$ git checkout [base branch name] or [commit id]
$ git checkout -b [new branch name]
$ git checkout -b [new branch name] [base branch name] or [commit id]

fetch/checkout : ローカルブランチ作成&移動(リモートブランチベース)

$ git fetch origin [remote branch name]
$ git checkout -b [local branch name] [origin/remote branch name]
  • リモートブランチをfetch後、リモートブランチをベースに、新しいローカルブランチを作成

checkout : ローカルブランチ切り替え

$ git checkout [branch name]

branch -d : ローカルブランチ削除

$ git branch -d [branch name]

branch -D : ローカルブランチ強制削除

$ git branch -D [branch name]

branch -m : ローカルブランチ名変更

$ git branch -m [old branch name] [new branch name]
$ git push origin
  • 旧ブランチはリモートに残ったままとなる

show-branch : ブランチの派生元表示

$ git show-branch

push : リモートへブランチ反映

$ git push [remote name] [branch name]

push : リモートブランチ削除

$ git push [remote name] :[branch name]

merge : ブランチ統合

merge履歴が残らず統合側ブランチのログが全て残る
$ git checkout [Base branch name]  # ベースのブランチへ移動
$ git merge [取り込み branch name]  # 取り込む側のブランチを指定。ローカルリポジトリまで反映。
$ git merge [commit id]
or
$ git merge --ff [取り込み branch name]
$ git merge --ff [commit id]
merge履歴が残り統合側ブランチのログが全て残る
$ git checkout [Base branch name]  # ベースのブランチへ移動
$ git merge --no-ff [取り込み branch name]  # 取り込む側のブランチを指定。ローカルリポジトリまで反映。
$ git merge --no-ff [commit id]
merge履歴、統合側ブランチのログが残らず、commit未実施の状態になる
$ git checkout [Base branch name]  # ベースのブランチへ移動
$ git merge --squash [取り込み branch name]  # 取り込む側のブランチを指定。ローカルリポジトリまで反映。
$ git merge --squash [commit id]

【コンフリクト発生部分】
<<<<<<<
=======
>>>>>>>

mergetool : マージツール

$ git mergetool

rebase : ブランチ統合

$ git rebase [branch name]

cherry-pick : 任意のコミットの取り込み

$ git cherry-pick [commit id]
コミットメッセージ編集
$ git cherry-pick -e [commit id]
コミットスキップ
$ git cherry-pick -n [commit id]
範囲指定
$ git cherry-pick [commit id]^..[commit id]

■ ファイル操作

add : インデックスへ反映

$ git add [file name1] [file name2]
全ファイル指定
$ git add .

commit : ローカルリポジトリへ反映

$ git commit -m "[commit message]"
$ git commit -m "[commit message]" [finename]

push : リモートリポジトリへ反映

$ git push [remote name] [branch name]

rm -f : 強制削除

$ git rm -f *

clean -n : ワーキングツリーのgit管理対象外のファイル確認

add済みのファイルは非表示
$ git clean -n

clean -f : ワーキングツリーのgit管理対象外のファイル削除

$ git clean -f

clean -d : ワーキングツリーのgit管理対象外のディレクトリ削除

$ git clean -d

reset --hard : ワーキングツリー・インデックスをローカルリポジトリの状態に戻す

$ git reset --hard HEAD
$ git checkout HEAD -- filename

reset --hard : ワーキングツリー・インデックス・ローカルリポジトリをリモートの状態に戻す

$ git reset --hard [remote name]/[branch name]

reset --mixed : インデックス・ローカルリポジトリをリモートの状態に戻す

$ git reset --mixed [remote name]/[banch name]

reset --soft : ローカルリポジトリをリモートの状態に戻す

$ git reset --soft [remote name]/[branch name]

revert : 過去のコミットを訂正する

$ git revert [commit id]
$ git push origin
  • Revertのコミットログが追加される

■ サブモジュール操作

submodule add: サブモジュール追加

$ git submodule add https://github.com/xxx/yyy.git

submodule add: サブモジュール一覧表示

$ git submodule

submodule init: サブモジュール初期化

$ git submodule init

submodule update: サブモジュール更新

$ git submodule update

■ 状態確認

status : Gitステータス確認

$ git status

■ 差分確認

diff : ワークツリーとインデックスの差分表示(add前に確認)

$ git diff [filename(option)]

【オプション】
--stat   :変更量表示
--name-only :ファイル名表示
-UN     :変更箇所の前後N行を表示

diff --cached : インデックスとローカルリポジトリ(HEAD)の差分表示(commit前に確認)

$ git diff --cached [filename(option)]

diff HEAD : ワークツリーとローカルリポジトリ(HEAD)の差分表示

$ git diff HEAD [filename(option)]  # HEADは作業中のブランチの先頭のこと

diff HEAD : ローカルリポジトリ(HEAD)とリモートリポジトリの差分表示

$ git diff HEAD [remote name]/[branch name]   # ex) git diff HEAD origin/maseter

diff : コミットIDで差分表示

$ git diff [commit id] [commit id] [filename(option)]

■ コミット履歴操作

log : コミット状況確認

$ git log

【オプション】
--oneline  :1行表示
--graph   :グラフ表示
--numstat  :追加/削除行数表示
--name-only :ファイル名表示
--decorate  :ブランチ名表示
--num-status :ファイルと修正属性表示

reflog : コミット状況簡易確認

$ git reflog

rebase : コミット修正

$ git rebase -i HEAD~2   # 直近2つのコミットを修正
$ git rebase --abort     # rebaseを破棄
$ git rebase --continue  # rebaseを継続

【オプション】
pick  :コミットをそのまま使用
reword :メッセージを修正してコミットを使用
edit  :rebaseの停止
squash :直前のコミットに統合
fixup :メッセージを残さず直前のコミットに統合
drop  :コミットを破棄

  • "git push -f origin"で強制push

blame : 行単位でcommit id表示

$ git blame [file name]

■ コメント操作

amend : 最新のコメント修正

git commit --amend -m "正しいコメント"

■ タグ操作

tag : タグ作成

$ git tag [tag name]
コメント付き
$ git tag -am "[comment message]" [tag name]

tag -d : タグ削除

$ git tag -d [tag name]

tag : タグ一覧表示

$ git tag
コメント付き
$ git tag -n

push --tags : リモートへ反映

$ git push --tags
$ git tag push [tag name]

tag -d : リモートからタグ削除

$ git tag -d [tag name]

tag show : タグ情報の確認

$ git tag show [tag name]

■ ブランチ運用ルール

ブランチ名 役割
develop 作業用ブランチ。
feature 修正・機能追加用ブランチ。developから分岐しdevelopへ統合
release 公開準備用ブランチ。developから分岐しmasterへマージ
master 公開管理用ブランチ
hotfix 緊急修正ブランチ。masterから分岐し修正後master・developへマージ

■ 一時作業の退避/復元

stash : 現在の作業を一時退避

$ git stash

stash list : 一覧表示

簡易表示
$ git stash list
詳細表示
$ git stash list -p
個別表示
$ git stash show stash@{0}

stash pop : 作業の復元

最新作業の復元
$ git stash pop
指定作業の復元
$ git stash apply stash@{1}

stash drop : 作業の削除

最新作業の削除
$ git stash drop
指定作業の削除
$ git stash drop stash@{1}

stash clear : 全作業の削除

$ git stash clear

■ ファイルをGit管理から外す

.gitignoreに追加

任意のファイルを外す
test.md
任意の拡張子のファイルを外す
*.md
任意の拡張子のファイルを外す
*.md
任意のファイルを外さない
*.md
!test.md
特定のディレクトリ以下を外す
test/
コメント
# コメント
11
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
18