Git for Windowsでの日常

gitの使い方 Git Bash編

少し前ですが、Git for Windowsのインストールをしましたが、IDEを使わない形でのGit運用編の記事を書きます。インストール編は、

でした。付加的な設定は、

でした。特に大事なのは、globalな.gitignoreです。他にWindows側のエクスプローラーの設定では、拡張子の表示と隠しフォルダー、ファイルの表示がオンになってる方が良いです。では非IDE環境での開発サイクルを開始してみましょうか?まずはフォルダーを作る。名前を適切に付ける。“bash for development cycle”でもおけです。そのフォルダーの内部で、右クリックつまり、

ここで、“Open Git Bash here”を選択します。

K&Rの最初のプログラムをhello.cとして作ります。短いので、そらで入れますが、前の記事で書いたのには少しだけ誤りがありました。ただしいというか原文通りのhello.cは、

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle
$ vi hello.c
下記を入力
jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle
$ cat hello.c
main()
{
        printf("hello, world\n");
}

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle
$

でした。コンパイルしてみましょう。コンパイルはBorland C Compilerを使いますが、今は32bit版で良いので、

$ bcc32 hello.c
Embarcadero C++ 7.70 for Win32 Copyright (c) 1993-2017 Embarcadero Technologies, Inc.
hello.c:
警告 W8065 hello.c 3: プロトタイプ宣言のない関数 'printf' の呼び出し (関数 main )
警告 W8070 hello.c 4: 関数は値を返すべき (関数 main )
Turbo Incremental Link 6.99 Copyright (c) 1997-2024 Embarcadero Technologies, Inc.

さすがにK&R Cの時代からは、少しだけ進歩してるので、警告が二つ出ましたが、コンパイルは完了したようです。実行してみましょうか?

$ ls -alt
total 326
drwxr-xr-x 1 jakeb 197609      0  5月 16 14:46 ./
-rw-r--r-- 1 jakeb 197609 196608  5月 16 14:46 hello.tds
-rwxr-xr-x 1 jakeb 197609  64512  5月 16 14:46 hello.exe*
-rw-r--r-- 1 jakeb 197609    252  5月 16 14:46 hello.obj
-rw-r--r-- 1 jakeb 197609     38  5月 16 14:44 hello.c
drwxr-xr-x 1 jakeb 197609      0  5月 16 14:39 ../

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle
$ ./hello
hello, world

動いたようですね。gitを使いましょう。使い始めるには、まず“git init”とコマンド入れます。

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle
$ git init
Initialized empty Git repository in C:/Users/jakeb/OneDrive/Desktop/bash for development cycle/.git/

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$

bashのプロンプトの末尾が(main)になっているのに注意しましょう。(master)でなくてなによりです。今の時点で、

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ ls -alt
total 330
drwxr-xr-x 1 jakeb 197609      0  5月 16 14:50 .git/
drwxr-xr-x 1 jakeb 197609      0  5月 16 14:50 ./
-rw-r--r-- 1 jakeb 197609 196608  5月 16 14:46 hello.tds
-rwxr-xr-x 1 jakeb 197609  64512  5月 16 14:46 hello.exe*
-rw-r--r-- 1 jakeb 197609    252  5月 16 14:46 hello.obj
-rw-r--r-- 1 jakeb 197609     38  5月 16 14:44 hello.c
drwxr-xr-x 1 jakeb 197609      0  5月 16 14:39 ../

となっていて、“.git”という名前のフォルダーができています。git関係の情報はここに集約されます。なので、このフォルダーを削除すると“git init”しなかったことになります。ここで“git status”とコマンドを入れましょう。

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ git status
On branch main

No commits yet

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

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

なんにもtrackしてないと言われました。確かに何もしていません。”git init”しただけでした。上のアドバイスに従って、“git add”しましょう。具体的には、“git add .”でおけです。.gitignoreが効いてるので、余計なものはaddされません。

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ git add .
warning: in the working copy of 'hello.c', LF will be replaced by CRLF the next time Git touches it

なにやら改行コードで文句言われましたが、本質的は問題ないのでスルー。ここでgit statusすると、

$ git status
On branch main

No commits yet

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

commit しておきましょうかね。最初のバージョンということでね。“git commit -m “K&R C hello world”とでも入れましょうか?

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ git commit -m "K&R C hello world"
[main (root-commit) d81f313] K&R C hello world
 1 file changed, 4 insertions(+)
 create mode 100644 hello.c

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ git status
On branch main
nothing to commit, working tree clean

このstatus nothing to commit, working tree clean が大事です。

さて、hello.cは動いてはいますけど、警告が出たりしているので一応ケアしましょうかね?プロトタイプ宣言云々は、printf()がらみなので、多分#include <stdio.h>を加えればおけ。main関数は値を返すべきとか言われましたけど、そうだっけ?おそらくコンパイラオプションでこのワーニングを抑制できると思いますが、調べるのが面倒なので、値を返すか?main()の戻り値をvoidにしましょうかね?って、K&R Cの時代にvoidってあったっけ?bcc32ではあるかもしれませんけどね。

$ cat hello.c

#include <stdio.h>

void main()
{
        printf("hello, world\n");
}

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ bcc32 hello.c
Embarcadero C++ 7.70 for Win32 Copyright (c) 1993-2017 Embarcadero Technologies, Inc.
hello.c:
Turbo Incremental Link 6.99 Copyright (c) 1997-2024 Embarcadero Technologies, Inc.

bcc32様が黙りました。時代考証的にはです。修正したので、commitしておきましょうか。

git commit -m “handle two warnings including stdio.h and void main()”とかしますか?

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ git add hello.c
warning: in the working copy of 'hello.c', LF will be replaced by CRLF the next time Git touches it

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.c


jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ git commit -m "handle two warnings including stdio.h and void main()"
[main da13daa] handle two warnings including stdio.h and void main()
 1 file changed, 4 insertions(+), 1 deletion(-)

このようにファイルを修正して、addしてcommitしていきますが、これが開発サイクルないしループで、結果が望ましいものになるまで続きます。プログラムの機能がある程度の段階になってきたら、リモートレポジトリへpushすることを考えたいですが、それは次回。ここまででも二つのcommitがあり、その経緯を示すログはgit logで得られます。

jakeb@Orbit-11 MINGW64 ~/OneDrive/Desktop/bash for development cycle (main)
$ git log
commit da13daa6d9ed5b8545bc56b96041ac941b018d70 (HEAD -> main)
Author: nao <jake.burst@gmail.com>
Date:   Sun May 18 14:09:09 2025 +0900

    handle two warnings including stdio.h and void main()

commit d81f31358e5a8099d8aece21f4efe7c295b2cfb8
Author: nao <jake.burst@gmail.com>
Date:   Sun May 18 13:53:54 2025 +0900

    K&R C hello world

以下次号。Comments welcome.

コメント