paloma blog

NWエンジニアやってます。主に自宅環境のお遊びを書きます。Pythonもちょっと。タイトルは好きなカクテルから。

gitで初めてbranchを切ってみた

自宅環境での話ですが、自分で使うツール類をgitでバージョン管理しています。
個人用かつ作るのも自分一人ということで各ツールずっとmasterのブランチで更新を行っていました。

特に問題もなかったのですが、この度ブランチ作りをやってみたので記録しておきます。

作成の理由

  • 本業の人に開発の仕方を聞いた

ちょっと前にプログラマの方と話す機会があったのですが、開発用のブランチを切ってそのあとプロダクト用のブランチにマージしているということです。

プロダクトに行く前に複数層のブランチを挟むそうです。
まあ仕事でやるならそうですよね。

私の本業はSI系のインフラなのでいまだにExcel方眼紙の設計書を日付ファイル管理ですw

gitの使い方のサイト・動画は山ほどありますが、ブランチとは何ぞやという情報が無かったので改めてチュートリアルを読みました。

Bitbucketのサイトがわかりやすかったです。

www.atlassian.com

ブランチを作る理由は以下のようです。

  1. ブランチは変更時のSnapshotへのポインタとなる
  2. ブランチは機能追加やバグ取り用に作る
  3. メインブランチのコード履歴を綺麗にする

ブランチは複数人の平行開発用というイメージがありましたが、2と3の理由だと1人の開発でも作るメリットはありそうですね。

新機能用のブランチ作成

5月くらいから着手しているスリーカードポーカーゲームですが、CLIでは何とか遊べるようになりました。
後はGUI化したいというタスクがあるので、こちらのリポジトリでブランチを作ってみましょう。

github.com

ちなみに開発環境はubuntu20.04LTSです。

  • ブランチ確認

現在のブランチ確認。

masashi@PC-ubuntu:~/Three-card-poker$ git branch
* master

1つしかないですね。

  • ブランチ作成

GUI機能ということでgraphicalという名のブランチを作ります。

masashi@PC-ubuntu:~/Three-card-poker$ git branch graphical
  • ブランチ移動

作成したブランチに移動します。
branchは作るだけで、移動はcheckoutコマンドです。

masashi@PC-ubuntu:~/Three-card-poker$ git checkout graphical 
Switched to branch 'graphical'

確認。

masashi@PC-ubuntu:~/Three-card-poker$ git branch
* graphical
  master

OKですね。
移動できました。

ブランチでファイル作成

GUI用のファイルを作成します。
GUIでの処理の仕方がさっぱりなので枠だけです。
ラシャの感じは出せてるかなw

f:id:paloma69:20201017141739p:plain

このファイルをコミットしてみます。

masashi@PC-ubuntu:~/Three-card-poker$ git status
ブランチ graphical
追跡されていないファイル:
  (use "git add <file>..." to include in what will be committed)
    game.py

nothing added to commit but untracked files present (use "git add" to track)
masashi@PC-ubuntu:~/Three-card-poker$ git add game.py 
masashi@PC-ubuntu:~/Three-card-poker$ git commit -m "Create GUI file"
[graphical 583a058] Create GUI file
 1 file changed, 19 insertions(+)
 create mode 100644 game.py

masterブランチに影響ないか確認

他のブランチに影響がないか確認してみます。

masashi@PC-ubuntu:~/Three-card-poker$ ls
README.md  __pycache__  game.py  pokerapp.py  simulater.py  test_pokerapp.py

game.pyがGUI用のファイルです。

ブランチをmasterに変更。

masashi@PC-ubuntu:~/Three-card-poker$ git checkout master 
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

再度lsすると

masashi@PC-ubuntu:~/Three-card-poker$ ls
README.md  __pycache__  pokerapp.py  simulater.py  test_pokerapp.py

game.pyファイルが無いですね。
OKです。

これで画面系のコードをメインブランチに影響を与えることなく作成し放題です。
画面処理の作り方が全然わからないのですが、じっくり進めていこうと思います。

おまけ pygame再インストール

画面処理はpygameを使おうと思っていますが、以前20.04LTSにアップデートした後aptのautoremoveを実行したのですが、
必要なライブラリを削除してしまった様で仮想環境のpygameが動かなくなってしまいました。
再インストールしようにも、

(pygames) masashi@PC-ubuntu:~$ pip install pygame
Collecting pygame
  Using cached pygame-1.9.6.tar.gz (3.2 MB)
    ERROR: Command errored out with exit status 1:
     command: /home/masashi/pygames/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kwypge18/pygame/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kwypge18/pygame/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-edtsy3uy
         cwd: /tmp/pip-install-kwypge18/pygame/
    Complete output (29 lines):

...    

    Hunting dependencies...
    WARNING: "sdl-config" failed!
    WARNING: "pkg-config freetype2" failed!
    WARNING: "freetype-config" failed!
    Unable to run "sdl-config". Please make sure a development version of SDL is installed.
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

このように失敗してしまうのですが、
このコマンドで必要なライブラリをインストールしてpygameも再インストールできました。

(pygames) masashi@PC-ubuntu:~$ sudo apt-get build-dep python-pygame
(長いので割愛)
(pygames) masashi@PC-ubuntu:~$ pip install pygame
Collecting pygame
  Using cached pygame-1.9.6.tar.gz (3.2 MB)
Using legacy 'setup.py install' for pygame, since package 'wheel' is not installed.
Installing collected packages: pygame
    Running setup.py install for pygame ... done
Successfully installed pygame-1.9.6

python - Installing pygame with pip - Ask Ubuntu