普段はGitを使ってないのですが、急にGitを使うことになった時の為に、Gitの基本的な使い方を備忘録としてまとめておきます。
対象
- Git普段あまり使ってない
- リポジトリは作ったりしない。利用するだけ
プロジェクトでGitを利用する時の流れ
ブランチ使ってない時
- 参加プロジェクトの指定されたリモートリポジトリからクローンする
- ローカルでコード編集
- ローカルでコミット
- リモートリポジトリにプッシュ
コマンド
// 作業ディレクトリに移動
cd git-test
$ ls -la
drwxr-xr-x 4 luck staff 128 7 31 23:44 .
drwxr-xr-x 4 luck staff 128 7 31 23:31 ..
// リモートリポジトリからクローン
$ git clone https://<リポジトリアドレス>
Cloning into 'luck-gk-demo01'...
Username for 'https://<URL>': <e-mail>
Password for 'https://<URL>': <password>
warning: You appear to have cloned an empty repository.
$ ls -la
drwxr-xr-x 4 luck staff 128 7 31 23:44 .
drwxr-xr-x 4 luck staff 128 7 31 23:31 ..
drwxr-xr-x 3 luck staff 96 8 1 13:39 luck-gk-demo01
// 何か作業
// ローカルでコミット
$ git add .
$ git commit -m "test commit"
// リモートリポジトリにプッシュ
// git push <リモート名> <ローカルブランチ名>
// 現在のブランチを確認して
$ git branch
* master
// リモートにプッシュする
$ git push origin master
指定されたブランチを利用する時
- リモートリポジトリの指定されたブランチをクローンする
- ローカルでコード編集
- ローカルでコミット
- リモートリポジトリの指定ブランチにプッシュ
【管理側の操作】一旦マスターブランチにpush
// ブランチ確認
$ git branch
* master
// リモートブランチ確認
$ git remote -v
origin https://<リポジトリアドレス> (fetch)
origin https://<リポジトリアドレス> (push)
// リモートブランチにpush(git push origin master と同じ)
$ git push
Username for 'https://<URL>': <e-mail>
Password for 'https://<URL>': <password>
Counting objects: 3, done.
Writing objects: 100% (3/3), 235 bytes | 235.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://<git URL>
* [new branch] master -> master
【管理側の操作】ローカルでブランチ作成してpushしておく
// 現在のローカルブランチ確認
$ git branch
* master
// ローカルで新規ブランチを作って移動
$ git checkout -b development_luck
Switched to a new branch 'development_luck'
// 現在のブランチ確認
$ git branch
* development_luck
master
$ ls -la
total 8
drwxr-xr-x 4 luck staff 128 8 1 13:52 .
drwxr-xr-x 5 luck staff 160 8 1 13:39 ..
drwxr-xr-x 13 luck staff 416 8 1 14:32 .git
-rw-r--r-- 1 luck staff 16 8 1 13:52 index.md
$ vi index.md
$ git diff
diff --git a/index.md b/index.md
index e26eaf2..84564a7 100644
--- a/index.md
+++ b/index.md
@@ -1,2 +1,3 @@
-Git clone test
+#Git clone test
+リモートリポジトリの特定のブランチからクローンするテスト
// コミットする
$ git status
On branch development_luck
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: index.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git add .
$ git commit -m "git branch test"
[development_luck f7a2e29] git branch test
1 file changed, 2 insertions(+), 1 deletion(-)
// リモートにブランチが無い時はそのままだと失敗するので
$ git push
fatal: The current branch development_luck has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin development_luck
// 追加したブランチを指定してpushする
$ git push origin development_luck
Username for 'https://<URL>': <e-mail>
Password for 'https://<URL>': <password>
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 341 bytes | 341.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://<git URL>
* [new branch] development_luck -> development_luck
![スクリーンショット 2020-08-01 15.23.48.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F214523%2Fb90927bf-99e6-a8ff-8fad-1e8900b3ed2e.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=17ed3884f9aa9991087104b2d20a1b79)
【作業者の操作】リモートリポジトリの指定ブランチからクローンする
// ローカルにクローンするディレクトリを作成して移動
$ mkdir git-test01
$ cd git-test01
$ ls -la
total 0
drwxr-xr-x 2 luck staff 64 8 1 18:36 .
drwxr-xr-x 5 luck staff 160 8 1 18:36 ..
// リモートリポジトリの特定のブランチを指定してクローン
// git clone -b <ブランチ名> <リモートリポジトリURL>
$ git clone -b development_luck https://<git URL>
Cloning into 'luck-gk-demo01'...
Username for 'https://<URL>': <e-mail>
Password for 'https://<URL>': <password>
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
// 指定したブランチがクローンされていることを確認
$ ls -la
total 0
drwxr-xr-x 3 luck staff 96 8 1 18:38 .
drwxr-xr-x 5 luck staff 160 8 1 18:36 ..
drwxr-xr-x 4 luck staff 128 8 1 18:38 luck-gk-demo01
$ cd luck-gk-demo01/
$ ls -la
total 8
drwxr-xr-x 4 luck staff 128 8 1 18:38 .
drwxr-xr-x 3 luck staff 96 8 1 18:38 ..
drwxr-xr-x 13 luck staff 416 8 1 18:38 .git
-rw-r--r-- 1 luck staff 102 8 1 18:38 index.md
$ cat index.md
#Git clone test
リモートリポジトリの特定のブランチからクローンするテスト
// 現在のローカルブランチの確認
$ git branch
* development_luck
【作業者の操作】リモートリポジトリの指定ブランチにpushする
// 現在のローカルブランチ確認
$ git branch
* development_luck
// コードを修正してコミット
$ git status
On branch development_luck
Your branch is up to date with 'origin/development_luck'.
nothing to commit, working tree clean
$ ls -la
total 8
drwxr-xr-x 4 luck staff 128 8 1 18:38 .
drwxr-xr-x 3 luck staff 96 8 1 18:38 ..
drwxr-xr-x 13 luck staff 416 8 1 20:13 .git
-rw-r--r-- 1 luck staff 102 8 1 18:38 index.md
$ vi index.md
$ git diff
diff --git a/index.md b/index.md
index 84564a7..ddc63e7 100644
--- a/index.md
+++ b/index.md
@@ -1,3 +1,4 @@
#Git clone test
リモートリポジトリの特定のブランチからクローンするテスト
+リモートリポジトリの特定のブランチにpushするテスト
$ git add .
$ git commit -m "git push test"
[development_luck d0171d2] git push test
1 file changed, 1 insertion(+)
// リモートリポジトリの確認
$ git remote -v
origin https://<リポジトリアドレス> (fetch)
origin https://<リポジトリアドレス> (push)
// リモートリポジトリの指定ブランチにpush
$ git push origin development_luck
Username for 'https://<URL>': <e-mail>
Password for 'https://<URL>': <password>
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 347 bytes | 347.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://luck-gk.backlog.com/git/PROG/luck-gk-demo01.git
f7a2e29..d0171d2 development_luck -> development_luck
![スクリーンショット 2020-08-01 21.29.48.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F214523%2F20572d13-9291-df8e-a5dc-086e66b26172.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=57a75f0ad73312e994da2391534200dd)
リポジトリの変更を取り込む
git pull
または、
git fetch
git merge
共有リポジトリの変更の取り込み
git fetch
変更履歴(ログ)の確認
git branch
git log HEAD..FETCH_HEAD
変更内容の確認
git diff HEAD..FETCH_HEAD
変更内容をカレントブランチにマージ
git merge FETCH_HEAD
リモート リポジトリの追加
git remote add
git remote add リモート名 リモートURL
例)
git remote add origin https://<リポジトリアドレス>
空のリモートリポジトリにローカルのリポジトリをプッシュする
既存のローカルリポジトリをGitHubやCodeCommitなどに登録する際の手順。
$ git push -u origin master
origin・・・リモートリポジトリ名
master・・・ローカルリポジトリ名
AWS CodeCommit
CodeCommitに接続する場合は、秘密鍵と公開鍵を作成してそれをIAMに設定します。
https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_credentials_ssh-keys.html
利用する場合、例えばリポジトリをクローンする場合にはgit clone
のパラメーターにSSHの公開キーを付与します。
例えば、下記。
git clone ssh://Your-SSH-Key-ID@git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo my-demo-repo
Your-SSH-Key-ID
の部分は、IAMに登録した際に発行されるコードになります。
秘密鍵と公開鍵ファイルの作成
秘密鍵と公開鍵ファイルはssh-keygenを使うなどして作成します。
~/.sshにてssh-keygen
します。
IAMに公開鍵の登録
後は、AWSのIAMに公開鍵の方を登録します。