小枝から幹をめざして

今は小枝しか拾えてないけどいつか幹にたどり着くんだ。個人的な勉強の忘備録PHP,MySQL,javascript

gitの使い方 自分用のまとめ

●ユーザー、emailアドレスをを登録する


ユーザー名/.gitconfig
というファイルに書いてある。

$ git config --global user.name "taro707"
$ git config --global user.email "nao_you_ni@yahoo.co.jp"
$ git config --global color.ui auto

C:\User\ari\.gitconfig
[user]
name = taro707
email = nao_you_ni@yahoo.co.jp
[color]
ui = auto

リポジトリを作成する

$ mkdir tutorial //好きな場所に移動して任意のディレクトリを作り
$ cd tutorial //その中へ入る
$ git init //Gitリポジトリに設定

隠しフォルダ.gitが作成される。
.git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly

●ファイルを作ってコミットする
$ touch sample.txt //空のファイルを作成
$ git status //ステータス確認。sample.txtがトラッキング(追跡)されていないと赤文字で表示される (ステージングされていない??)


$ git add sample.txt //ファイルをインデックスに追加
$ git add . //そのディレクトリにある全てのファイルを追加
$ git add * //サブディレクトリを含む全てのファイルを追加

$ git commit -m "first commit" //メッセージつきでコミットする

>Git は「git add コマンドを実行した時点の状態のファイル」をステージする


>git commit コマンドに -a オプションを指定すると、追跡対象となっているファイルを自動的にステージしてからコミットを行います
追跡対象とステージングの違いは…???

ためしにsample2.txtを作ってみて
$git commit -m "last" -a としてみたら(-aは自動ステージング?addがいらなくなる)
Untracked files : sample2.txtと出た
addを一度すれば永久に「追跡状態」にはなるということか?


ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
$ git commit -a -m "test"
$ git commit --amend -m "testtest"
前回のコミットtestがtesttestで上書きされる。


-----------------------------------------------------------------
コミットする前のsample.txtの変更を元に戻す方法
$git checkout -- sample2.txt
変更が元に戻った。

-----------------------------------------------------------------


ここまでローカル
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

●リモートリポジトリにpush

push = リモートリポジトリをローカルリポジトリと同じ状態にする
clone = リモートリポジトリからローカルリポジトリへ複製する
pull = リモートリポジトリから最新の変更履歴をローカルへダウンロードする





$ git remote //今までのリモートリポジトリが見える
$ git remote add [shortname][url]//リモートリポジトリの追加


$ git push -u origin master
mihotan
69696969

------------------------------------------------

$ git clone <repository> <directory>
(ユーザー名、パスワードが求められる)
tutorial2というディレクトリが生成され、その中にtest0528のファイルがそのまま入る

--------------------------------------------------
●cloneしたtutorial2を変更してcommitする

$ cd tutorial
$ git commit -a -m "tutorial2commit"

●それをリポジトリにpushする

$ git push
-------------------------------------------------
リポジトリからtest0528にpullする

$ git pull 
//省略するとorigin masterからpullされる
tutorial2で行った変更(sample2.txtの変更)がtest0528でも反映されている。
-------------------------------------------------