見出し画像

Gitを使いこなすには

ずっとGitが使いたかった。

勉強会とかでGitHubはインストして使ってはいるが、全くもって使い方は分からない。

で、ようやくGItをインストした。
あっ、コマンドプロンプトなんだって思った。

では、こつこつ理解していこうとCharGOT君に教えてもらう。


さあ、勉強スタート

①リポジトリの作成:
Gitを使うためには、まずリポジトリ(プロジェクトのフォルダー)を作成する必要があります。以下のコマンドを使用してリポジトリを作成します。
(コツ)これって、プロジェクトのフォルダーに移動してコマンドを実行するのね。すると、.gitフォルダーが追加されました。
ということで、作成する時だけ実行する。

git init

②ファイルの追加:
リポジトリ内にファイルを追加するには、以下のコマンドを使用します。

git add ファイル名

例えば、`git add index.html`と入力すると、`index.html`という名前のファイルが追加されます。 (コツ)実際に使い方が分かる(保管ですもの)と当たり前なんですが、、、もともとソースを作成していたフォルダからgit add のファイル名で指定してエラーになりました💦 追加したいファイルはどこに置くの? --->登録する作業ディレクトリの下に保管します。

ステージングエリアとはgitレポジトリにコミットするファイルを置いておくためのエリアです。
ステージングエリアがあることによって、パスワードが書いてあるファイルや、環境変数が書いてあるファイルなど、gitにコミットしたくないファイルを分けることができます。つまり、以下のような流れとなります。

●作業ディレクトリ:全てのファイル
●ステージングエリア:作業ディレクトリから、git add で追加されたファイルが格納される。
●gitレポジトリ:ステージングエリアから、git commit でコミットされたファイルが格納される。

まっとにかく使ってみましょう!!

user@CS03432 MINGW64 ~
$ git add C:\Users\user\samy\ボールド\シェルスクリプト\6th-\6th\cpu_moniter.sh
fatal: not a git repository (or any of the parent directories): .git
user@CS03432 MINGW64 ~/github/sample (master)
$ ls -al
total 4
drwxr-xr-x 1 user 197609 0 Dec 24 16:58 ./
drwxr-xr-x 1 user 197609 0 Dec 24 16:39 ../
drwxr-xr-x 1 user 197609 0 Dec 24 16:58 .git/

user@CS03432 MINGW64 ~/github/sample (master)
$ git add C:\Users\user\samy\ボールド\シェルスクリプト\6th-\6th\cpu_moniter.sh
fatal: C:Usersusersamyボールドシェルスクリプト6th-6thcpu_moniter.sh: 'C:Usersusersamyボールドシェルスクリプト6th-6thcpu_moniter.sh' is outside repository at 'C:/Users/user/github/sample'

