Windows7 + PortableGit + GitHub の環境を構築する

Windows環境でGitHubを使いたいと調べたときのメモ。
端末固有の環境を構築したくなかったため、ローカル環境はPortable版を利用しました。

 
目次

 

構築する環境


 

流れ

7-Zip Portableをダウンロード・解凍

Git Portableを解凍するために必要なため、PortableAppsよりダウンロード・解凍します。
PortableApps - 7-Zip Portable

 

msysgit Portable のダウンロード・解凍

最新のGit Portableをダウンロード・解凍します。
ファイル名は、PortableGit-x.x.x-previewyyyymmdd.7z
msysgit - Download

 

日本語対応の準備

less

現行バージョンでは日本語対応は問題なさそうだったので、別途設定は行いませんでした。

 

nkf

調べたところ、nkfは以下のところで発見しました。

  1. sourceforge.jp - nkf Network Kanji Filter
  2. Vector - nkf.exe nkf32.dll Windows用
  3. nkf Network Kanji Filter for Win32 (自家用)

 
sourceforge.jp版は、Windowsでの動作確認の記載がなかったため、更新日付の新しいVector版を利用します。

vc2005\win32(98,Me,NT,2000,XP,Vista,7)Windows-31J\nkf32.exe を、「nkf.exe」にリネームし、\bin配下へとコピーします。

 

inputrcの変更

\etc\inputrc をサクラエディタ開き、14行目あたりを以下のように修正します。

# disable/enable 8bit input
#set meta-flag on
#set input-meta on
#set output-meta off
#set convert-meta on

set meta-flag on
set output-meta on
set convert-meta off
set kanji-code utf-8


 

profileの変更

\etc\profile をサクラエディタで開き、一番最後に以下を追記します。

export GIT_PAGER="nkf -s | LESSCHARSET=utf-8 less"
export GIT_EDITOR="'<サクラエディタのパス>' -CODE=4"


 

$HOMEの変更

何もしないと、GitがPortable版なのに、ユーザー固有の情報は %USERPROFILE% に保存されてしまいます。

そのため、markashleybell.com - Portable Git for Windows: setting the $HOME environment variable to allow complete portability (including SSL keys and configuration for use with GitHub)を参考に、別の場所へと保存できるように設定します。

 
\etc\profile をサクラエディタで開き、70行目あたりの「normalize HOME to unix path」の前に、「HOME="/path/to/your/home"」を追加します。

HOME="/path/to/your/home"

# normalize HOME to unix path
HOME="$(cd "$HOME" ; pwd)"


 

Gitの設定

自分用に以下の設定をします。

更新は、上記で設定した $HOME\.gitconfig に保存されます。$HOMEを設定しない場合は、%USERPROFILE%\.gitconfig にほぞんされます。

$ git config --global user.name "自分のユーザー名"
$ git config --global user.email "自分のメールアドレス"
$ git config --global color.ui auto
$ git config --global core.autocrlf false


 

GitHub用の設定

上記のmarkashleybell.comを参考に、設定します。

SSHキーの生成

GitHub - Set Up Git の「Set Up SSH Keys」の作業を行います。

PortableGitの場合、「git-bash.bat」を起動し、1・3の手順にて、$HOME\.sshSSHキーを作成します。

 

ssh-agentの設定

passphraseを都度入力するのを省略するため、ssh-agentを設定します。

  1. \etc\profile ファイルをサクラエディタで開く
  2. GitHub - SSH key passphrases : Auto-launching ssh-agent on msysgit に従い、profileファイルの一番下に、記載されているコードを追加する
  3. git-bash.bat を起動し、passphrase を入力する

 
ssh-agentが起動しているかどうかは、git-bashで確認します。

$ <span class="deco" style="font-weight:bold;">ps</span>
      PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND
     1420       1    1420       1420    ?  500 07:19:23 /bin/ssh-agent
     5708       1    5708       5708  con  500 07:22:54 /bin/bash
      532    5708     532       1424  con  500 07:22:55 /bin/ps

 

GitHubへの設定

  1. $HOME\.ssh\id_rsa.pub ファイルを、サクラエディタで開き、キーをコピー
  2. GitHubの公開キーの追加ページへ行き、Key欄へとペースト
  3. git-bash.batにて以下のコマンドを入力し、設定が成功しているかをテスト
$ ssh -T git@github.com

 

GitHubリポジトリを作成

今回は、Sample_FileWriter というリポジトリを作成します。


 

ローカルに Sample_FileWriter というリポジトリを作成

リポジトリ内には、必要なファイルを格納します。

実行したコマンドとその結果は、以下のとおりです。

$ cd /d/github/sample_filewriter

$ git init
Initialized empty Git repository in d:/github/sample_filewriter/.git/

$ git add .

$ git commit -a
[master (root-commit) 23e0c0f] Google拡張機能でのFileSystemAPIサンプル
 3 files changed, 92 insertions(+), 0 deletions(-)
 create mode 100644 background.html
 create mode 100644 contentscript.js
 create mode 100644 manifest.json
warning: Your console font probably doesn't support Unicode. If you experience strange characters in the output, consider switching to a TrueType font such as Lucida Console!

$ git log
commit 
Author: thinkAmi
Date:   

    Google拡張機能でのFileSystemAPIサンプル

    Writerでローカルファイル保存するためのサンプルです。

 
git commit の最後で warningが出ていますが、今回は特に気にしません。

日本語入力・表示も問題なくできていそうです。

 

GitHubにPushする

$ git remote add origin git@github.com:thinkAmi/Sample_FileWriter.git

$ git push -u origin master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 1.85 KiB, done.
Total 5 (delta 0), reused 0 (delta 0)
To git@github.com:thinkAmi/Sample_FileWriter.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

 
GitHub上で確認しても、無事にアップされていました。
READMEファイルがないと注意されていますが、ひとまず今回の作業は完了とします。

 
最後に、markashleybell.com - Portable Git for Windows: setting the $HOME environment variable to allow complete portability (including SSL keys and configuration for use with GitHub) に記載されていた注意事項を引用しておきます。

Cautionary Note: if you’re carrying your install about on a USB drive, be careful with your private SSL key! If someone gets hold of it, they can potentially authenticate as you until you revoke that key's access from within GitHub.

 

その他

git log でログが長くなったときに 「(END)」 のように表示され、先に進めないことがありました。
その場合は、「q」を押すことで元に戻りました。(Unixも勉強しないと・・・)
stackoverflow - Git Windows Command Prompt gets stuck during Git commands with (END)

 

*1:対象は、Chrome拡張機能でFileSystemAPIを使ってみた にて作成したもの