hotch-potch, Note to self

いろいろ作業記録

git, Note to self

(覚え書き用)

1.はじめに

たまにしか使わない、あるいは”やっちまった”ときに手直しするコマンドなどをまとめました。

2.初期化

(1)空のままcommitする

# リポジトリ作成
$ git init

# 最初のコミット
$ git commit --allow-empty -m "first commit"

(2)master→mainに変更

ローカルのブランチ名をmainに変更

$ git branch -m master main

※ローカルを変えてからリモートを変えること

2.過去の修正

(1)commit直後に名前やメールアドレスを修正

.gitconfigが設定されていること。

$ git commit --amend --reset-author

直前のコミットだけ修正されます。

(2)名前やメールアドレスを過去に遡って修正

ステージングしていない修正があると、この方法は出来ません。

#名前
$ git filter-branch -f --env-filter 'export GIT_AUTHOR_NAME="なまえ"'
#メール
$ git filter-branch -f --env-filter 'export GIT_AUTHOR_EMAIL="メール"'

(2)先程のコミットを無かったことに

$ git reset --soft HEAD^

3.設定変更

ユーザ情報

$ git config --local user.name hoge
$ git config --local user.email hoge@fuga.org

ファイルの大文字・小文字の変更を検知

$ git config core.ignorecase false

改行コードの自動変更を無効化

$ git config --global core.autocrlf false

ホームディレクトリをまとめて圧縮

$ cd ~ &&  \
sudo apt install zip && \
cd .. && \
sudo zip -r homepi_$HOSTNAME.zip ./pi/ -x \*/.git/\* \*/.ros/\* \*/\catkin_ws/log/\*  \*/\MagPi/\* \*/\.cache/\* \*/\.local/\* \*/\.Downloads/\* && \
sudo mv homepi_$HOSTNAME.zip pi/ && \
sudo chown pi pi/homepi_$HOSTNAME.zip

4.そのほか

(1)追加の変更を最新のコミットに追加する

$ git commit --amend --no-edit

(2)UNIX, Windows併用するときの改行コード設定

改行コード変換が、デフォルトで有効のため、異なるOSを行き来するごとに不具合を起こしやすい(気がします)

改行コード変換を無効にするため、設定ファイルc:\users\<name>\.gitconfigに、以下のエントリーを追加

[core]
    autocrlf = false

(3)コミット履歴をグラフで表示

$ git log --oneline --graph

(4)SourceTreeの使い方

(5)bash用gitプロンプト

$ cd
$ wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
$ mv git-completion.bash .git-completion
$ nano .bash_aliases

.bash_aliases

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
...
## for Git
. $HOME/.git-completion
...

(6)git-flowを使う

# 初回はインストールが必要
$ sudo apt install git-flow 

# git初期化してgit-flow化
$ git init
$ git commit --allow-empty -m "first commit"
$ git flow init -d
$ git branch -m master main

#作業を開始
$ git flow feature start MYFEATURE

#作業を終了
$ git flow feature finish MYFEATURE 

(7)エラーadding the parent project as a safe.directoryの処置

設定ファイルc:\users\<name>\.gitconfigに、以下のエントリーを追加

[safe]
    directory = *

(8)エラーThe server's host key is not cached in the registryの処置

プッシュ・プルするときに、下記のエラーが出る。

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
  • SourceTreeからbashターミナルを起動
  • SourceTreeに付属のplink.exeと、自分の秘密鍵を使って、ホストを登録しなおす
$ "C:\Users\<user name>\AppData\Local\SourceTree\app-3.4.12\tools\putty\plink.exe" -ssh -C -i "C:\Users\<username>\keys\rsa-key-hogefuga.ppk" git@github.com
The host key is not cached for this server:
  github.com (port 22)
You have no guarantee that the server is the computer
you think it is.
The server's ssh-ed25519 key fingerprint is:
  ssh-ed25519 255 SHA256:*************
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n, Return cancels connection, i for more info) y

Using username "git".
Server refused to allocate pty

参考

  • puttyの設定SshHostKeyレジストリ保存先
    • コンピューター\HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys

(9)git英語