コードを書く時の Tips(Ruby, Python, Git)
目次
Directory
Ruby
ディレクトリ | 説明 | メモ |
---|---|---|
bin/ | バイナリ実行可能ファイル | |
lib/ | ライブラリモジュール | ラッパーも置いておくと〇 |
spec/ | unit テスト | gem rspec |
Rakefile | rake コマンドで使えるタスク | make ファイルの Ruby version |
Gemfile | このアプリケーションに必要な gem の定義ファイル | bunder init で生成 |
Gemfile.lock | アプリケーションで使われる gem のバージョンを確認するためのリスト | bundle install で生成 |
.gitignore | Git に取り込みたくないファイルを指定するためのパターン | |
.editconfig | 言語ごとの tab 数などのエディタ設定 | |
README.md | アプリケーションの簡単な説明 |
Rails (参考: デフォルトのRailsフォルダ構造 - Qiita )
ディレクトリ | 説明 | メモ |
---|---|---|
app/ | モデル、ビュー、コントローラ、ヘルパーなどを含む主要なアプリケーションコード | |
app/assets | アプリケーションで使う CSS (Cascading Style Sheet)、JavaScript ファイル、画像などのアセット | |
bin/ | バイナリ実行可能ファイル | |
config/ | アプリケーションの設定 | |
db/ | データベース関連のファイル | |
doc/ | マニュアルなど、アプリケーションのドキュメン | |
lib/ | ライブラリモジュール | |
lib/assets | ライブラリで使う CSS (Cascading Style Sheet)、JavaScript ファイル、画像などのアセット | |
log/ | アプリケーションのログファイル | |
public/ | エラーページなど、一般(Web ブラウザなど)に直接公開するデータ | |
bin/rails | コード生成、コンソールの起動、ローカルの Web サーバの立ち上げなどで使う Rails スクリプト | |
spec/ | unit テスト | gem rspec |
test/ | アプリケーションのテスト | |
tmp/ | 一時ファイル | |
vendor/ | サードパーティのプラグインや gem など | |
vendor/assets | サードパーティのプラグインや gem で使う CSS (Cascading Style Sheet)、JavaScript ファイル、画像などのアセット | |
README.md | アプリケーションの簡単な説明 | |
Rakefile | rake コマンドで使えるタスク | |
Gemfile | このアプリケーションに必要な gem の定義ファイル | |
Gemfile.lock | アプリケーションで使われる gem のバージョンを確認するためのリスト | |
config.ru | Rack ミドルウェア用の設定ファイル | |
.gitignore | Git に取り込みたくないファイルを指定するためのパターン | |
.rubocop | コーディング規約のチェック | gem rubocop, rubocop-rails |
Python
ディレクトリ | 説明 | メモ |
---|---|---|
bin/ | バイナリ実行可能ファイル | |
lib/ | module | moduleを呼び出すラッパーも置いておく |
tests/ | test code | |
Pipfile | このアプリケーションに必要な pip の定義ファイル | pipenv を使う場合 |
.gitignore | Git に取り込みたくないファイルを指定するためのパターン | |
.editconfig | 言語ごとの tab 数などのエディタ設定 | |
README.md | アプリケーションの簡単な説明 |
package
Ruby
パッケージ名 | 説明 | メモ |
---|---|---|
rbenv | ruby version 管理 | |
bundler | gem 管理 | |
rspec | unit テストの gem | bundler でインストール |
rails | Rails App を作成 | bundler でインストール |
Python
パッケージ名 | 説明 | メモ |
---|---|---|
pyenv | python version 管理 | |
pipenv | pip 管理 | poetry の選択肢もあり |
unittest | unit テストの pip | pipenv でインストール |
お作法
git commit
項目 | 参考 |
---|---|
git commit | Gitのコミットメッセージの書き方 - Qiita |
.gitignore
項目 | 参考 |
---|---|
.gitignore | https://github.com/github/gitignore |
.editconfig
項目 | 参考 |
---|---|
.editconfig | https://editorconfig.org/ |
Git(参考: gitコマンド チートシート - Qiita)
設定
用途 | コマンド |
---|---|
ユーザ名設定 | git config --global user.name "[name]" |
メールアドレス登録 | git config --global user.email "[example@example.com]" |
リポジトリ作成
用途 | コマンド |
---|---|
ローカルリポジトリ作成 | git init |
リモートからクローン作成 | git clone [url] |
確認
用途 | コマンド |
---|---|
staged / modified / untracked の一覧 | git status |
ローカルブランチの一覧 | git branch |
ローカルブランチとリモート追跡ブランチの一覧 | git branch -a |
[履歴] 現在のブランチの履歴 | git log |
[履歴] ファイルの履歴 | git log --follow [file/path] |
[差分] ブランチ間差分表示 | git diff [first-brach] [second-branch] |
[差分] ワーキングディレクトリ と ステージングエリア の差分を表示 | git diff |
ステージング
用途 | コマンド |
---|---|
指定のファイルを staged にする(スペース区切りで複数選択) | git add [file/path] |
すべての modified を staged にする (--update) | git add -u |
すべての untracked とすべての modified を staged にする (--all) | git add -A |
カレントディレクトリのすべてのファイルを staged にする (cf. -A はリポジトリ全体) | git add . |
staged から modified にもどす | git reset [file/path] |
コミット
用途 | コマンド |
---|---|
コミット | git commit -m "[comment]" |
ユーザ名メールアドレスを指定してコミット | git -c user.name='username' -c user.email='example@example.com' commit -m "[comment]" |
ステージング(untrackedは対象外) & コミット | git commit -a -m "[comment]" |
直前のコミットのコメントを修正する | git commit --amend -m "[comment]" |
チェックアウト
用途 | コマンド |
---|---|
チェックアウト | git checkout [branchname] |
ブランチ作成 & チェックアウト | git checkout -b [branchname] |
特定のコミットからブランチを作成してチェックアウト | git checkout -b [branchname] [コミットID] |
ブランチ
用途 | コマンド |
---|---|
ブランチ作成 | git branch [branchname] |
ブランチ名の変更 | git branch -m [new_branchname] |
ブランチ削除 | git branch -d [branchname] |
リモートブランチの削除 | git push --delete origin [remote_branchname] |
マージ (ブランチの合流) | git checkout [取込先ブランチ] git merge [対象ブランチ] --no-ff |
リベース (ブランチの付替え) | git checkout [対象ブランチ] git rebase [ブランチを生やすブランチ名] |
他ブランチのコミットを取り込む | git cherry-pick [コミットID] |
タグの作成 | git tag [tagname] |
リモートリポジトリ
用途 | コマンド |
---|---|
リモートリポジトリの一覧表示 | git remote |
リモートリポジトリのすべてのブランチの更新履歴をリモート追跡ブランチに取り込む | git fetch |
リモート追跡ブランチを指定ローカルブランチにマージする | git merge [origin/branchname] [branchname] |
リモートブランチの変更をローカルブランチに反映させる・1 | git fetch git checkout [branchname] git merge [origin/branchname] |
リモートブランチの変更をローカルブランチに反映させる・2 | git pull |
リモートブランチからローカルブランチの作成 | git branch [branchname] [remote_branchname] |
現在のブランチの変更をリモートブランチに反映させる | git push |
現在のブランチのリモートブランチを作成する | git push --set-upstream origin [branchname] |
ファイル編集・削除
用途 | コマンド |
---|---|
ファイル名の変更、ファイルの移動 | git rm [old_file/path] [new_file/path] |
ファイルを削除する | git rm [file/path] |
ファイルを監視対象から外す | git rm --cached [file/path] |
ディレクトリを監視対象から外す | git rm --cached -r [path] |
ファイル名変更・コミット | git mv [file_original] [file_renamed] |
変更の一時保存
用途 | コマンド |
---|---|
modified と staged を保存し、HEADの状態までクリーンに戻す | git stash |
直近の記録をワーキングディレクトリに反映する (LIFO(後入れ先出し)) | git stash pop |
スタッシュの一覧を表示する | git stash list |
直近の記録を破棄する (LIFO) | git stash drop |
やり直す
用途 | コマンド | 備考 |
---|---|---|
直前のコミットのコメントを変更する | git commit --amend -m "[comment]" |
|
commit_id 以降のすべてのコミットを取り消し (ワークツリーに残す) | git reset --soft [commit_id] |
[commit_id]をHEAD^ に変更すると直前のコミットを取り消す(ファイルは残る) |
commit_id 以降のすべてのコミットを取り消し (ワークツリーに残さない) | git reset --hard [commit_id] |
[commit_id]をHEAD^ に変更すると直前のコミットを取り消す(ファイルも消える) |
マージを取り消す | git merge --abort |
|
コミットを打ち消すコミットをする | git revert [commit_id] |
|
特定のファイルだけ以前の状態に戻す | git checkout [commit_id] [file/path] |
|
直前のコミットに変更を追加する | git add [file/path] git commit --amend --no-edit |
|
遡ってコメントを修正する | git rebase -i HEAD~3 # viエディタで対象のコミットのpickをeditに変更し、保存 git commit --amend -m "[comment]" git rebase --continue |
|
削除したブランチを戻す | git reflog # viでHEADの履歴が表示される # 消してしまったブランチの最後のコミットを見つけたらコミット番号をコピー git branch [branchname] [コミット番号] |