2021/7/31 わかりにくい部分を修正しました。
gitを使用して構成管理をスタートする手順です。
プロジェクトの構成管理を行なえるようになるまでについてまとめます。
前提
- gitがインストールされていること
- GUIのクライアントアプリは使用せず、CUIベースで説明をします
- 環境はMacです
手順
gitが使用できることを確認する
ご自身の使用しているコマンドラインツールを使用して、以下のコマンドを実行します。
$ git --version
git version 2.26.2
gitのバージョンが表示されればOKです。
gitが使用できる状態になっています。
ソースコードを格納するフォルダを準備する
好きな場所に、ソースコードを格納するフォルダを準備します。
今回は sample-code
フォルダの中にソースコードを置いていく想定で進めます。
$ mkdir sample-code
gitリポジトリを作成する
先程作成したsample-code
でリポジトリを作成します。
フォルダに移動し、git init
コマンドを実行します。
$ cd /sample-code
$ git init
Initialized empty Git repository in **********/sample-code/.git/
この状態でsample-code
フォルダを見てみると、.git
フォルダが作成されています。
$ ls -a
. .. .git
この.git
が含まれているフォルダとそれより下位にあるフォルダがgitで管理されるようになります(すなわち sample-code
フォルダ)。
ここまでで、構成管理をすることができるようになりました = sample-code
プロジェクトでGitを使うことができるようになりました。
今日は最初のコミットをしてみるところまでやってみます。
最初のコミットをする
git init
をした直後の状態でgit status
コマンドを打ってみます。
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
No commits yet
とあるように、コミットがまだないことがわかります。
コミットをしてみます。
新規にtest.txt
ファイルを作成します。
$ touch test.txt
$ echo 'hello git' > test.txt
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
git add
し、git commit
します。
$ git add test.txt
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt
$ git commit -m "first commit"
[master (root-commit) xxxx] first commit
1 file changed, 1 insertion(+)
create mode 100644 test.txt
$ git status
On branch master
nothing to commit, working tree clean
これで最初のコミットが完了です。
その他
- git initコマンドは、すでにファイルなどが存在するフォルダでも実行できます。
- もともとgitで構成管理をしていなかったフォルダ直下で実行することで、そのフォルダ以下をgitで管理できるようになります。
- ここで作成したコードをgithubなどにあげたい場合は、以下を参照してください