Skip to content

Instantly share code, notes, and snippets.

@IMOKURI
Last active August 29, 2015 14:19
Show Gist options
  • Save IMOKURI/ca9c31c50f5066257803 to your computer and use it in GitHub Desktop.
Save IMOKURI/ca9c31c50f5066257803 to your computer and use it in GitHub Desktop.
Gitの便利なコマンド

gitの初期設定。

  • 必須
    git config --global user.name "Username"
    git config --global user.email "yourname@subdomain.tld"
  • 任意: gitの出力をカラー表示にする。
    git config --global color.ui auto
  • 任意: git push 実行時にupstreamが設定されていなくても、現在のローカルのブランチ名と同じブランチ名でリモートにpushする設定。
    git config --global push.default current
  • 任意: mergeの時にデフォルトをno-ffとする。
    git config --global merge.ff false
  • 任意: pull時のmergeをrebaseにする。
    git config --global branch.autosetuprebase always
  • 設定確認
    git config --list

ファイル名・ディレクトリ名を変更する。

git mv <oldfilename> <newfilename>

commitの履歴を確認する。

git log --oneline --decorate --graph --branches --tags --remotes

直前のcommitを取り消す。

  • ワーキングディレクトリの変更内容は残す。
    git reset --soft
    git reset --soft HEAD^
  • ワーキングディレクトリの変更内容も消す。
    git reset --hard

修正内容を打ち消すcommitを作る。

  • 通常のcommitの場合
    git revert <commitのSHA1>
  • merge commitの場合
    git revert -m 1 <merge commitのSHA1>

branch名を変更する。

git branch -m <oldbranch> <newbranch>

ローカルのbranchのリストをリモートと同期する。

git remote update origin --prune

リモートのbranchをローカルに取得してcheckoutする。

git checkout -b <new-branch> origin/<new-branch>

リモートのbranchを削除する。

  • はじめにローカルのbranchを削除する。
    git branch -d <branch-name>
  • リモートのbranchを削除する。
    git push origin :<branch-name>

branch間のdiff。

git diff <branch1> <branch2>

  • 差分ファイルのみの出力
    git diff --name-only <branch1> <branch2>

commit logから任意のcommitをマージする。

git cherry-pick <commit-id>

現在の修正を一旦退避して、リモートを取り込みたい。

  • 変更履歴を保存する。
    git stash save "comments"
  • stashしたログを出力。
    git stash list
  • 変更履歴を戻す。
    git stash pop stash@{0}
  • 変更履歴の一部削除。
    git stash drop stash@{0}
  • 変更履歴をすべて削除。
    git stash clear

tag付されたブランチの内容を確認する。

  • リモートのtagの情報を取得する。
    git remote update
  • tagの一覧を確認。
    git tag
  • tagの詳細を確認。
    git show <tag名>
  • tagのブランチを作成してチェックアウトする。
    git checkout -b <tag名> refs/tags/<tag名>

conflictした時に、一方を優先させる。

  • 現在のブランチを優先する。
    git checkout --ours <filename>
  • マージしたブランチの方を優先させる。
    git checkout --theirs <filename>
  • 最後にコミット。
    git commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment