cui、コマンドを使ってgithubを使うメモ書き。

guiのtortoiseGitについてはこっちを参照(msysgit,gitbashのインストールについても)

よく使うgitコマンド
さるでもわかるgit入門

githubメモ


・リポジトリをcloneする
⇒git cloneコマンド。参照「githubからcloneする

・ゆーざめい、パスワードとか設定を確認する
⇒git config -l

■ブランチ
ブランチには統合ブランチとトピックブランチがあるらしい・・・統合ブランチは俗にいうmaster
refs:ブランチ、ブランチの運用

・ブランチ一覧表示
⇒対象のリポジトリのディレクトリに移動して、git branchコマンド

・ローカルリポジトリのブランチ削除
⇒「git branch -d hogebranch」。強制削除の場合「git branch -D hogebranch」

・リモートリポジトリのブランチ削除
⇒git push origin :hogeremotebranch
空のブランチをpushしている。空のブランチは空文字で指定する。originはリモートリポジトリのこと。
⇒git push --delete origin hogeremotebranch でもおっけ。deleteの前はダブルハイフン。

・リモートブランチにプッシュ
⇒git push origin master
リモートブランチにブランチがない場合、ブランチを新規作成してくれる。origin masterがないと、オプションによって全ブランチをプッシュするようなので注意。

・リモートブランチを作成
git push origin hogebra:hogehogehoge
ローカルリポジトリのhogebraブランチをoriginリポジトリのhogehogehogeブランチを作成してpushする。

リモートにブランチを単体で作るってのは無理か・・

■checkout(チェックアウト)

・git checkout hogebra
hogebraブランチに切り替え

・git checkout -b hogebra
hogebraブランチを作成し、切り替える。ブランチ作るなら、git branchでもおっけ。

refs:git checkoutの使い方

あと、addしてステージング状態にしたファイル(作業ワークツリー)をもとに戻すときにも使われる。

git checkout -- hoge.txt
git checkout .

refs:作業ツリーの変更をもとに戻す

■fetch,merge,pull
refs:git fetchの理解からgti mergeとpullの役割
pull = fetch + merge origin/master
fetchはローカルリポジトリにファイルを持って生きているだけで、実際に使っているファイるには反映されていない。origin/masterが最新の状態に更新され、masterはその分の更新がされていない状況。
ローカルのmasterブランチにmergeするには、git merge origin/master。
git pullはfetch,mergeを一気にする。コンフリクトが発生する可能性がある。

・git pull https://github.com/hogeuser/hogerepo.git hogebra hogebra
リモート(github)のhogerepoリポジトリのhogebraブランチをローカルのhogebraブランチにpullする

httpsじゃなくてもgit pullできる
git pull git@github.com:hogeuser/hogerepo.git hogebra hogebra
ただし、RSA認証鍵が必要?jenkinsのシェルスクリプトとかで使える。

■そのほかよく使うコマンド

○add,commit(ローカルリポジトリ)
・git add .
⇒ピリオドで変更すべてをaddする。addしてステージングエリアへ
・git commit -m"commit comment"
⇒コミットする
・git commit -am"add + commit comment"
addとcommitを同時にする

refs:git入門(どっといんすとーる)

○こんふぃぐ系
git config
git config -l
git config --help
git help config

○初期化からステージングエリア(インデックス)へ、ステージングエリア(インデックス)からコミット
git init
⇒初期化
git add(git add .)
⇒ピリオドの場合、いまのディレクトリより下にあるすべてのファイルが対象
git commit


git commitは、ユーザ名とemailを設定しないとできない。ユーザ名とemailは下記で設定できる。
git config --global user.name ”(your name)"
git config --global user.email "(your email)"

○git diff
git diff
⇒ステージング前との差分
git diff --cached
⇒ステージング後、コミット前との差分

■git diffの差分の行が長い行で改行されずに見れないとき

refs : http://d.hatena.ne.jp/sugyan/20100507/1273209155

「git diffした時に、横に長い行の表示がコンソールの右につきぬけてしまって、右のほうが見えなくなってしまうんだけど、どうすれば>コンソールの右で折り返して表示してくれるのか誰か教えてくれませんか。MacのTerminalです。」

git diff --color | less -R

または、~/.gitconfigに以下を書けばいい

[color]
ui = auto
[core]
pager = less -r


○削除したいファイルを一括してgit rm
grep,awkとあわせて使う
$git status | grep deleted: | awk '{print $3}' | xargs git rm

○gitの管理に含めないようにする、ファイルを無視
.gitignoreファイルで指定。そのファイルが存在するディレクトリだけでなくサブディレクトリも対象になる。

○git commit --amend
⇒コミットをひとつにまとめる。git logでしょぼいコミットログがたまることを防げるので結構使える。

