refs:gitのcui操作

■github
・リポジトリの削除
リポジトリを選択してsettingsを選択。Danger ZoneからDelete this repositoryボタンを選択して「Are you ABSOLUTELY sure?」ダイアログが開くので、リポジトリ名を入力して「I understand consequence」を押す。
refs:GitHubからレポジトリを削除する方法

・リポジトリダウンロード
git cloneコマンドでリポジトリをローカルにダウンロードできる。下がplyaのサンプル、マニュアル等のリポジトリの例。
> git clone git://github.com/playframework/Play20.git
refs
play Japan コードの取得
gitを使いこなすためのコマンド2ページ目
playframework / github

■用語
・HEAD,HEAD^,HEAD^^
HEAD1は、現在使用しているブランチの先頭
refs:head,猿でもわかる

・FETCH_HEAD
fetchしたときの名前のないブランチ。このブランチはFETCH_HEADという名前でチェックアウトできる。

・fast-forwardマージ
refs:fast-forwardマージって何?


■git merge
fast-forwardマージ

■git log

$ git log -h
⇒ヘルプ
$ git log --since="2013-12-01" --author="*hogeuser*"
⇒2012/12/1以降で作者がhogeuserを含む場合
$ git log --graph
⇒グラフで見れる。よくわからん
$ git log -p
⇒変更内容も表示
$ git log --grep 'テスト'
⇒テストというコミットログを含むもの
$ git log --pretty=oneline --abbrev-commit
⇒prettyはログの位置業表示、abbrevはcommit idを短縮表示
$ git log --decorate
⇒ログにタグも表示
$ git log -1
⇒最新のログを一つ表示
$ git log --oneline
ログを一行表示

■git show
コミットの詳細を確認する
git show HEAD
HEADのコミット詳細
git show 423itrwerw3532
コミットIDが423itrwerw3532のコミット詳細

○git showで指定ファイルの指定バージョンの内容を見る

こんな感じ。26721f3がリビジョン番号の先頭7桁、セミコロンつけてその後に対象のファイルパスを指定する。
git show 26721f3:hogedir/hogedir2/hoge.rb

■タグ、git tag

$ git tag v1.0
⇒最新のコミットIDにv1.0というタグをつける
$ git tag
⇒tag一覧を表示

・タグv1.0の内容を知りたい時
>git show v1.0
commit 432432432f8f5fec7e254d0a5240f13df3da
Author: hgoeuser <hgoe@git.com>
Date: Sat Dec 21 19:48:04 2013 +0900

add line9 commit

diff --git a/hoge.txt b/hoge.txt
index 7559e00..1096861 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -7,3 +7,4 @@ line5
lin6
line7
line8
+line9

・過去のコミットIDにタグをつけたい時
$git tag v0.5 3460e517619ff4569a5e6e7
⇒ 3460e517619ff4569・・・というコミットIDにタグ付けしている。

・タグを指定してgit resetもできる
$git reset --hard v0.5
⇒タグv0.5のコミットまでreset

$git tag -d v0.5
⇒タグv0.5を削除

あと、タグのブランチ、リポジトリでのグローバル性だけど、ローカルリポジトリにmaserブランチにタグをつけて、ローカルブランチのdevelopブランチにcheckoutする。ここでgit logを実行しても結果は一緒だった。あと、タグを付けた後に、git push origin mater:materしてみても、Everything up-to-dateだった。同一リポジトリ内では共有されるけど、別リポジトリとは全く無関係って事かな。

■リモートの新規にできたブランチをローカルリポジトリに落とす
元々、ローカルとリモートブランチにそれぞれmasterしかなく、リモートにdevelopブランチができた。そしてそのdevelopブランチをローカルリポジトリにdevelopブランチを作成して落としたい。そんな場面が想定です。

git branch develop origin/develop

もしくは

git checkout -b develop origin/develop

これでおけ。新規にローカルリポジトリdevelopを作ってくれる。間違っても下のようにgit pullを実行してはいけない。

git pull origin develop:develop

これを実行すると、ローカルリポジトリdevelopをリモートのdevelopの内容で作ってくれるのだが、例えばローカルリポジトリがmasterの状態で実行してたら、ローカルのmasterもリモートのdevelopの内容で上書きされてしまう。正直git pull何してる?って感じ・・・とりあえず、ここでやりたい事はgit branchの方。

refs:gitで新しいブランチをローカルに持ってくる場合、git pullしてはいけない理由
git pullの詳細な挙動を追ってみる

■conflict(コンフリクト)発生時
git mergeしてコンフリクトが発生した場合、コンフリクトを解消した後に、git add, git commitすればいいだけ。
refs:マージの衝突を解決してみよう

