More Related Content Similar to ソフトウェア工学2023 07 バージョン管理 Similar to ソフトウェア工学2023 07 バージョン管理 (20) More from Toru Tamaki (20) ソフトウェア工学2023 07 バージョン管理12. $ docker compose exec myubuntu diff -u main.py main_new.py
--- main.py 2022-04-12 02:26:14.433331280 +0000
+++ main_new.py 2022-04-12 02:26:14.433474365 +0000
@@ -5,14 +5,12 @@
return z
-def myfunc_2(a):
- for i in range(5):
- myfunc(a[i], a[i + 1])
-
def myfunc_3(a):
for i in range(5):
myfunc(a[i], a[i + 1])
+def myfunc_4(a):
+ z = a[1] + a[2]
def main():
a = [1, 2, 3, 4, 5]
$
diffの例
ファイルBで追加
ファイルAから削除
17. patch (win)
nWindowsの場合
• PowerShellは標準入力に「<」を
使えないので,catでdiffファイル
を表示して,それをパイプ「|」
でコンテナに渡します
• (ちなみにこれはmacでも動作
します)
docker-composeで標
準出力を受け付けるオ
プション
PS C:¥...¥08_02_patch> cat main.py.diff | docker compose exec -T myubuntu patch
(Stripping trailing CRs from patch; use --binary to disable.)
patching file main.py
PS C:¥...¥08_02_patch>
• Git for Windowsをインストールしてgit cloneした場合の
注意点:
• 最初からインストール時に改行コードを「checkout
as-is, commit Unix-style line endings」にしておく
• もしくはインストール後に以下を実行し,git clone
しなおす
• git config --global core.autocrlf input
パイプで標準入力からdiff
ファイルを入力
18. バージョン管理専用のツール
nConcurrent Versions System (csv) https://www.nongnu.org/cvs/
• 90年代に使われたが現代では廃れた
nsubversion (SVN) http://subversion.apache.org
• 中央管理型.クライアントサーバ
• ある人が編集するとロックがかかり,他の人が編集できない
ngit:近年の業界標準 https://git-scm.com
• 分散管理型
• 複数のリポジトリが別々に管理される
• レクチャーではgitを紹介
nMercurial https://www.mercurial-scm.org
• 分散型
• gitと並んでよく使われてる
26. Visual Studio Code (vscode)
n統合開発環境
• 各種拡張機能あり
• Dockerコンテナ内
での開発も可能
• ターミナルも完備
nその他のエディタ
• vi / vim
• Emacs
• Eclips
• Sublime Text
https://azure.microsoft.com/ja-jp/products/visual-studio-code/
46. docker composeを利用する
起動する
$ cd 08_03_git
$ docker compose build
$ docker compose up -d
確認する
$ docker compose ps
停止する
$ docker compose down
これ以降はzipファイルダウンロードではなく,
gitコマンド(git clone)でコードを入手します
$ git clone https://github.com/se-nitech/08_03_git.git
48. ローカルリポジトリによるバージョン管理
nローカルリポジトリを利用
• 履歴を保持
• 変更,修正,追加,削除など
ngitコマンド
• リポジトリを管理する
変更
修正 ローカル
リポジトリ
コンテナで実行する場合: ホストで実行する場合:
macos
win
$ docker compose exec myubuntu git --version
git version 2.25.1
$
$ git --version
git version 2.32.1 (Apple Git-133)
以降ではホストで実行
する手順を説明します
コンテナで実行する場
合の注意点はスライド
の最後で説明 PS C:¥...¥> git --version
git version 2.36.1.windows.1
49. gitにおける変更の流れ
git add git commit
ステージング コミット
作業ツリー
(ワークツリー)
インデックス
(ステージングエリア)
ローカル
リポジトリ
変更
修正
コーディングや
デバッグを行う
コミット用の
一時保管場所
コミットが
保存される
1つのコミットが
1つの「バージョン」
55. 設定ファイル:.gitconfig
n必須の設定
• 氏名
• メールアドレス
n場所
• linux, macos:ホームディレクトリ直下
• ~/.gitconfig
• windows:「ユーザー名」は自分のユーザー名
• C:¥ユーザー¥ユーザー名¥.gitconfig
n 設定のコマンド
これは自分のPCのユーザー名です
• 本名,英語
• Firstnameが先,Familynameが後
• 先頭だけ大文字
• すべて半角
• 間に半角の空白
[user]
name = Toru Tamaki
email = tamaki.toru@nitech.ac.jp
$ git config --global user.name "Toru Tamaki"
$ git config --global user.email tamaki.toru@nitech.ac.jp
56. ローカルなgitconfig
nリポジトリ固有の設定
• ローカルなgitconfigに記述
• ローカルはグローバルに優先する
n場所
• 現在のディレクトリ直下
• ./.git/config
n 設定のコマンド例:
$ git config user.name "Toru TAMAKI (local)"
$ git config user.email tamaki.toru@nitech.ac.jp.local
(base) $ ls -la
total 8
drwxr-xr-x 4 tamaki staff 128 4 17 18:32 .
drwx------ 1050 tamaki staff 33600 4 17 18:31 ..
drwxr-xr-x 12 tamaki staff 384 4 17 18:33 .git
(base) $ ls -la .git
total 40
drwxr-xr-x 12 tamaki staff 384 4 17 18:33 .
drwxr-xr-x 4 tamaki staff 128 4 17 18:32 ..
-rw-r--r-- 1 tamaki staff 7 4 17 18:33 COMMIT_EDITMSG
-rw-r--r-- 1 tamaki staff 21 4 17 18:31 HEAD
-rw-r--r-- 1 tamaki staff 212 4 17 18:33 config
-rw-r--r-- 1 tamaki staff 73 4 17 18:31 description
drwxr-xr-x 15 tamaki staff 480 4 17 18:31 hooks
-rw-r--r-- 1 tamaki staff 145 4 17 18:33 index
drwxr-xr-x 3 tamaki staff 96 4 17 18:31 info
drwxr-xr-x 4 tamaki staff 128 4 17 18:32 logs
drwxr-xr-x 10 tamaki staff 320 4 17 18:33 objects
drwxr-xr-x 4 tamaki staff 128 4 17 18:31 refs
普通は前ページのグローバルな設定を使います
58. git checkout:リポジトリのコピー
n既存のリポジトリをコピーする
• 特にリモートリポジトリから
リポジトリをコピーし
てくるコマンド リモートリポジトリのURLなど
これが便利!
ネット時代に大活躍!
https://github.com/missing-semester/missing-semester
$ git clone https://github.com/missing-semester/missing-semester.git
Cloning into 'missing-semester'...
remote: Enumerating objects: 2101, done.
remote: Counting objects: 100% (561/561), done.
remote: Compressing objects: 100% (90/90), done.
remote: Total 2101 (delta 484), reused 473 (delta 470), pack-reused 1540
Receiving objects: 100% (2101/2101), 15.52 MiB | 10.32 MiB/s, done.
Resolving deltas: 100% (1260/1260), done.
$
61. 補足:このレクチャーのコード
nGitHubにあります
• cloneしてください
• GitHubについては別の回で説明します https://github.com/se-nitech
$ git clone https://github.com/se-nitech/08_03_git.git
Cloning into '08_03_git'...
remote: Enumerating objects: 31, done.
remote: Counting objects: 100% (31/31), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 31 (delta 10), reused 28 (delta 7), pack-reused 0
Receiving objects: 100% (31/31), done.
Resolving deltas: 100% (10/10), done.
$ cd 08_03_git
$
リポジトリをコピーし
てくるコマンド リモートリポジトリのURLなど
62. git add, commit:ステージングとコミット
このコミット
の統計情報
ステージングす
るコマンド ステージングする
ファイル名
コミットする
コマンド
-m:コミットに付けるコメント
(なぜ・何を変更したのかが後で分
かるように書くことが重要!)
コミットする
ファイル名
まずmain.pyにな
にか修正を加えて
ください.
$ git add main.py
$ git commit -m "change equation" main.py
[main 53e5c93] change equation
1 file changed, 1 insertion(+)
$
63. git log:コミット履歴
これまでのコミットの
ログを表示
コミットのコ
メント
コミットのID
(ハッシュ値)
1つのコミット
$ git log
commit 53e5c9376984700c265a561023578b738fec22c6 (HEAD -> main)
Author: Toru Tamaki <tamaki.toru@nitech.ac.jp>
Date: Wed Jun 1 16:13:17 2022 +0900
change equation
commit b0b1924365f2ebf6f7871779ace0853c9ed9d3f7 (origin/main, origin/HEAD)
Author: Toru Tamaki <tamaki.toru@nitech.ac.jp>
Date: Thu May 26 15:30:25 2022 +0900
fix workdir pagerの設定がlessになっている場合,
lessを終了するなら"q"
65. git diff:コミット間差分
差分を見るコマンド 差分を取る2つのコミットID
コミットIDを指定しないと,
作業ツリーとインデックスの
差分を表示
$ git diff eb88fd18 8b4c7528e
diff --git a/main.py b/main.py
index 1790121..6a41df2 100644
--- a/main.py
+++ b/main.py
@@ -15,7 +15,7 @@ def myfunc_4(a):
def main():
a = [1, 2, 3, 4, 5]
for _ in range(100):
- myfunc_2(a)
+ myfunc_4(a)
for _ in range(300):
myfunc_3(a)
$ git diff
diff --git a/main.py b/main.py
index 3e3af03..2f991d6 100644
--- a/main.py
+++ b/main.py
@@ -1,7 +1,7 @@
def myfunc(x, y):
- z = 2 * x - y + 1
+ z = 2 * x - y + 2
return z
66. git mv, git rm:ファイルの操作
nファイルの操作はgitコマンドを使う
• それまでの履歴が失われてしまう
• 通常のmv, rmは使わずgit rm, git mvを使おう
$ mv main.py main2.py
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working
directory)
deleted: main.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
main2.py
no changes added to commit (use "git add" and/or "git commit -a")
$ git mv main.py main2.py
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: main.py -> main2.py
通常のmv:git履歴が失われる git mv:git履歴が残る!
削除されている
新しいファイル
ができている
rename扱い!
67. git mv, git rm:ファイルの操作
nファイルの操作はgitコマンドを使う
• それまでの履歴が失われてしまう
• 通常のmv, rmは使わずgit rm, git mvを使おう
通常のrm:単に削除 git rm:削除してadd済み
$ rm main.py
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be
committed)
(use "git restore <file>..." to discard changes in
working directory)
deleted: main.py
$ git rm main.py
rm 'main.py'
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: main.py
削除されている
削除されてadd済みな
のでcommit可能
69. バージョン管理する対象
n対象:テキストファイル
• コード
• *.py, *.cpp
• 設定ファイル
• dockerfile
• docker-compose.yml
• ドキュメント
• README.md
n対象外
• バイナリファイル,データ
• 実行形式ファイル
• 画像ファイル
• 音声ファイル
• etc
• 管理する必要のないファイル
• .vscodeなどエディタ設定
• 中間ファイル
• __pycache__
• *.pyc
• 一時ファイル
• .coverageなど(次回)
72. コミット:コードの更新
nバージョンをグラフで表現する
• コミットするたびにノードが移動
• 主幹ブランチの名前
• mainブランチ
• (古いリポジトリはmasterブラン
チの場合もある)
• HEAD
• 先頭(最新バージョン)
初期コミット
コミット1
コミット2
HEAD
初期コミット
コミット1
HEAD
初期コミット
HEAD
現在はmainが主流.masterは使わないようにする.
・現在のブランチ名を変更するには
git branch -M main
・今後すべての新規のデフォルトを変更するなら
git config --global init.defaultBranch main
• Twitter、コードやドキュメント内の用語「Whitelist /
Blacklist」 「Master / Slave」 「Dummy value」などを
好ましい用語へ置き換え、具体例も発表, 2020/7/6
Publickey
• GitHub、これから作成するリポジトリのデフォルトブラ
ンチ名が「main」に。「master」から「main」へ変更,
2020/10/15, Publickey
75. ブランチの作成と切り替え
HEAD main bugfix
git branch ブランチ名
新規ブランチを作成
まだmainのまま
$ git branch
* main
$ git branch bugfix
$ git branch
bugfix
* main
$ git checkout bugfix
Switched to branch 'bugfix'
$ git branch
* bugfix
main
$
76. ブランチの作成と切り替え
HEAD
main bugfix
git checkout ブランチ名
ブランチを切り替え
bugfixブランチに
切り替わった
$ git branch
* main
$ git branch bugfix
$ git branch
bugfix
* main
$ git checkout bugfix
Switched to branch 'bugfix'
$ git branch
* bugfix
main
$
85. .gitconfigの扱い
nホストの場合
• linux, macos:ホームディレクトリ直下
• ~/.gitconfig
• windows:「ユーザー名」は自分のユーザー名
• C:¥ユーザー¥ユーザー名¥.gitconfig
nコンテナの場合
• コンテナ内に.gitconfigが必要
• ユーザーがrootなので/root/.gitconfigが必要
• 方法は2つ
• コンテナ内で作成:毎回作成するので面倒
• ホストの.gitconfigをコピー:ちょっと手間,でも
• ホストとコンテナで同じものが使えるので便利
• VScodeのターミナルを利用するのが楽
86. .gitconfigをホストからコンテナへコピー:1
ndocker cpコマンド
• docker cp ホストファイル名 コンテナ名:コピー先
• docker compose cp ホストファイル名 サービス名:コピー先
コピーしないとrootユーザー
の.gitconfigは存在しない
いちいち手動でコピー
$ docker compose up -d
[+] Running 1/0
✔ Container 08_03_git-myubuntu-1 Run...
$ docker compose exec myubuntu ls -la /root
total 16
drwx------ 2 root root 4096 Apr 12 02:06 .
drwxr-xr-x 1 root root 4096 May 2 00:24 ..
-rw-r--r-- 1 root root 3106 Dec 5 2019 .bashrc
-rw-r--r-- 1 root root 161 Dec 5 2019 .profile
$
87. .gitconfigをホストからコンテナへコピー:1
ndocker cpコマンド
• docker cp ホストファイル名 コンテナ名:コピー先
• docker compose cp ホストファイル名 サービス名:コピー先
rootユーザーの.gitconfigとし
てコピーされた.これが使
われる
いちいち手動でコピー
$ docker compose up -d
[+] Running 1/0
✔ Container 08_03_git-myubuntu-1 Run...
$ docker cp ~/.gitconfig 08_03_git-myubuntu-1:/root/
$ docker compose exec myubuntu ls -la /root
total 16
drwx------ 2 root root 4096 Apr 12 02:06 .
drwxr-xr-x 1 root root 4096 May 2 00:24 ..
-rw-r--r-- 1 root root 3106 Dec 5 2019 .bashrc
-rw-r--r-- 1 501 dialout 90 Apr 17 09:33 .gitconfig
-rw-r--r-- 1 root root 161 Dec 5 2019 .profile
$
mac/linux
88. PS C:¥...¥08_03_git> docker compose up -d
[+] Running 2/2
✔ Network 08_03_git_default Created
✔ Container 08_03_git-myubuntu-1 Started
PS C:¥...¥08_03_git> docker cp C:¥Users¥*****¥.gitconfig 08_03_git-myubuntu-1:/root/
Successfully copied 2.05kB to 08_03_git-myubuntu-1:/root/
PS C:¥...¥08_03_git> docker compose exec myubuntu ls -la /root
total 20
drwx------ 1 root root 4096 May 2 00:39 .
drwxr-xr-x 1 root root 4096 May 2 00:39 ..
-rw-r--r-- 1 root root 3106 Dec 5 2019 .bashrc
-rwxr-xr-x 1 root root 104 Feb 14 07:23 .gitconfig
-rw-r--r-- 1 root root 161 Dec 5 2019 .profile
PS C:¥...¥08_03_git>
.gitconfigをホストからコンテナへコピー:1
ndocker cpコマンド
• docker cp ホストファイル名 コンテナ名:コピー先
• docker compose cp ホストファイル名 サービス名:コピー先
rootユーザーの.gitconfigとし
てコピーされた.これが使
われる
いちいち手動でコピー
win