gitの小技 4つ
大掃除中にいろいろ躓いたり調べたりしたので4つほど小技をまとめときました。
この記事中に登場するGitのバージョンは 1.8.5.2 です。他のバージョンではうまく動作しないかもしれません。 あらかじめご了承ください。
$ git --version
git version 1.8.5.2
直前の作業ブランチに戻る
複数ブランチを行ったり来たり反復横跳びする場合は git-checkout
でブランチ名を - と指定します。 cd -
と同じ要領ですね。
# git b = git branch
# git co = git checkout
$ git b
* master
$ git co -b some-work
Switched to a new branch 'some-work'
$ git b
master
* some-work
$ git co -
Switched to branch 'master'
$ git b
* master
some-work
$ git co -
Switched to branch 'some-work'
$ git b
master
* some-work
...
gitconfig で外部ファイルの設定を読み込む
git 1.7.10 以降ならば gitconfig で include が設定できます。
gitconfigをGitHubの公開レポジトリにコミットしてるけどトークンは公開したくない… そこで公開したくない設定を外部ファイルを記述し、外部ファイルは秘密の場所に保存しておく、といった感じで運用できます。
gitconfigの記述は以下のような感じです。
$ less ~/.gitconfig
[include]
path = ~/.gitconfig.local
$ less ~/.gitconfig.local
[github]
user = jiska
token = XXXXXXXX
initial commit のhashを調べる
いい感じの方法がわからなかったので適当に…。
$ git log --format=%h | tail -1
c555ea7
下記のような感じで使います。
$ git log --name-status $(git log --format=%h | tail -1)
commit c555ea7c9fa46b61a2dd8b72015530b8f1d791b1
Author: jiska <jiska.ym+github@gmail.com>
Date: Wed Jan 1 19:59:27 2014 +0900
Initial commit
A home/.bash.d/alias
A home/.bash.d/alias-svn
...
initial commit を改ざんする
危険 かつ バッドノウハウ …。
git-rebase に --root
オプションを付けるとコミット一覧にInitial commitも表示されます。
…あとは 自己責任 で。
# Initial commitのhashは c555ea7
$ git rebase -i c555ea7
# Initial commitが表示されない
pick 866e937 Refuse wrong owner\'s file
pick a7b3f53 Add alias
pick db1183d Add homeshick settings
...
# --root オプションをつける
$ git rebase -i --root
# Initial commitが表示される
pick c555ea7 Initial commit
pick 866e937 Refuse wrong owner\'s file
pick a7b3f53 Add alias
pick db1183d Add homeshick settings
...
まとまらない
他にも調べたら書きます。よりよいgitライフを。