アプリとサービスのすすめ

アプリやIT系のサービスを中心に書いていきます。たまに副業やビジネス関係の情報なども気ままにつづります

GitでPR(pull request)を投げるまでと、チームでgitでのコード管理ログ

gitコマンドでbranchを作成してPR(Pull Request)を投げるなど、チームでコード管理したのでその備忘録。


git cloneしてPRを投げるまでの branchの流れ
f:id:trafalbad:20200107205057j:plain





目次
1. PRを投げるまで
2.PR作成作業
3.キレーなコードの書き方

PRを投げるまで

# コンソールのコマンドいじるファイル操作
$ source ~/.bashrc

macOSならこのファイルにいろいろ書き込むことでコンソールのコマンドを自分好みにカスタマイズできる。

# まずgitでコードをクローンする
git clone [URL] && cd [dirctory]

# 新しいブランチ作成(必ず[origin/master]を入れる)
$ git checkout -b refactorer origin/master
(git checkout -b [new branch] [current branch])

>>>
Branch 'refactorer' set up to track remote branch 'master' from 'origin'.
Switched to a new branch 'refactorer'

変更が反映された。




# 今いるbranchを確認
$ git branch
>>>
   master
* refactorer


# ステージング(git commitするときのファイルのリスト)の変更 →index.htmlを変更。commmit すると変更される

$ git add index.html



# git addでの変更内容の確認
$ git status

>>>
On branch refactoring_hagi
Your branch is up to date with 'origin/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:  index.html

no changes added to commit (use "git add" and/or "git commit -a")


# コミット
$ git commit -m “コメント”

>>>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

git config --global --edit

After doing this, you may fix the identity used for this commit with:

git commit --amend ―reset-author

1 files changed, 183 insertions(+), 184 deletions(-)
rewrite index.html(88%)


# originにrefactorerをpushする
$ git push origin refactorer




新しいbranchができたので、PRを作成
f:id:trafalbad:20200107205151p:plain




※※※※※branch 削除用コマンド

git branch -D [ブランチ名]




PR作成作業


GUIでbranchを変更する場所



f:id:trafalbad:20200107205225p:plain

「default」がついてる branchがgit cloneしてきたときの branchになる。



Pull Request(PR)出すページ



f:id:trafalbad:20200107205249j:plain


・「Able to merge」が出てれば、即mergeできる

・PRの対象者は「Reviewers」から選択・変更できる





Reviewersを変更


f:id:trafalbad:20200107205313p:plain


ちなみに

・ファイルごとにPR投げる

ファイルごとに branchを作る必要あり


PRは、ファイルの変更ごとに投げたり、全体の変更を一括で変更するため投げたりできる。







gitでコード管理するときのserverとlocalとのおとまかな関係図
f:id:trafalbad:20200107205341j:plain


.gitはローカルにある隠しファイル的な存在で、表には出てこない。


・fetchコマンドはローカルにある".git"を変更

・mergeコマンドはPR後に行うやつで、serveも".git"の変更を反映する




branchを作ることでチームでのコード管理が簡単にできた。







キレーなコードの書き方

・関数は動詞+名
→何がされて何が返ってくるかを名前を反映させる


・変数名の付け方は「be 動詞 = 主語」
→ 例 is_completed_lv = 処理(主語)


・同じ動作が多い場合は減らす


・Ifの多さ
ネストが3は深いので4以降NG。関数化すればネストはなくなる


・1箇所変更するだけで、何箇所も同時に変更できる書き方
→ もし10箇所エラー、 1箇所直すだけで済む



・キレー・読みやすい
→元々英語で読むため、コードを読んでどんな処理が行われているかわかること



参考サイト


よく使うgitコマンド

Gitでローカルブランチを削除する

Pythonの命名規則まとめ