githubを試してみる



ブログのソースをどこかにアップしたく、githubを利用する方法を調べていました。
 

Windowsでgitを利用する

 Cygwinから利用をしてみます。

 
 パッケージの選択
 Search欄に「git」を入力し、必要なパッケージをインストールする

 
インストールしたものは、下記になります。

  1. git
  2. git-completion
  3. git-svn
  4. gitk

 

Windowsでとりあえず、gitを試してみる

Cygwin版Gitの環境構築を参考にいろいろコマンドを実行してみました。

#Subversionリポジトリの作成
mkdir ~/svn_repos
cd ~/svn_repos
svnadmin.exe create .

#作業用ディレクトリの作成
mkdir ~/work
mkdir ~/work/svnwork
mkdir ~/work/gitwork

#Subversionへコミット
cd ~/work/svnwork

svn checkout file:///cygdrive/d/usr/home/svn_repos/
Checked out revision 0.

cd svn_repos
echo aaaa > file1.txt
svn add file1.txt
svn commit -m"initial"

#Gitへsvn cloneする
cd ~/work/gitwork

git svn clone file:///cygdrive/d/usr/home/svn_repos/

#Gitからsvn dcommitする
cd svn_repos/

ls

echo eeee>>file1.txt

git status

git commit -a

git log

git svn dcommit

#Subversion側で確認

cd ~/work/svnwork/svn_repos/
svn update
svn log
cat file1.txt

 

githubを利用する

なにはともあれ、githubにユーザー登録します。
 

ユーザー登録を行う

 「Create your free personal account」から新規ユーザー登録を実施します。
 

SSH Keysを登録する

 help.githubの「Set Up SSH Keys」ページを参考に、SSH Keysを設定します。
 SSHようのKey設定に関しては、「さくらVPSからgithubに接続してみる」時にキーを作成するのでそこで一緒に登録をおこないます。
 

リポジトリーを作成する

 help.githubの「Create A Repo」ページを参考に、リポジトリーを作成します。

リポジトリーを登録すると下記のような形で、コマンドのチュートリアルが表示されます。

---
全体設定
 ダウンロードおよびインストール Git
  git config --global user.name "happytann"
  git config --global user.email happytann@gmail.com
        
次の手順:
  mkdir blog_source
  cd blog_source
  git init
  touch README
  git add README
  git commit -m 'first commit'
  git remote add origin git@github.com:happytan/blog_source.git
  git push -u origin master
      
すでにGitリポジトリがありますか?
  cd existing_git_repo
  git remote add origin git@github.com:happytan/blog_source.git
  git push -u origin master
      
Subversionのリポジトリを取り込みたい?
  ここをクリック
      
終わったら
  続ける
---

リポジトリーができました。
 

さくらVPSからgithubに接続してみる

Github用のキーを設定する

ssh-keygen -b 2048 -t rsa -f ~/.ssh/github_rsa

 
~/.ssh/configに以下の内容を記載する

Host github.com
  User git
  Port 22
  Hostname github.com
  IdentityFile ~/.ssh/github_rsa
  TCPKeepAlive yes
  IdentitiesOnly yes

~/.ssh/github_rsa.pubの内容をgithubの管理画面から公開鍵を登録します。
 
「~/test」ディレクトリを作成し、一通りコマンドを確認してみました。

git init
git add test.txt
git commit -m 'first commit'
git remote add origin git@github.com:happytan/blog_source.git
git push -u origin master

 
pushすると下記のようなメッセージが表示されるようです。

Enter passphrase for key '/home/username/.ssh/github_rsa':
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:happytan/blog_source.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

 
公開されている自分のリポジトリーを確認するとtest.txtが表示されました。

リポジトリー


WindowsCygwinからgithubにアクセスしてみる

WindowsCygwinからgithubにアクセスしてみます。CygwinについてくるTerminalだと
日本語表示が不便そうなので「MinTTY」というものをいれてみました。
 

CygwinにMinTTYをインストールする

code.googleからダウンロードできるみたいです。mintty
mintty-0.9.6-cygwin17.zipを解凍し、「mintty.exe」「create_shortcut.js」を「cygwin\bin」に配置します。
その後、create_shortcut.jsを実行し、「mintty」というショートカットを作成します。
 

minttyからgithubにアクセスしてみる

「~/work/gitwork」でgithubの環境をコピーします。
 

git clone git@github.com:happytan/blog_source.git

リポジトリーのコピーができました。「~/work/gitwork/blog_source」
 

$ cd blog_source/
$ ls
test.txt
$ vi hoge.txt
$ git add hoge.txt
$ git commit -m 'second commit'
$ git remote add origin git@github.com:happytan/blog_source.git
$ git push -u origin master

上記のコマンドを実行すると、新たにhoge.txtがpushされたようです。

Enter passphrase for key '/home/username/.ssh/github_rsa':
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:happytan/blog_source.git
   f90123a..43c0822  master -> master
Branch master set up to track remote branch master from origin.

 
公開されている自分のリポジトリーを確認するとhoge.txtが表示されました。

リポジトリー