■git reset, git reflog

commitを戻す。過去のコミットIDに戻る。git reset HEAD ファイル名
>git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html

git reset --hard HEAD
⇒直前のコミットに戻す。HEADは直前のコミットを表す

git reset --hard HEAD^
⇒直前のコミットの一つ前に戻す

git reset --hard rewrew432432
⇒任意のコミットに戻したい場合はコミットIDを指定する

git reset --hard ORIG_HEAD
⇒任意のコミットに戻したけど、やっぱりgit resetする前に戻したい場合。ORIG_HEADはあくまで一つ前のgit resetを無効にする。最新のコミット状態に戻れるわけではない。今までのgit resetをすべて無効にして、最新のコミットIDに戻したい場合、git reflogでそのコミットIDを探し、git resetする


>git reflog
8781bc4 HEAD@{0}: reset: moving to ORIG_HEAD
3bbee82 HEAD@{1}: reset: moving to ORIG_HEAD
781bc4 HEAD@{10}: commit: add line8 commit
d9359b0 HEAD@{11}: commit: add line7 commit

戻したコミットIDが5000811の場合、git reset --hard HEAD@{9}

*git logでコミットが戻ったかどうかを確認できる。

○リモートリポジトリへ

ローカルリポジトリのmasterをgit reset --hardでいくつか前のコミットに戻し、それをリモートリポジトリのmasterに反映させようとpushしようとしたらエラーが出た。

>git push origin master:master
To git@github.com:hogeuser/hogerepo.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:hogeuser/hogerepo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

さきにgit pullしろと書いてあるから、git pullしたら同期はどれたけど、結局ローカルリポジトリのブランチがgit reset前に戻っただけ・・・やろうとしていることができない。

じゃあ、リモートブランチを直接戻そうとして、git reset --hard <commitId> origin/masteとかやってみたけど、できない。で、結局やりたいことは-fオプションをつけたらできた。

>git push -f origin master:master

refs:gitでリモートリポジトリを巻き戻す(追記部分)

forceってことは、正攻法ではないんだ。エラーメッセージにもあったえど、non-fast-forward,fast-forwardsというのが意味わからんキーワード。この二つをあとで調査。

ちなみにこの後やっぱりローカルもリモートのmasterもgit resetする前に戻そうと思って、ローカルをgit reset --hard ORIG_HEADしたあと、git push origin master:masterを実行したら、問題なくできた。今度は-fオプションなしで。

refs
タグ、猿でも解る。タグのチュートリアルもある

■git rebase -i
過去のコミットをまとめる

■リモートリポジトリのブランチからローカルリポジトリのブランチを作る

git branch hogehogehogehoge origin/hogehogehoge
originのhogehogehogeブランチからローカルリポジトリのhogehogehogehogeブランチを作る

*リモートリポジトリの反映を確実にするためにgit fetchをしておく

■変更したファイルのうち一部のファイルだけコミットする。git add, git commit

変更したファイルのうち、一部をコミットしたい。例えば、git statusで変更が表示されたファイルが沢山あるけど、hoge.txt,hoge4.txtの二つだけコミットする。

>git add hoge.txt hoge4.txt

git add でファイル名を指定してステージングに移行。あとは、git commitできる。git pushもできる。

■二要素認証
refs
GitHubで二要素認証(Two-factor Authentication)を有効にする
github 2段階認証を導入してみた

githubで二要素認証メモ。

githubのサイトの設定⇒Acount Settingsページ。Twofactor authonicationのstatusがoffになっている。set up two factor authonicationボタンを押してパスワード入力し画面遷移。smsとappの二つの方法があるけど、スマホを使ったsmsでやってみる。国コードと携帯番号を入力してsend code。そうするとスマホにsmsで6桁のdigit codeが送られてくるから。このコードを6-digit codeに入力する。これで完了。two-factor authonication is currently Onになっている。試しに一度ログアウトして再ログインしてみる。username/password入力した後に、two-factor authonication画面がでる。そして、ここまでくるたびに毎回smsでdigit codegが送信されてくるので、これを入力してログイン完了。

あと、二要素認証のデバイス(今回はスマホ)をなくしたときのために、recovery codesのメモはとっておく。

ついでにFallback SMS numberにも自分のスマホを登録してsmsを受け取った。これ何の意味があるのかはっきりしてない・・・

■ssh,鍵認証

上で書いたgithubの二要素認証をすると、web上のログインは二要素認証が確率されたわけだけど、コマンドラインからリモート(今回はgithub)にアクセスするときに、httpsだとはじかれる。

