LoginSignup
3
1

More than 3 years have passed since last update.

git init から git push までおさらい

Last updated at Posted at 2020-12-30

よく使うGitコマンド(個人的に)

●とにかく、作業開始前にブランチをつくる(なるべくmasterブランチ開発はしない)

→ git branch(今のブランチ、他のブランチの存在確認)
→ git branch <任意のブランチ名>
または
→ git checkout -b <任意のブランチ名>
または
→ git branch <任意のブランチ名> origin

●ブランチを切り替える
→ git branch(今のブランチ、他のブランチの存在確認)
→ git checkout <切り替えたいブランチ名>

●よし、キリのよいとこで変更差分(作業)を保存しよう 

 → git add . (変更分全部をワークツリーからステージングへ)
 → git status(確認)
●やべ、やりなおそう
 → git reset HEAD (git add を取り消す、ワークツリー状態に戻る)
 → git status(確認)

●add(ステージング)したからコミット(保存)しよう

 → git commit -m "任意のコミットメッセージ"
 → git status(確認)
 → git show(確認) qキー で終了
●やべ、やりなおそう
→ git reset --hard HEAD^

●よし、リモートブランチにプッシュしよう

 → git push origin master(local作業ブランチ名) master(remote先ブランチ名)
→ git push origin HEAD

gitで作業を保存する

gitのリポジトリを作成し、githubのリモートリポジトリにプッシュする

ターミナル/ローカルでリポジトリ作成

//作業ディレクトリでコマンド実行
%git init
//結果.gitディレクトリ生成 以下OK
Initialized empty Git repository in /Users/user/StaticSiteGeneratorBlog/.git/

//.gitディレクトリ生成を確認
%ls -a
.   ..  .git    BLOG

//gitconfig設定 githubと合わせておく
%git config --global user.name "ユーザーネーム"
%git config --global user.email "githubで使用するメールアドレス"

//git設定の確認
%git config -l
//結果
user.name=***********
user.email=***********@gmail.com

//ssh接続用の公開鍵、秘密鍵生成コマンド
%ssh-keygen -t rsa
//Enterキーを押します* 3

//.sshディレクトリに鍵が生成できているか確認
//移動 
%cd ~/.ssh/  (/Users/user配下の隠しディレクトリ.ssh)
//隠しファイルも表示
%ls -a
//結果
id_rsa          (秘密鍵)
id_rsa.pub      (公開鍵これをGithub側に登録する)  

//githubに公開鍵をコピペする
//catコマンドで公開鍵を見る
%cat id_rsa  (.sshディレクトリ以外から実行する場合 cat ~/.ssh/id_rsa)

//なが〜い公開鍵暗号テキストをコピーしておく
ssh-rsa *****........

//github に移動
URL: github.com/setting/keys
github>setting>SSH and GCP keys>New SSH keyボタンを押す

titleを入力する
keyに公開鍵をペーストする
>Add SSH keyボタンを押す

//ローカルとgithub(リモート)の疎通を確認するコマンド
%ssh -T git@github.com
//結果
Hi ************! You've successfully authenticated, but GitHub does not provide shell access.

リモートリポジトリの作成

githubブラウザ>マイページ上部のRepositories>上部右側のNewボタンを押す

Create a new repositoryページで任意のリポジトリ名を決める
image.png

Create repositoryボタンを押すと
リポジトリページが作成される
image.png

/Users/user/Blog
//リモートリポジトリのURLを追加 "origin"にリポジトリ先を登録する
//URLはブラウザからコピーして、新しく作りたいリモートリポジトリ名"ssg_blog"を書き足す
Blog%git remote add origin https://github.com/**********/Hugo-Blog

//リモート確認
Blog%git remote
//結果
origin

//リモートoriginの設定状態
Blog%git remote -v
//結果
origin  https://github.com/**********/Hugo-Blog (fetch)
origin  https://github.com/**********/Hugo-Blog (push)

ローカルの作業差分をコミットまで(プッシュの直前まで)

/Users/user/Blog
//ローカルの作業状況確認
//作業ディレクトリで
Blog%git status
//結果
On branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        archetypes/
        config.toml
nothing added to commit but untracked files present (use "git add" to track)
 ーーー翻訳ーーー
ブランチマスターについて
まだコミットはありません
追跡されていないファイル
   (「gitadd <file> ...を使用してコミットされる内容に含めます
        archetypes/
        config.toml
コミットに何も追加されていませんが追跡されていないファイルが存在します追跡するにはgitaddを使用してください

//BLOGというディレクトリ作成状況がワーキングツリーにあるので git add でステージングします
Blog%git add /Users/user/StaticSiteGeneratorBlog/BLOG 
Blog%git status
//結果
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   archetypes/default.md
        new file:   config.toml
ーーー翻訳ーーー
ブランチマスターについて
まだコミットはありません
コミットする変更
   (「gitrm --cached <file> ...を使用してステージングを解除します
         新しいファイルarchetypes / default.md
         新しいファイルconfig.toml


//ステージングからローカルリポジトリにコミットする
//コミットメッセージ付きでコミットする
Blog%git commit -m "hugo new site Blogしたところまで" 
//結果
[master (root-commit) 7c66fd3] hugo new site Blogしたところまで
 2 files changed, 9 insertions(+)
 create mode 100644 archetypes/default.md
 create mode 100644 config.toml

//確認
%git status
//結果
On branch master
nothing to commit, working tree clean  //OK

//コミットできたか
%git show
//結果
commit 7c66fd3440a2db1c404de664254b0c634b88f7a9 (HEAD -> master)
Author: Sakagami-Keisuke <iwayasunset@gmail.com>
Date:   Sun Jan 3 15:22:15 2021 +0900

    hugo new site Blogしたところまで

...........OK

リモートリポジトリにプッシュする

今回は作業ブランチを作らずにローカルmasterブランチ→リモートmasterブランチにプッシュ

ターミナル
//ローカルブランチ名確認
%git branch
//結果
* master  (←作業ブランチつくらなかったのでmaster

//リモートリポジトリ(ssg_blog)のmasterブランチにPUSH
//origin=https://github.com/**********/ssg_blog (push)

%git push origin master:master
           宛先  ローカルリモート
//結果OK
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 501 bytes | 501.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/Sakagami-Keisuke/HugoBlog.git
 * [new branch]      master -> master 

プッシュできました
image.png

以降のプッシュは

ターミナル
%git push origin HEAD or  %git push origin master
//結果
Everything up-to-date
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1