LoginSignup
8
15

More than 1 year has passed since last update.

ローカルでgitを使って構成管理をスタートする

Last updated at Posted at 2017-05-24

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などにあげたい場合は、以下を参照してください
8
15
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
8
15