$ git remote -v
origin https://github.com/hogeuser/hogerepo.git (fetch)
origin https://github.com/hogeuser/hogerepo.git (push)

$ git push origin hogehogehogehoge:hogehogehoge
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/hogeuser/hogerepo.git/'

上はlocalのhogehogehogehogeブランチをoriginのhogehogehogeブランチにpushしようとした例。リモートとやりとりするためには、httpsからsshに変更して、鍵認証にする必要がある。

まずは鍵。

$ cd ~/.ssh/
$ ls
$ ssh-keygen -t rsa -C"hoge@gmail.com"
Enter file in which to save the key (/Users/hogeuser/.ssh/id_rsa): /Users/hogeuser/.ssh/id_rsa_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/hogeuser/.ssh/id_rsa_github.
Your public key has been saved in /Users/hogeuser/.ssh/id_rsa_github.pub.
The key fingerprint is:

.ssh $ ls
id_rsa_github id_rsa_github.pub

id_rsa_githubが秘密鍵、id_rsa_github.pubが公開鍵。公開鍵をgithunに登録する

$pbcopy < id_rsa_github.pub
公開鍵の中身をpbcopyでコピー

githubのサイトへいき、account setting⇒ssh keys⇒Add ssh keyではりつけ。titleは適当な名前。これで鍵の登録も完了。

sshでgithuのアクセス確認してみる。まずは鍵登録前

.ssh $ ssh -T git@github.com
The authenticity of host 'github.com (222.22.252.228)' can't be established.
RSA key fingerprint is
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added (RSA) to the list of known hosts.
Permission denied (publickey).

もしくは

.ssh $ ssh -l git -i ~/.ssh/id_rsa_github github.com
Permission denied (publickey).

両方ともpermission deniedされた。次に鍵登録後

.ssh $ ssh -l git -i ~/.ssh/id_rsa_github github.com
Saving password to keychain failed
Identity added: /Users/hogeuser/.ssh/id_rsa_github (/Users/hogeuser/.ssh/id_rsa_github)
PTY allocation request failed on channel 0
Hi hogeuser! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

.ssh $ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '11' to the list of known hosts.
Hi hogeuser You've successfully authenticated, but GitHub does not provide shell access.

successfully authenicated おっけ。ちなみにこれを実行したら~/.ssh/にknown_hostsファイルが生成された

もう一回push。

$ git push origin hogehogehogehoge:hogehogehoge
Username for 'https://github.com': hogeuser
Password for 'https://hogeuser@github.com':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/hogeuser/hogerepo.git/'

まだだめ

$ git remote -v
origin https://github.com/hogeuser/hogerepo.git (fetch)
origin https://github.com/hogeuser/hogerepo.git (push)

originの設定をhttpsからsshに変更する。sshのアドレスは、githubのサイトでsshをクリックしSSH clone URLでひょうじされたもの。

$ git remote set-url origin git@github.com:hogeuser/hogerepo.git
testFramework $ git remote -v
origin git@github.com:hogeuser/hogerepo.git (fetch)
origin git@github.com:hogeuser/hogerepo.git (push)

再度push。今度はpushできた

$ git push origin hogehogehogehoge:hogehogehoge

refs
Generation ssh keys
ターミナルからgithubのリポジトリにアクセスする設定
ssh鍵認証 ssh-keygenコマンドで作成する
ssh-keygen 認証用の鍵を生成

■Permission denied (publickey).エラー

違うpc(ubuntu)でも同じ秘密鍵を使おうとして、上の手順で設定したけど、リモートとの通信でPermission deniedエラーが発生した。

$ git push origin master:master
Warning: Permanently added the RSA host key for IP address '192.**.***.131' to the list of known hosts.
Permission denied (publickey).

sshで確認してもダメ。

$ ssh -T git@github.com
Permission denied (publickey).

ssh-agentに秘密鍵の登録作業が必要らしい。ssh-addコマンドでできる。引数に秘密鍵を指定する。また、鍵のパスフレーズの入力が必要。

$ ssh-add ~/.ssh/github_id_rsa
Enter passphrase for /home/hogeuser/.ssh/github_id_rsa:
Identity added: /home/hogeuser/.ssh/github_id_rsa (/home/hogeuser/.ssh/github_id_rsa)

今度はできた

$ ssh -T git@github.com
Hi hogeuser! You've successfully authenticated, but GitHub does not provide shell access.

refs
GithubでSSH通信した時に「Permission denied (publickey).」が発生した時の対処方法。
番外編】Permission denied (publickey). fatal: The remote end hung up unexpectedly と言われ続けたらssion denied (publickey). fatal: The remote end hung up unexpectedly と言われ続けたら…m
ssh-agentについて