user@CS03432 MINGW64 ~/github/sample (master)
$ git add ^[[200~C:\Users\user\github\sample\data~\cpu_moniter.sh
fatal: pathspec '?[200~C:Usersusergithubsampledata~cpu_moniter.sh' did not match any files

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        data/

nothing added to commit but untracked files present (use "git add" to track)

$ rm -r data

user@CS03432 MINGW64 ~/github/sample (master)
$ ls -al
total 4
drwxr-xr-x 1 user 197609 0 Dec 29 11:30 ./
drwxr-xr-x 1 user 197609 0 Dec 24 16:39 ../
drwxr-xr-x 1 user 197609 0 Dec 29 11:29 .git/

user@CS03432 MINGW64 ~/github/sample (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)



user@CS03432 MINGW64 ~/github/sample (master)
$ ls -al
total 8
drwxr-xr-x 1 user 197609   0 Dec 29 11:36 ./
drwxr-xr-x 1 user 197609   0 Dec 24 16:39 ../
drwxr-xr-x 1 user 197609   0 Dec 29 11:34 .git/
-rwxr-xr-x 1 user 197609 534 Dec 28 20:35 cpu_moniter.sh*

user@CS03432 MINGW64 ~/github/sample (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        cpu_moniter.sh

nothing added to commit but untracked files present (use "git add" to track)

user@CS03432 MINGW64 ~/github/sample (master)
$ git add cpu_moniter.sh
warning: in the working copy of 'cpu_moniter.sh', LF will be replaced by CRLF the next time Git touches it

user@CS03432 MINGW64 ~/github/sample (master)
$ git add cpu_moniter.sh

user@CS03432 MINGW64 ~/github/sample (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   cpu_moniter.sh


user@CS03432 MINGW64 ~/github/sample (master)

git addで使えるオプション

1.◎git add .

現在のディレクトリ配下の全ての変更をaddします。
通常はこのオプションを指定して、ファイルをステージングエリアに移動します。

2.git add ファイル名

ファイル名を指定してaddします。
スペース区切りでファイル名を複数指定することもできます。
git add sample.sh sample.ba

3.git add *.拡張子

特定の形式のファイルだけaddする場合に使います。

git add *.sh

4.git add -A or git add –all

git管理下のディレクトリの、全ての「追加・変更・削除」がaddされます。git add –all も同じ意味です。

git add -A
または
git add --all

5.git add -n

ファイル追加のシミュレーションです。実際には追加されません。
大量に追加作業する事前確認に使用します。
git add . -n

user@CS03432 MINGW64 ~/github/sample (master)
$ ls
cpu_moniter.sh*

user@CS03432 MINGW64 ~/github/sample (master)
$ ls -al
total 8
drwxr-xr-x 1 user 197609   0 Dec 29 11:36 ./
drwxr-xr-x 1 user 197609   0 Dec 24 16:39 ../
drwxr-xr-x 1 user 197609   0 Dec 29 11:38 .git/
-rwxr-xr-x 1 user 197609 553 Dec 29 11:38 cpu_moniter.sh*

user@CS03432 MINGW64 ~/github/sample (master)
$ git commit -m "初めての変更"
[master (root-commit) cd87814] 初めての変更
 1 file changed, 19 insertions(+)
 create mode 100644 cpu_moniter.sh

user@CS03432 MINGW64 ~/github/sample (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   cpu_moniter.sh

no changes added to commit (use "git add" and/or "git commit -a")

user@CS03432 MINGW64 ~/github/sample (master)
$ git branch
* master

user@CS03432 MINGW64 ~/github/sample (master)
$ git branch shellscript

user@CS03432 MINGW64 ~/github/sample (master)
$ git branch
* master
  shellscript

user@CS03432 MINGW64 ~/github/sample (master)
$ git checkout  shellscript
Switched to branch 'shellscript'
M       cpu_moniter.sh

user@CS03432 MINGW64 ~/github/sample (shellscript)
$ git branch
  master
* shellscript

user@CS03432 MINGW64 ~/github/sample (shellscript)
$ git diff
diff --git a/cpu_moniter.sh b/cpu_moniter.sh
index 73e2ce7..3af96d7 100644
--- a/cpu_moniter.sh
+++ b/cpu_moniter.sh
@@ -7,7 +7,7 @@ OK=0
 WG=1
 NG=2
 D_NUM=`expr $NUM - 1`
-VMSTAT_IDOL=`vmstat 1 $NUM | awk 'NR>3 {sum+=$15} END {print sum}'`
+VMSTAT_IDOL=`vmstat 1 $NUM | awk 'NR>4 {sum+=$15} END {print sum}'`
 VMSTAT_IDOL=`expr $VMSTAT_IDOL / $D_NUM`
 CPU_RESOURCE=`expr 100 - $VMSTAT_IDOL`
 if [ $VMSTAT_IDOL -le $CRIT ]; then

user@CS03432 MINGW64 ~/github/sample (shellscript)
$ git log
commit cd87814e231347a49f2fb6bd9d25c19b692da0fe (HEAD -> shellscript, master)
Author: samy <tashiro.satomi@gmail.com>
Date:   Fri Dec 29 11:59:46 2023 +0900

    初めての変更

user@CS03432 MINGW64 ~/github/sample (shellscript)
$ git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

user@CS03432 MINGW64 ~/github/sample (shellscript)
$ git merge  master
Already up to date.

user@CS03432 MINGW64 ~/github/sample (shellscript)
$ git status
On branch shellscript
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   cpu_moniter.sh

no changes added to commit (use "git add" and/or "git commit -a")

user@CS03432 MINGW64 ~/github/sample (shellscript)
$ ls -al
total 8
drwxr-xr-x 1 user 197609   0 Dec 29 11:36 ./
drwxr-xr-x 1 user 197609   0 Dec 24 16:39 ../
drwxr-xr-x 1 user 197609   0 Dec 29 12:13 .git/
-rwxr-xr-x 1 user 197609 553 Dec 29 11:58 cpu_moniter.sh*

user@CS03432 MINGW64 ~/github/sample (shellscript)
$


③変更の確定:
ファイルの変更を確定(コミット)するためには、以下のコマンドを使用します。

git commit -m "コミットメッセージ"

例えば、`git commit -m "初めてのコミット"`と入力すると、`"初めてのコミット"`というメッセージとともに変更が確定されます。

④リモートリポジトリへのプッシュ:
ローカルのリポジトリをリモートリポジトリにプッシュするには、以下のコマンドを使用します。

git push リモート名 ブランチ名

リモート名は通常`origin`、ブランチ名は通常`master`です。例えば、`git push origin master`と入力すると、ローカルの変更がリモートの`master`ブランチにプッシュされます。

SourceTreeで登録した内容を確認してみました。


この記事が気に入ったらサポートをしてみませんか?