ぷろみん

プログラミング的な内容を扱ってます

github/hubコマンドを使ってみた

概要

githubCUIで操作できるhubコマンドの使い方や困った事をメモしました。

環境

debian

インストール

まず、ビルドにgoが必要なのでインストールします。
私の場合はansibleで入れました。
こんなややこしい方法取らずに適当な方法でインストールした方が楽です。

# パッケージマネージャから入れる方法
sudo apt-get install -y software-properties-common
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

# pipから入れる方法
sudo apt-get install -y python-pip
sudo pip install ansible
$ mkdir -p develope/roles
$ git clone https://github.com/jlund/ansible-go develope/roles/ansible-go
$ vim develope/site.yml
# develope/site.yml
---
- hosts: all
  sudo: yes
  roles:
    - ansible-go
$ vim hosts
# ansible_connectionを付けない場合自身のssh鍵が必要
localhost ansible_connection=local
# 接続確認を無効化したい場合
$ vim ansible.cfg
[defaults]
host_key_checking = false
$ ansible-playbook -i hosts develope/site.yml
# 再読み込みしないとgoへのパスが通らないので
$ exec $SHELL -l

次はhubのビルド

$ git clone https://github.com/github/hub.git
$ cd hub
$ ./script/build
$ sudo cp hub /usr/local/bin
# hubにgitエイリアスをかける
$ echo 'eval "$(hub alias -s)"' >> ~/.bash_profile
$ exec $SHELL -l
$ git version
git version 2.1.4
hub version 2.2.0-96-gaa63aa6

push

$ mkdir foo
$ cd foo
$ git init -g
$ echo '# foo' >> README.md
$ git create
# ここで色々聞かれる

ユーザ名とパスワード、two-factor authenticationを有効にしている場合はSMS等で送られてくるコードを入力します。すると

https://github.com/settings/tokens

に新しくアクセストークンが追加されています。
fooレポジトリも作成されています。
また、~/.config/hubも生成されています。このファイルにより次回からはgit createの際に認証が不要になります。
ファイルの内容は以下です。

github.com:
- user: YOUR_USER
  oauth_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  protocol: https

早速pushしていきましょう。

$ git config --global user.name "YOUR_USER"
$ git config --global user.email "YOUR_EMAIL"
$ git add .
$ git commit -m 'Initial commit'
$ git push origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

アクセストークン作ったのならそれでpushして欲しかったのですが、そこはカバーしてくれない様子です。以下の様にすればアクセストークンでpushできるようになるはずですが、せっかく名前とレポジトリを自動取得できるようにしたのに、手入力したくありません。

git remote set-url origin https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@github.com/YOUR_USER/CURRENT_REPO.git

諦めてssh接続します。
ssh-keygenid_rsa.pubを生成し、内容をgithubに登録します。

その後に再トライです。

$ git push origin master
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

レポジトリの場所が間違っている様子なので、どこにアクセスしているか確認します。

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@github.com:/foo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

url = git@github.com:/foo.gitとユーザ名が抜けてしまっているみたいです。仕方ないので作り直します。

$ rm -rf .git
$ git init -g
$ git add .
$ git commit -m 'Initial commit'
$ git push origin master

今度は上手くいきました。

次回はgit-flow入れてhubを活用してみようと思います。