○ブランチのマージ
git merge hogebranch
⇒ブランチのマージ。masterにいる状態で上を実行するとhogebranchの変更がmasterにマージされる
git branch -d hogebranch
⇒hogebranchを削除する

○マージしたらコンフリクトが発生
$ git merge hoge
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

こんな感じになる。git statusを実行するとboth modifiedとか出る。

$ git status
# On branch master
#
# both modified: index.html
#
no changes added to commit (use "git add" and/or "git commit -a")

問題のindex.htmlの中身を見ると、一応コンフリクトした箇所を教えてくれるらしい。

<<<<<<< HEAD
master line 3
=======
hogehogehoge
>>>>>>> hoge

ファイルを編集してコンフリクトを解消して、git add / git commitをする

○タグ、コミットIDにタグをつける
git tag
git tag (tag name)
git tag (tag name) (commit id)
git tag -d (tag name)

○えいりあす。例えば、git checkoutをgit coにしたい場合

$ git config --global alias.co checkout

$ git co hoge
Switched to branch 'hoge'

○共有りぽじとり
共有リポジトリを初期化する時は--bareをつける
git init --bare

git remote add origin (repos location)
git push origin master

git config -lで確認できる

git clone
git push
git pull

○git stash, git stash pop
hogebraというブランチでなにか作業をして、commitせずにmasterブランチにcheckoutしてみたら、下のエラーが出た。

$ git co master
error: Your local changes to the following files would be overwritten by checkout:

Please, commit your changes or stash them before you can switch branches.

ブランチを移動するときはgit stashで退避させる必要があるらしい・・・再度hogebraで作業を再開したいときはgit stash pop。

stashは隠すという意味
refs:Chapter7 ブランチを使った開発

まだコミットしていない変更内容や新しく追加したファイルが、インデックスやワークツリーに残ったままで、他のブランチへのチェックアウトを行うと、その変更内容は元のブランチから、移動先のブランチに対して移動します。
refs:ブランチの切り替え

○ローカルでhogebraブランチ、リモートでもhogebraブランチ
ローカルでhogebraブランチを作った後、githubでも同期したかったのでhogebraブランチを作った。その後、git branch -aコマンドを実行してもリモート(github)のhogebraブランチが表示されない。そんな時は「git fetch origin」。これで、remoteのbranch一覧を更新して見れるようになる。originはリモートリポジトリにつけた任意の名称

refs:他人がつくったリモートブランチが見えない時は
refs:githubをremote repoとして、macにインストールしたgitから使ってみる

■github入門、初心者、中級者のためのおすすめ本の紹介

入門git/オーム社
¥2,520
Amazon.co.jp

Gitポケットリファレンス/技術評論社
¥2,604
Amazon.co.jp

■既に存在するディレクトリをgitで管理する(ローカルリポジトリ)
同じようにgit init すればいいだけ。git init した直後にgit statusを実行すると、ディレクトリに存在するすべてのファイル、ディレクトリがUntracked files状態になっているから、add,commitすればいい。下はscriptコマンドのログ。

