LoginSignup
14
16

More than 5 years have passed since last update.

git メモ

Last updated at Posted at 2012-12-26

履歴関係

ログを見る

ファイル名と変更の量を表示

git log --stat

ファイル名と変更の種別を表示

git log --name-status

グラフ表示

git log --graph --oneline

あるブランチからmasterまでのログを抽出

git log from-branch..master --name-status

マージを除く修正コミット一覧を抽出

git log from..to --no-merges --oneline

変更の差分を見る

git diff --name-status

インデント量の違いは無視する

git diff --ignore-space-change

特定のファイルの過去のコミット時点での内容を確認

git show HEAD^:path/to/file
git show badcafe:path/to/file

特定のファイルを過去のコミット時点に戻す

git checkout HEAD^ path/to/file
git checkout badcafe path/to/file

リモートにブランチに対する操作

リモートブランチの確認

git branch -r

リモートブランチのチェックアウト

git checkout -b hogehoge origin/hogehoge

master以外のブランチをリモートリポジトリにpushする

# ブランチ変更
git checkout hogehoge
# リモートのリポジトリにブランチを push
git push -u origin hogehoge
# リモートのリポジトリに別名(fugafugaという名前)でブランチを push
git push -u origin hogehoge:fugafuga

現在のブランチがトラッキングするリモートのブランチを設定する

$ git branch --set-upstream origin/(ブランチ名)

リモートのブランチ(タグ)を削除

リモートの hoge ブランチ(または hoge タグ)を削除したい

git push --delete origin hoge

git push のデフォルトのリモートブランチを設定する

git branch --set-upstream master remote/master

その他の作業

マージ済みブランチの確認

git branch --merged

リベース(rebase)

コミットの順序を入れ替え

git rebase -i HEAD~2

export

git checkout-index -a -f --prefix=../export_20121220/

個人用の .gitignore を用意する

以下を実行すると、~/.gitignore がどのリポジトリでも参照される個人の .gitignore として使えるようになる。

git config --global core.excludesfile ~/.gitignore

git ready » ファイルを無視する

もうひとつワーキングディレクトリを作る

git-new-workdir . ../foobar

参考:git-new-workdir が便利 - #生存戦略 、それは - subtech

トピックブランチで作業中に発生したmasterブランチの変更をrebaseで取り込む

$ git commit -v -a #トピックブランチの変更をとりあえずコミット(あとで取り消す)
$ git checkout master
$ git pull
$ git checkout topic-branch
$ git rebase master 
$ git reset HEAD^  # 先にとりあえずコミットした修正を取り消す

参考:git rebaseって超便利じゃね? - Seasons.NET

コンフリクトが発生した場合は以下を参考に対応する。

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

bash 連携

bash プロンプトにブランチ名を表示し変更されているかどうかを’*’で識別できるようにする

# for git
#
# http://henrik.nyh.se/2008/12/git-dirty-prompt
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/

function parse_git_dirty {
  [[ ! $(git status 2> /dev/null | tail -n1) =~ ^nothing\ to\ commit ]] && echo "*"
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
export PS1='\h \[\033[1;33m\]\W \[\033[0m\]$(parse_git_branch)$ '

github連携

githuを使う準備をする

ユーザ名の設定

git config --global user.name "Your name"

Email

git config --global user.email "your_email@example.com"

既存のローカルリポジトリを github のリポジトリにpushする

Gitの挙動設定

参考:Gitを使い始めたらやっておきたい便利な設定いろいろ : アシアルブログ

カラーリング

git config --global color.ui auto

エイリアス設定

git config  --global alias.co checkout
git config --global alias.st status
git config --global alias.br branch

ユーザ名とemailアドレス

git config --global user.name "hoge"
git config --global user.email "hoge@example.com"

グローバルなignore設定

設定を有効化
$ git config --global core.excludesfile ~/.gitignore_global

~/.gitignore_global に設定する

~/.gitignore_global

参考: グローバルで.gitignoreを適応する - Qiita

push時の挙動の変更

思わぬ事故を防ぐため、現在作業中のブランチのみをpush対象とする

git config --global push.default simple

pull 時の挙動の変更

pull --rebase をデフォルトの動作とする。

$ git config --global pull.rebase true

タブ制御文字のインデント幅を設定

git config --global core.pager 'less -x4'

参考: Setting the Tab Width for the git-diff command | The Coding Journal ツ

コミット時に使用するエディタを vim に

git config --global core.editor 'vim'

マージするときに fast-forward しない

git config --global --add merge.ff false
git config --global --add pull.ff only

参考: gitのmerge --no-ff のススメ - Qiita

参考

14
16
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
14
16