■Permission denied (publickey).エラー2

また、リモートと通信しようとしたらこのエラーが発生(aws,sakuraのインスタンスとかで)。対処法は上で書いたけど、ssh-agentを起動してssh-addで公開鍵を登録すればいい。

$ eval `ssh-agent` // ssh-agent を起動
$ ssh-add ~/.ssh/hogekey.pem

refs : http://kyamada.hatenablog.com/entry/2012/05/16/225351

で、これを起動するたびに毎回実行するのは面倒。そんなときは、~/.bashrcに以下を登録

#ssh-agent
eval `ssh-agent`
ssh-add

refs : http://takanosho.wordpress.com/2013/01/11/input-ssh-key-passphrase-only-once-on-git-bash/




ローカル環境のroot権限でgithubのリモートリポジトリと接続して、cloneやpushをしようと試みたら、鍵関連のエラーが発生したことがあった。~/.ssh/ディレクトリ下に鍵はあるはずなのに。鍵は秘密鍵のid_rsa_githubと公開鍵のid_rsa_github.pub。で、よく考えたらgithubに登録している鍵は別のユーザhogeuserの鍵だった。鍵違い。なので、hogeuserの鍵をrootの鍵に上書きコピーする。rootユーザで下を実行。

$ cp ~hogeuser/.ssh/id_rsa_github ~/.ssh/
$ cp ~hogeuser/.ssh/id_rsa_github.pub ~/.ssh/

これでリモートと接続できるようになった。

■githubでgit cloneしたときにPermission denied(publickey)
githubのあるリポジトリをcloneしたときに下のエラーが出てできなかった。
Permission denied (publickey).
fatal: Could not read from remote repository.

~/.ssh/下に鍵はある。そんなときはssh-agentにgithubの鍵を登録したら解決できるかも

$eval `ssh-agent`
$ ssh-add ~/.ssh/hoge_rsa

refs:githubからcloneするとpermissionエラーが発生する


■remoteへgit pushしてrejectされたら、git pullしてエラーが出たとき
・git push origin hogebra:hogebra
これで下のエラーが出た。
>git push c:\tmp\gittest\test master
To c:\tmp\gittest\test
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'c:\tmp\gittest\test'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

で、git pullしろと書いてあるからしてみたけどできない・・・git fetchしてリモートの変更を取得して、git mergeでマージしたらできた。

$git fetch
$git merge origin/hogebra

refs:git pushがrejectされたときの対処方法

もしこれでもダメなら、fetchしてrebaseしたり、pull -commitやpull -rebaseという手段もあるらしい・・・複雑・

■git cloneで Permission denied (publickey).エラー
git cloneしたら、下のエラーが出た

$ git clone ssh://ec2-user@52.111.111.111/home/ec2-user/hoge.git hoge-local.git
Cloning into 'hoge-local.git'...
Permission denied (publickey).
fatal: Could not read from remote repository.

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

鍵はpermisonは400、~/.ssh/configファイルもある。で、結局問題は鍵の所有者、所有グループがe2-userとは別の人になっていたからだった。なのでそれを変更

$ sudo chown ec2-user hoge_key.pem
$ sudo chgrp ec2-user hoge_key.pem

■git clone --bareで中央集権リポジトリの複製
hostnameはconfigで設定済みのHost
git clone --bare ssh://hostname/home/ssh-user/hoge.git hoge.git
refs:gitサーバ、サーバ用のgitを取得
Git サーバーを立ち上げるには、既存のリポジトリをエクスポートして新たなベアリポジトリ (作業ディレクトリを持たないリポジトリ) を 作らなければなりません。これは簡単にできます。リポジトリをクローンして新たにベアリポジトリを作成するには、clone コマンドでオプ ション --bare を指定します。

あと、これは本当に必要か疑問だけど、サーバリポジトリURLにエイリアスoriginをつける
git remote add origin ssh://hostname/home/ssh-user/hoge.git hoge.git

ref
超入門:"git push origin master"の"push"と"origin"と"master"の意味がわからないあなたへ
githubで新規リポジトリ作成のあとのpushコマンドについてメモ


git tutorial for designers
GitとGithubの使い方~超初級編~
Gitメモ(Hishidama's Git Memo)
ブランチの切り替えとHEADとチェックアウト
 
入門git/オーム社
¥2,520
Amazon.co.jp

アリスとボブのGit入門レッスン/秀和システム
¥2,310
Amazon.co.jp


■todo
・git fetch,git diffについて
・git logで見れる過去のコミットとのdiff
・non-fast-forward,fast-forwards