git reset 取り消し

git reset 取り消し

間違えて

git reset --hard 

したときの対処

いざという時のためのgit reflog

にあるように

git reflog 
[/shell:で過去のあらゆるコミット履歴をみれる

72e7d9c (HEAD -> master) HEAD@{0}: reset: moving to 72e7
861751a HEAD@{1}: commit: ver3
91c72a0 HEAD@{2}: commit: ver2(
72e7d9c (HEAD -> master) HEAD@{3}: commit (initial): ver1

なお、ubuntu で

git commit

を実行すると
vim ではなく nano エディタが起動

git のリモートリポジトリの修正

Github Page をつくるのに cloud9 を使おうとしたけど
cloud9 の端末操作でURLのコピペができなかったため、
手打ちですべてやったため、URLの間違いが発生

本来なら、

git remote add origin https://github.com/Snowpooll/snowpool.github.io.git

とするはずが

git remote rm https://github.com/Snowpool/snowpool.github.io.git

と1文字違いで登録してしまい、これが原因で修正することになりました

そのままリポジトリ登録すればいいかなと思っても

fatal: remote origin already exists.

となってしまうため、一度 origin を削除する必要があります

このため

git remote rm origin

を実行し、origin を削除

次に

git remote add origin https://github.com/Snowpooll/snowpool.github.io.git

で再度登録

git push origin master 

とすることで解決しました

リポジトリ共有時のトラブル解決

リポジトリ共有時のトラブル解決
#22 共有時のトラブルを解決する
http://dotinstall.com/lessons/basic_git/6722
を参考に、
共有ゆえに起こるトラブルの解決方法の学習
これは共有であるために起きるトラブルとして
ありえるのが
ユーザBが編集し commit までしたことを知らず
ユーザAが同じ場所を編集し
このためコンフリクトが発生して
エラーがでる
というもの
とりあえず、実践したほうがわかりやすかったので
cd ~/myweb2/

ディレクトリを移動して
vim index.html
で最終行へ
share test
と記述して保存
git add.
git commit -m “share test added”
git push origin master
で変更した内容を共有リポジトリに送ります
ここまでは問題なしです
次に、ユーザA体験をします
cd ~/myweb
で移動して
vim index.html

share start !
と追記して保存します
そして、今回は git add と git commit を同時に行います
git commit -am “shared start”
というように
git commit のオプションに
-am
をつけるだけです
実際に git commit までできているか確認するには
git log
とすれば
commit 2a08e46f36ed7d10a431adc6fdf227548212902c
Author: snowpool
Date: Fri May 31 21:33:01 2013 +0900
shared start
というように、無事に commit されているのが
確認できます
ここまでは問題ないのですが
ここから共有リポジトリに送ろうとすると
エラーで止まります
git push origin master
を実行すると
To /home/snowpool/ourweb.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘/home/snowpool/ourweb.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.
となり、エラーがおきて先にすすめません
hint: its remote counterpart. Merge the remote changes (e.g. ‘git pull’)
とあるように、
先に git pull するように促されます
この場合、
git push origin master
を実行します
修正箇所が異なっている分には問題があまりないのですが
修正箇所が同じ場合コンフリクトが発生します
このため、修正することが必要になります
今回、 vim index.html でファイルを開くと
以前、コンフリクトの修正をしたときのように
line 1st
line2
line3
line end
<<<<<<< HEAD share start! ======= share test >>>>>>> c11d37315a0378907809998e842de7b1a59ffbde
となっているので、
line 1st
line2
line3
line end
share start!
というように修正し、保存します
あとは、インデックス化とコミットを両方一気に行うので
git commit -am “conflict fixed”
そして再び
git push origin master
を実行すると、無事に実行できます
このように、共有リポジトリの場合
git push
git pull
を多用することになりそうです
あと、Git サーバー – プロトコルがあって
Local
SSH
Git
HTTP/S
といったプロトコルが使えます
それぞれ利点、欠点がありますので
用途により使い分けすることになると思います
プロトコルに関しては
Git サーバー – プロトコル
に詳細がかかれています

git のリポジトリ内容の共有

git のリポジトリ内容の共有
#21 リポジトリの内容を共有してみよう
http://dotinstall.com/lessons/basic_git/6721
をもとに、
複数ユーザでの内容の共有について学習
今回は、共有リポジトリの内容を
myweb2
というディレクトリの中に作成する
これには
git clone
を使う
git clone ~/ourweb.git/ myweb2
と実行すると
Cloning into ‘myweb2’…
done.
となり、
新しく myweb2 というディレクトリが作成され
その中にリポジトリも作成されている
書式としては
git clone 共有リポジトリのURL 新しいディレクトリ
というかんじになる
内容ができているか、確認するには
cd myweb2/
git log
で確認できる
そして、今度は、myweb2 の中でファイルを編集して
それを 共有リポジトリに追加する
vim index.html
でファイルを開いて
line end
と追記して保存
git add .
でインデックス化
git commit -m “line end added”
でリポジトリ登録
git clone したときに、リモート情報も
引き継ぎできているので
git push で共有リポジトリに追加することができる
今回なら
git push origin master
とすれば、URLの指定をせずに、そのまま送ることができる
これは、リモート情報も引き継ぎしているため
とりあえず、ユーザB役での編集などはできたので
今度は
ユーザA役の myweb ディレクトリで操作する
Linux なら端末を
Ctrl + Alt + t で開けるので
新しい端末を開いて
cd myweb/
で移動
共有リポジトリの内容をマージするには
git pull
を使う
でも、違いを理解するために
まずは git log で内容の確認
commit 0180f5a3844aa7df2cb4c231f03df5e61f36efe9
Author: snowpool
Date: Thu May 30 20:51:12 2013 +0900
line3 added
が最新の内容
確認したら、 git pull で内容を共有リポジトリから引っ張ってくる
今回なら
git pull origin master
とする
これで、git log で内容を確認すると
commit b3c348a6f9fa5416831020ad5649fbdf09050acc
Author: snowpool
Date: Fri May 31 20:57:41 2013 +0900
line end added
とでているので、
ユーザB役の myweb2 ディレクトリで編集したものも
しっかりと反映されていることが確認できる
これらのことから、
git clone で共有リポジトリを作成
git push で編集した内容をアップする
git pull で編集された内容をダウンロードして編集する
というような流れで複数の人による共同開発が可能となる
もっとも、複数人数での開発以外にも
git サーバーを構築して
自分の複数マシンでソースを共有することで
外出先でもソースの編集を容易にするという使い方もできるので
git 関連をやっておくと便利だと思う

git の共有リポジトリの設定

git の共有リポジトリの設定
#19 はじめての共同作業
http://dotinstall.com/lessons/basic_git/6719
を参考に
git での共有リポジトリを作成
これを使い、後々複数人で共同作業できるようにする予定
これを使えば、他の人とソースを共有して開発ができる
共有リポジトリは、本来ならネットに公開して使うもの

.git と名前を付ける
今回は ourweb.git という名前にする
今回は
ユーザAが myweb ディレクトリ
ユーザBが myweb2 ディレクトリで作成している状態
まず、共有ディレクトリにするために
mkdir ourweb.git

ourweb.git というディレクトリを作成する
cd ourweb.git
でディレクトリを移動して
共有リポジトリを作成する
共有リポジトリを作成するには
git init –bare
とする
–bare オプションをつけることで
これにより管理ファイルだけの管理となり
ここでは commit などをしないようになる
とりあえず、共有の設定はできたので
さっきまで作業していたディレクトリへ移動する
cd ../myweb
これでユーザA役のディレクトリに移動したので
#20 共有リポジトリにpushしてみよう
http://dotinstall.com/lessons/basic_git/6720
を参考にすすめていく
まずは、共有のリポジトリを登録するので
別のリポジトリを登録する
git remote
を使う
追加には add をつけるので
git remote add origin ~/ourweb.git
となる
書式としては
git remote add origin gitのパス
となる
git のパスは http:// というようなURLでもOK
git config -l
で状態確認できるので
ここで remote.origin のURLの確認もできます
core.editor=vim
alias.co=checkout
alias.st=status
alias.br=branch
alias.ci=commit
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=/home/snowpool/ourweb.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
が実行結果で
remote.origin.url=/home/snowpool/ourweb.git
がURLとなります
もし、削除したいのなら
git remote rm origin gitのパス
で削除も可能
ここまでで設定ができたので
master ブランチの内容を
共有リポジトリにいれる
これは
git push origin master
とすればできる
成功すると
Counting objects: 20, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (20/20), 1.76 KiB, done.
Total 20 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (20/20), done.
To /home/snowpool/ourweb.git
* [new branch] master -> master
というメッセージがでてくる
転送の書式は
git push origin ブランチ名

git で短縮名のエイリアスを使う

git で短縮名のエイリアスを使う
#18 エイリアスを使ってみよう
http://dotinstall.com/lessons/basic_git/6718
を参考に
エイリアスについて学習
これは git のコマンドを短縮名でつかうというもの
例えば
git checkout
これを短縮したいなら
git config –global alias.co checkout
とする
これと同様に
status
branch
commit
これらも短縮で使えるように登録しておく
git config –global alias.st status
git config –global alias.br branch
git config –global alias.ci commit
こうしておくことで
bit branch を
git br
で行うことができる
Linux の場合、Tab キーによる補完機能を使えば
ある程度は楽できるけど
このエイリアスを設定することでよりやりやすくできる
設定したエイリアスについては
git config -l
とすることで確認が可能
私の環境の場合だと
core.editor=vim
alias.co=checkout
alias.st=status
alias.br=branch
alias.ci=commit
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
となっています

git でのタグ

git でのタグ
git をわかりやすくするためのタグについて学習
参考サイトは
#17 タグを使ってみよう
http://dotinstall.com/lessons/basic_git/6717
git のタグは
git commit したときのID
commit cce3862b9c30a544c772e675ccd5f6a98e345022
というような数字の羅列ではわかりにくいので
これをわかりやすくするために
タグを使う
まずは、実践したほうがわかりやすいので
vim index.html
で最後に
line3 を追記して保存
そして
git add .
git commit -m “line3 added”
でリポジトリ作成
これで git log で確認すると
commit 0180f5a3844aa7df2cb4c231f03df5e61f36efe9
Author: snowpool
Date: Thu May 30 20:51:12 2013 +0900
line3 added
が追加されているのを確認できる
今回は、まずここへ version 1.0 というタグをつける
直近の commit に対してタグを付けるには
git tag v1.0
というようにする
書式は
git tag タグ名
また
git tag
とすれば、タグの一覧をみることができる
また、タグを付けることで
タグからcommit 内容をみることも可能
git show v1.0
というように
git show タグ名
とすれば
commit 0180f5a3844aa7df2cb4c231f03df5e61f36efe9
Author: snowpool
Date: Thu May 30 20:51:12 2013 +0900
line3 added
diff –git a/index.html b/index.html
index fc3bbf4..705cd1f 100644
— a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
line 1st
line2
+line3
というように、タグをつけた commit の内容をみることができる
タグ名は git tag で確認できる
git tag タグ名は、直近の commit だけで
もっと前のものにもタグをつけたいのなら
git tag タグ名 commitのID
とする
commit のIDは、全部ではなく
最初から7行ぐらいまででOK
例えば
commit 0180f5a3844aa7df2cb4c231f03df5e61f36efe9
のIDのところに v0.9 としたいのなら
git tag v0.9 0180f5a3844aa7
というようにする
これで、git tag
とすれば
v0.9
v1.0
となる
あとかた追加したタグでも
git show v0.9
とすることで
commit 0180f5a3844aa7df2cb4c231f03df5e61f36efe9
Author: snowpool
Date: Thu May 30 20:51:12 2013 +0900
line3 added
diff –git a/index.html b/index.html
index fc3bbf4..705cd1f 100644
— a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
line 1st
line2
+line3
というように、問題なく追加できる
また、タグを削除したいときには
git tag -d v0.9
というように
git tag -d タグ名
とすれば消す事ができる
成功すると
Deleted tag ‘v0.9’ (was 0180f5a)
というようなメッセージがでる
消えているか確認するには
git tag
で一覧がでるので、ここからなくなっていれば消去成功

マージの衝突を解決

マージの衝突を解決
#15 マージの衝突を解決してみよう (1)
http://dotinstall.com/lessons/basic_git/6715
を参考に
マージの衝突を解決する
新しくブランチをつくって
さらに、そのブランチに移動する
つまり checkout まで行うには
git checkout -b hogehoge
というようにすればOK
書式としては
git checkout -b ブランチ名
となる
確認のため
git branch とすると
hoge
* hogehoge
master
となっていて
作成した hogehoge に移動しているのが確認できる
ここからは git のマージの衝突を実行
vim index.html
でファイルを
line1 から line first へ変更して保存
git add .
でインデックス化して
git commit -m “not 1 but fitst”
とすることで
vim を起動せずにリポジトリに保存
ここまではいままでと同じ
ここからが衝突
まず、ブランチを hogehoge から master に変えるので
git checkout master
そして、 vim index.html で編集
中身は、ブランチごとにわけられているので
line1
をmaster のほうでは
line 1st
と変更して保存
そして、同じように
git add .
git commit -m “not 1 but 1st”
と実行
この状態でマージすると衝突が発生する
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
このように、問題発生して
解決しないと git commit できないという状況になる
この解決方法が動画で紹介されているのが
#16 マージの衝突を解決してみよう (2)
http://dotinstall.com/lessons/basic_git/6716
のほう
現状をまずは git status で確認
# On branch master
# Unmerged paths:
# (use “git add/rm …” as appropriate to mark resolution)
#
# both modified: index.html
#
no changes added to commit (use “git add” and/or “git commit -a”)
とメッセージがでて
修正を促される
このため、vim index.html でファイルを修正する
ファイルを開くと
<<<<<<< HEAD line 1st ======= line first >>>>>>> hogehoge
line2
となっている
現在のブランチは master のほうで
<<<<<<< HEAD line 1st ======= が master のほう line first >>>>>>> hogehoge
line2
が hogehoge ブランチのほうで
このうちどちらかを残すか、もしくは修正することになる
今回は master のほうを残しておきたいので
修正するには
<<<<<<< HEAD line 1st ======= line first >>>>>>> hogehoge
line2

line 1st
line2
というように、いらない部分を削除して保存
あとは git add .
git commit -m “confilict fixed”
というように、ふつうに git commit できるようになる
確認するには
git log

commit 734665444c30621d32b3800d56ffd1dcd5583201
Merge: 6379343 a989662
Author: snowpool
Date: Thu May 30 20:09:07 2013 +0900
confilict fixed
commit 637934389be80431ac468343f50129bb0a587018
Author: snowpool
Date: Thu May 30 19:57:21 2013 +0900
not 1 but 1st
commit a989662710cb5542428878d9de9028c89920c2f7
Author: snowpool
Date: Thu May 30 19:52:36 2013 +0900
not 1 but fitst
commit cce3862b9c30a544c772e675ccd5f6a98e345022
Author: snowpool
Date: Thu May 30 07:02:34 2013 +0900
script added
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
(END)
commit a989662710cb5542428878d9de9028c89920c2f7
Author: snowpool
Date: Thu May 30 19:52:36 2013 +0900
not 1 but fitst
以下略
というように
commit できているのが確認できる

git でブランチをマージする

git でブランチをマージする
#14 ブランチをマージしてみよう
http://dotinstall.com/lessons/basic_git/6714
を参考に
ブランチをマージする方法を学習
前回作成した hoge の中で作成した
myscript を
master のほうへ反映させたいときには
git merge
を使う
まず、 git checkout master
で hoge から master にブランチを移動する
次に
git merge hoge
とする
書式としては
git merge 取り込みたいブランチ名
というかんじ
結果に
Updating 1110869..cce3862
Fast-forward
myscript.js | 1 +
1 file changed, 1 insertion(+)
create mode 100644 myscript.js
とでて反映される
本当に反映されているか確認したいときには
git log
を実行して
commit cce3862b9c30a544c772e675ccd5f6a98e345022
Author: snowpool
Date: Thu May 30 07:02:34 2013 +0900
script added
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
というように、script added が追加されていることを確認できる
もし、作成した hoge が
もう機能を取り込んだので、不必要なので消したい
というときには
git branch -d hoge
このように、テスト環境のブランチを作成し
いらなくなったら
git branch -d ブランチ名
で削除する
というようにしていくことで、効率的な開発が可能になる

http://dotinstall.com/lessons/basic_git/6714

の補足情報に
ほかにもいろいろ載っているので
こちらも参考にしながら使っていこうと思います

git でブランチ

git でブランチ
#13 ブランチを使ってみよう
http://dotinstall.com/lessons/basic_git/6713
を参考に、ブランチを行ってみた
ブランチとは
分岐、枝分かれという意味
複数バージョンを平行して開発するときに使う
git branch
とすると
いまあるブランチ一覧がみれる
最初は
master
のみ
ブランチを作成するには
git branch ブランチ名
とする
今回は
git branch hoge
この状態で git branch すると
hoge
*master
というように2つになっている
master の横に*がついているけど
これは今いるブランチを示している
なので、hoge に移動する
移動するときには
git checkout ブランチ名
今回は
git checkout hoge
で成功すると
Switched to branch ‘hoge’
と表示される
この状態で git branch で確認すると
* hoge
master
というように
hoge のほうに移動して*がついているのが確認できる
これにより、各ブランチごとに状態をわけることが可能
つまり、データベースに近い感覚
使うデータベースが ブランチ
中のテーブルとかが それぞれのバージョン
というかんじになる
試しに
vim myscript.js
を作成して、中身を
alert();
だけ記述して保存
git add .
git commit -m “script added”
で追加
git log
で確認すると
commit cce3862b9c30a544c772e675ccd5f6a98e345022
Author: snowpool
Date: Thu May 30 07:02:34 2013 +0900
script added
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
となっている
この状態から
master のほうへ戻りたいときには
git checkout master
を実行
そして、 git log を実行して状態を確認してみると
commit 1110869d4bef57d3502c434ad9f4a94c8633242f
Author: snowpool
Date: Wed May 29 20:55:00 2013 +0900
line2 を追加
commit 53d156650c331b31b33c8907f25527e61978dfcf
Author: snowpool
Date: Tue May 28 21:28:11 2013 +0900
initial commit
というように、
hoge のほうでは追加した script added
が入っていないことが確認できる
web 開発をするときに最初にベースを作って
そこから派生して作成するというときに便利