^[[?1034hhogedir $ git status^M
# On branch master^M
#^M
# Initial commit^M
#^M
# Untracked files:^M
# (use "git add <file>..." to include in what will be committed)^M
#^M
# ^[[31mscript.log^[[m^M
# ^[[31mtest2.txt^[[m^M
# ^[[31mtest_dir/^[[m^M
# ^[[31mtext.xt^[[m^M
nothing added to commit but untracked files present (use "git add" to track)^M
hogedir $ git log^M
fatal: bad default revision 'HEAD'^M
hogedir $ git branch^M
hogedir $ ls -al^M

■githubのremoteリポジトリを落とす
前提条件としてhogePrj.gitにはmaster,develop,test-remote-branchというブランチがある。
$ git clone https://github.com/hogename/hogePrj.git
cloneで落とす。privateならuser/passが要求される。
カレントディレクトリにhogePrjというディレクトリが作成され、その中にmasterブランチの内容が落とされている。git branchを実行してみると、masterブランチだけができている。git logもそのまま反映されているっぽい。git statusを実行するとuntracked files....git add . / git commitすると、git statusはきれいなるけど、よけいなコミットログが一つ残るからしない方がいいかもね。

今度はdevelopブランチを落としたい。ちなみにリモートのブランチ一覧を見たい場合、git branch -r

$ git branch -r
origin/HEAD -> origin/master
origin/develop
origin/master
origin/test-remoto-branch

本題。git checkoutを使う。hogePrjディレクトリで下を実行。

$ git checkout -b develop origin/develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'

developが作成するローカルリポジトリのブランチ、origin/developがおとしてくるリモートリポジトリのブランチ。git branchで確認したらdevelopが作成されていた。ちなみにgit checkout ブランチ名 でブランチの切り替えができる。

今度はローカルリポジトリのdevelopブランチのファイルをいくつか変更してremoteにpushする。ちなみにリモート側のファイルを一つ変更しておいた(ローカルとは別ファイル。コンフリクトは発生しない)

$ git push origin develop

error: failed to push some refs to

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

エラーが発生した。コンフリクトが発生しなくてもだめか・・・コンフリクトは発生しないことは解っているので、git pullを実行。git logを実行すると、Merge branchのログが一つ追加されている。この後にもう一回git pushを実行。無事remoteにpushできました。

あと、ローカルでは何も変更せずにリモート側の変更をローカルに反映したい場合、git pullを実行し、そのときにmergeのログが一つのこる。ここで、git statusを実行すると、git pushをみないなメッセージがでる。まぁ実行しなくてもいいと思うけど、git pushを実行すると、リモートのこのログのみ?が反映される

今度はローカルにhogerepというリポジトリを作成し、リモート(github)でhogeremotedirというリポジトリを作成して、ローカル側の編集をgithubにpushする。

・ローカル
mkdir hogerep
cd hogerep
git init
適当なファイルを作る
git add / git commit -m""

・github上
webでリポジトリhogeremotedirを作る(readmeも一緒に作成)

・ローカル
git remote add origin https://github.com/hogeuser/hogeremotedir.git
リモートのリポジトリを登録している。originは適当な名前。https://~はリモートの場所。

git config -lでリモートの場所を確認できる。

・ローカル
git push origin master
readmeのせいで、一度git pullしろといわれた。

git pull https://github.com/hogeuser/hogeremotedir.git master
第3引数はリモートリポジトリ、第4匹数はローカルリポジトリのブランチ

この後に再度、git push origin master。こんどはおっけ

■git push
リモートリポジトリのブランチにpushしたり、ブランチを削除したりできる

refs:githubをremote repoとして、macにインストールしたgitから使ってみる

■git remote
リモートリポジトリの操作、githubのリポジトリとかも登録できる

git remote
リモートリポジトリ一覧を表示

git remote add origin https://github.com/hogeuser/hogerep.git
githubのhttps://github.com/hogeuser/hogerep.gitをoriginという名前でリモート追加

git remoteだけでなく、git config -lでリモートリポジトリが確認できる
remote.origin.url=https://github.com/hogeuser/hogerep.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

また、git remote addした後に、git branch -aを実行してもリモートリポジトリのブランチは表示されない。反映させるためにはgit fetch origin。originはリモートリポジトリにつけた任意の名称。

$ git fetch origin
From https://github.com/hogeuser/hogerepo
* [new branch] develop -> origin/develop
* [new branch] master -> origin/master
* [new branch] test-remoto-branch -> origin/test-remoto-branch

$ git remote remove origin
リモートリポジトリの登録削除。originはリモートリポジトリにつけた名称

$ git remote show origin
remoteリポジトリの情報を表示

$git remote -h
git remoteのヘルプ

refs:リモート操作(猿でもわかる)

■git fetch
$ git fetch
更新。なにを?リモートリポジトリ

$ git fetch origin
originリポジトリを更新、ブランチが表示されないときとか

refs :ブランチの管理

■リモートリポジトリのブランチ削除
$ git push origin :hogebra
originという名前をつけたリモートリポジトリのhogebraブランチを削除

■ブランチを指定してリポートリポジトリにpush

$ git push origin develop:develop_r
otihinという名前のリモートリポジトリのdevelop_rブランチにローカルリポジトリのdevelopブランチをpush

refs:git push 使い方

■リモートリポジトリにpush
$git push origin develop
originという名前のリモートリポジトリのdevelopブランチにローカルリポジトリのdevelopブランチをpushする。
リモートのブランチ名を省略した場合、ローカルリポジトリと同じdevelopにpushされた

これでもいい。ローカルリポジトリのブランチ名も省略
$ git push origin
ローカルのdevelopブランチ上で上のコマンドを実行した場合、ローカルのdevelopブランチがリモートのdevelopブランチにpushされた

■過去のバージョンに戻す。git rest --hard コミットid

$git reset --hard 37brew77ad9a3b
タグを指定してもおっけ
$git reset --hard v1.0


■リモートに存在しないブランチが「git branch -a」でまだ表示される

リモートのブランチを削除したのに、git branch -aでまだ表示されるので消したい場合、それは
$ git remote show origin
で、staleという状態で残っている。これを完全に削除したい場合は
$ git remote prune origin
と実行すればいい。ちなみにstaleは新鮮でない、腐りかけという意味らしい・・・