Gitの使い方(1 基本操作)
| 登録日 | :2024/01/21 11:56 |
|---|---|
| カテゴリ | :Git |
1 Gitの初期化
$ git init
.gitディレクトリが作成される
$ ls .git
HEAD description info refs
config hooks objects
- objects:ローカルリポジトリの本体(圧縮ファイル、ツリーファイル、コミットファイル)
- config:設定ファイル
*index: gid addすると追加される
2. 他人のリポジトリをコピーする
git clone <リポジトリ>
他人の開発プロジェクトに参加するために、他人のリポジトリをローカルにコピーする。
$ git clone https://github.com/atom/atom.git
Cloning into 'atom'...
remote: Enumerating objects: 204170, done.
remote: Counting objects: 100% (85/85), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 204170 (delta 64), reused 62 (delta 62), pack-reused 204085
Receiving objects: 100% (204170/204170), 322.98 MiB | 1.69 MiB/s, done.
Resolving deltas: 100% (144746/144746), done.
3. ステージに追加する
git add .
4. 変更を記録する
ステージからローカルリポジトリにスナップショットを記録する
git commit
gitで登録しているエディタが起動する。
$ git commit
[master (root-commit) 985824c] initial commit
1 file changed, 1 insertion(+)
create mode 100644 index.html
その他
git commit -m"メッセージ"
エディタが起動するのが不要の場合は、-mオプションでコメント記入可能。1行で完結に書くようにする。
チーム開発やオープンソースの場合は、エディタで下記フォーマットで詳細を書いた方がいい。
- 変更の目的
- 変更内容
git commit -v : ファイルの変更内容を確認することができる
5. 現在の変更状況を確認する
git status
- 「ワークツリー」と「ステージ」で変更されたこと
- 「ステージ」と「ローカルリポジトリ」で変更されたこと
を表示する
$ git status
On branch master
nothing to commit, working tree clean
変更を加えて、再度git statusを見ると、modifyが表示される。stageとリポジトリとの差分があることが示唆される
$ vi index.html
$ 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: index.html
no changes added to commit (use "git add" and/or "git commit -a")
6. 変更差分を確認する
git addする前の変更分を確認する
$ git diff
$ gid diff <file name>
実行例
$ git diff
diff --git a/index.html b/index.html
index a66864d..47028db 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
<h1>Gitチュートリアル</h1>
<p>git status</p>
+<p>git diff</p>
ステージ(git add した後)の変更差分を確認する場合は--stagedをつける
$ git diff --stated
git addすると、git diffは何も表示されない
$ git add index.html
$ git diff
git diff --stagedを確認する
$ git diff --staged
diff --git a/index.html b/index.html
index a66864d..47028db 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
<h1>Gitチュートリアル</h1>
<p>git status</p>
+<p>git diff</p>
コミットすれば、stageとの差分もなくなることも確認する
$ git commit -m"git diffを追記"
[master 32344c8] git diffを追記
1 file changed, 1 insertion(+)
$ git diff
$ git diff --staged
変更履歴を確認するコマンド
$ git log
1行で確認する
$ git log --oneline
ファイルの変更差分を表示する
$ git log -p index.html
表示するコミット数を制限する
& git log -n <コミット数>
7. ファイルの削除を記録する
git addとは別のコマンドを用いる
- gitのローカルリポジトリとファイルを一括して削除したい
$ git rm <file name>
$ git rm -r <dir name>
- gitのローカルリポジトリからだけ削除する(ワークツリーのファイルは残す)
$ git rm -cached <file name>
git rm操作例
$ git status
On branch master
nothing to commit, working tree clean
$ git rm index.html
rm 'index.html'
$ ls
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: index.html
削除したものを戻す場合
$ git reset HEAD <file name>
$ git checkout index.html
実行例
$ git reset HEAD index.html
Unstaged changes after reset:
D index.html
$ git checkout index.html
Updated 1 path from the index
$ ls
index.html
$ git status
On branch master
nothing to commit, working tree clean
--- cacheの削除例
$ git rm --cached index.html
rm 'index.html'
$ ls
index.html
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
削除操作を取り消す場合
$ git reset HEAD index.html
The most similar command is
status
$ git status
On branch master
nothing to commit, working tree clean
$ ls
index.html
4. ファイルの移動を記録する
$ git mv <old file name> <new file name>
これは以下の操作を一括して行うことに相当する
$ mv <old file name> <new file name>
$ git rm <old file name>
$ git add <new file name>
実行例
--- 名前を変更する
$ git mv index.html index2.html
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: index.html -> index2.html
--- 変更を戻す
$ git mv index2.html index.html
$ git status
On branch master
nothing to commit, working tree clean