
gitをローカル環境で使用して、ファイルのバージョン管理をする方法をまとめます。
記事の目次
使用環境
- Ubuntu 18.04.3 Desktop
- git version 2.17.1
gitのインストール
gitがインストールされていない場合には、aptコマンドで「git」をインストールします。
$ sudo apt install git $ git --version git version 2.17.1
gitの初期設定
gitコマンドを使用する際のアイデンティティ情報として、E-mailアドレスと指名を登録します。特定のリポジトリのみに設定する場合は、「--global」は指定しないようにします。
$ git config --global user.email "lab4ict@gmail.com" $ git config --global user.name "Lab4ict Webmaster"
ディレクトリの作成
リポジトリとして使用するディレクトリを作成します。
$ mkdir -p ~/Documents/git/sample/sample001 $ cd ~/Documents/git/sample/sample001
リポジトリの初期化
リポジトリを初期化します。「.git」ディレクトリが作成されます。
$ git init $ ls -a . .. .git
リポジトリが空の状態であることを確認します。
$ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track)
リポジトリにステージングの状態としてファイルを登録
リポジトリにファイルを作成し、gitで認識されるか確認します。
$ vi readme.txt $ git add On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) readme.txt nothing added to commit but untracked files present (use "git add" to track)
リポジトリにステージングの状態として登録します。この時点では、まだ正式版としてコミットされていません。
$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: readme.txt
ステージングの状態を取り消す場合は、「git reset」コマンドを使用します。
$ git reset
リポジトリにステージング状態として登録されたファイルをコミット
リポジトリに登録されたファイルをコミットします。コミットする際には、変更履歴として有用なコメントを指定しましょう。
$ git commit -m "First Version" [master (root-commit) 0b01d5c] First Version 1 file changed, 1 insertion(+) create mode 100644 readme.txt
リポジトリの状態を確認します。
$ git status On branch master nothing to commit, working tree clean
ファイル一覧を表示して確認してみます。
$ git ls-files readme.txt
ファイルの内容を変更してリポジトリにコミット
ファイルの内容を変更した場合は、新規と同じ手順でリポジトリにコミットします。
$ vi readme.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add readme.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
$ git commit -m "Second Commit: readme.txt"
[master 2e3d0a3] Second Commit: readme.txt
1 file changed, 1 insertion(+)
リポジトリへのコミットの履歴を確認
リポジトリへのコミットの履歴を確認します。
$ git log
commit 2e3d0a30cdc1c5d59d00c372b054b8e3bc976b5a (HEAD -> master)
Author: Lab4ict Sitemaster <lab4ict@gmail.com>
Date: Tue Jan 7 10:09:51 2020 +0900
Second Commit: readme.txt
commit 0b01d5c6308e08645189c46427a9dee07d6b6c73
Author: Lab4ict Sitemaster <lab4ict@gmail.com>
Date: Tue Jan 7 09:51:41 2020 +0900
First Version
ファイルをまとめてリポジトリに登録
「git add .」コマンドを使用すると同じディレクトリ内のファイルをまとめてリポジトリに登録することができます。
$ ls
sample1.txt sample2.txt
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
sample1.txt
sample2.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: sample1.txt
new file: sample2.txt
$ git commit -m "First Version"
[master (root-commit) 79dfc72] First Version
2 files changed, 2 insertions(+)
create mode 100644 sample1.txt
create mode 100644 sample2.txt
リポジトリに登録されたファイルのファイル名を変更する!
ファイル名の変更は、「git mv」コマンドで行うことができます。
$ git mv sample1.txt sampleX.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: sample1.txt -> sampleX.txt
$ git commit -m "Change Filename sample1.txt -> sampleX.txt"
[master 897e082] Change Filename sample1.txt -> sampleX.txt
1 file changed, 0 insertions(+), 0 deletions(-)
rename sample1.txt => sampleX.txt (100%)
リポジトリを以前のコミットの状態に戻す!
リポジトリを以前のコミットの状態に戻す方法です。履歴の情報からコミットのIDを確認して「git reset --hard」コマンドをを使用して戻します。
$ git log
commit 897e0821dc2517cd2c8df5bb19a10243e310b49f (HEAD -> master)
Author: Lab4ict Sitemaster <lab4ict@gmail.com>
Date: Tue Jan 7 11:42:30 2020 +0900
Change Filename sample1.txt -> sampleX.txt
commit 79dfc727025122d1712c7def61d4f55efc1fa967
Author: Lab4ict Sitemaster <lab4ict@gmail.com>
Date: Tue Jan 7 11:39:17 2020 +0900
First Version
$ git reset --hard 79dfc727025122d1712c7def61d4f55efc1fa967
HEAD is now at 79dfc72 First Version
$ git log
commit 79dfc727025122d1712c7def61d4f55efc1fa967 (HEAD -> master)
Author: Lab4ict Sitemaster <lab4ict@gmail.com>
Date: Tue Jan 7 11:39:17 2020 +0900
First Version
特定のファイルやフォルダを登録対象外にする!
「.git]と同じ階層に「.gitingore」というファイルを作成し、特定のファイル名やフォルダ名を記載をして、登録対象外にすることができます。「.gitignore」を作成した場合は、このファイル自体もコミットしておいたほうが有益なケースが多いです。以下は、「sample2.txt」を登録対象外にする例になります。
$ cat .gitignore sample2.txt
bash環境でのgitコマンドのサブコマンドの補完
bash環境では、「TAB」キーを押すことで、gitコマンドのサブコマンドの補完を行うことができます。積極的に活用しましょう!
おわりに
gitのリポジトリをローカル環境に作成して使用する方法をまとめました。
参考