初めてのGit リポジトリの作成からgithubへpushするまで

初めてのgit

まずはローカルに新規リポジトリを作成しましょう

git init


ファイルの作成や編集をしたら、addでステージにあげましょう

git add filename
<||


ステージに上げたファイルの変更は--cahedオプションで確認できます
>||
git diff --cached


変更を保存するときはcommitです

git commit


またcommitのときにuser.nameとuser.emailを定義しておかないと怒られるので設定しておきましょう

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global --list //確認

初めてのgithub

まずはリモートリポジトリを作成します
githubのマイページのtopにアクセスし、
Repositories->New->nameとdescriptionを入力ー>create repository と行えば簡単に作成できます

作成できたらローカルのリポジトリとリモートリポジトリを紐付けしましょう

git remote add origin https://github.com/username/hoge.git


git pushをする前にgithubssh鍵登録しておきましょう

ssh-keygen
cd .ssh/
cat id_rsa.pub

catされた値をコピーしておき、

githubにログイン->settings->SSH keys->Add SSH key->titleとkey(コピーしておいたid_rsa.pubの値)を入力->add key で登録ができます


登録できたら実際にsshして確認しましょう

ssh -T git@github.com

最後にリモートにpushしましょう!

git push -u origin master

ブラウザから確認し、コミットの結果が反映されてたら完了です

※鍵登録したのにpushやpullのリモート操作時にパスワードをきかれる場合

httpsでリモートリポジトリのURLが設定されているのが原因の可能性が高いです
ssh経由にconfigを書き換えてみましょう

url = https://github.com/{$account}/{$repository_name}.git
↓
url = git@github.com:{$account}/{$repository_name}.git