LoginSignup
22
22

More than 5 years have passed since last update.

gitコマンドメモ

Last updated at Posted at 2016-02-11

gitリポジトリを作成する

git init

gitリポジトリを作成する(ワーキングディレクトリを含まない)

git init --bare

指定したリポジトリを複製する

git clone <repository> <directory>

指定したリポジトリを複製する(.gitを置くディレクトリを変更する)

git clone --separate-git-dir=<directory> <repository> <directory>

指定したブランチでリポジトリを複製する

git clone --single-branch -b <branchname> <repository> <directory>

リポジトリの指定した履歴のみを複製する

git clone --depth <depth> <repository> <directory>

ファイルモードの違いを無視する

git config core.filemode false

ファイルをステージングに追加する

git add <filename>

ステージングからファイルを削除する

git reset -- <filename>
git rm —-cached <filename>

ワーキングディレクトリの状態を指定したコミットの状態にした上で、ブランチのHEADを指定したcommitに変更する。差分はステージングに追加した状態にする

git reset -—soft <commit>

ワーキングディレクトリの状態を指定したコミットの状態にした上で、ブランチのHEADを指定したcommitに変更する

git reset -—hard <commit>

指定したファイルをリポジトリに含める。事前にファイルをステージングに追加済みであること

git commit <filename>

空のコミットをする

git commit --allow-empty -m <msg>

リモートリポジトリに指定したブランチを反映する

git push <repository> <refspec>

リモートリポジトリに指定したブランチを強制的に反映する

git push -f <repository> <refspec>

リモートリポジトリに指定したローカルブランチを指定したリモートブランチに反映する

git push <repository> <local_refspec>:<remote_refspec>

指定したリモートブランチを削除する

git push <repository> :<remote_refspec>

コミットメッセージから検索する

git log --grep=<pattern>

コミットの差分から検索する

git log -S <string>
git log -G <regex>

指定したファイルが変更されたコミットを検索する

git log <filename>

指定したファイルが変更されたコミットを検索する(削除されたファイルも検索対象に含める)

git log -- <filename>

リポジトリ内を検索する

git grep <regex>
git grep -F <string>

指定したtreeオブジェクト内を検索する

git grep <string> <tree>

リポジトリ内をand/or検索する

git grep -e <regex> --and -e <regex>
git grep -e <regex> --or -e <regex>

指定したコミットを現在のブランチに取り込む

git merge <commit>

指定したコミットとの差分をステージングに取り込む

git merge --squash <commit>

マージを実行して、コンフリクトした際に、マージを実行する前に戻す

git reset --merge

指定した2つのコミットの最後の共通コミットのsha1を得る

git merge-base <commit> <commit>

指定したコミットひとつを取り込む

git cherry-pick <commit>

指定したブランチからのコミット履歴を手動で改変する

git rebase -i <branch>

指定したディレクトリ内のみを抽出したリポジトリを作る(元に戻せない)

git filter-branch --subdirectory-filter <directory>

指定したコミットのファイルのみをマージする

git checkout <commit> -m <filename>

指定したファイルをコンフリクトした状態に戻す

git checkout --merge <filename>

rebaseを取り消す

git rebase --abort

指定したファイルをコミット対象から除外する

git update-index --assume-unchanged <filename>
git update-index --no-assume-unchanged <filename>

指定したファイルをコミット対象から除外し、かつ、 reset, merge対象からも除外する

git update-index --skip-worktree <filename>
git update-index --no-skip-worktree <filename>

ファイル変更の差分を参照する

git diff
git diff <filename>

ステージングに追加されたファイルの差分を参照する

git diff --cached

ファイル変更の差分を単語単位で参照する

git diff --word-diff=color

指定したファイルを行単位で変更履歴を参照する

git blame <filename>

指定したブランチを削除する

git branch -D <branchname>

ローカルのリモートブランチを削除する

git branch -D -r <remote_branchname>

ステージングを現在のコミットに上書きする

git commit --amend -C HEAD

指定したオブジェクトの種類を参照する

git cat-file -t <object>

指定したオブジェクトの中身を参照する

git cat-file -p <object>

現在のリポジトリのTOPディレクトリのディレクトリパスを得る

git rev-parse --show-toplevel

リポジトリで管理されていないファイルとディレクトリを削除する

git clean -f -d

リポジトリで管理されていないファイルとディレクトリを削除する(無視対象も含む)

git clean -f -d -x

無視対象にしたファイルのみを削除する

git clean -f -X

削除対象のファイル名を取得する

git clean -n

変更したファイルを一時退避する

git stash

変更したファイルを退避リストに含め、退避を実行しない

git stash save

退避したファイルを元に戻す

git stash apply <stash>

退避したファイルを元に戻し、退避リストから削除する

git stash pop <stash>

退避リストから削除する

git stash drop <stash>

指定したリポジトリをZIP圧縮する

git archive --format=zip <commit> -o <filename>

リモートリポジトリのURL一覧を取得する

git remote -v

リモートリポジトリのURLを取得する

git remote get-url <name>

リモートリポジトリのエイリアスを登録する

git remote add <name> <url>

リモートリポジトリの参照先を変更する

git remote set-url <name> <url>

リモートリポジトリの参照を解除する

git remote remove <name>

リポジトリ管理されているファイル一覧を取得する

git ls-tree -r --name-only <refspec> <path>
git ls-files <path>

リポジトリ管理されてないファイル一覧を取得する

git ls-files --other --ignored --exclude-standard
22
22
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
22
22