2019/07/16に見た記事等を雑に分類するだけ
- AWS
- Chromebook
- Rails
-
- 1.4 Gitによるバージョン管理
- 1.4.1 インストールとセットアップ
- git configで設定
-
$ git config --global user.name "Your Name"
$ git config --global user.email your.email@example.com
- git initでセットアップ
-
$ git init
Initialized empty Git repository in
/home/ec2-user/environment/environment/hello_app/.git/
- $ git add -A
- 現在のディレクトリにあるファイルがすべて追加される
- ステージングの状態を知るにはstatusコマンドを使用する
-
$ git status
On branch master Initial commit
Changes to be committed:
(use "git rm --cached
..." to unstage)
new file: .gitignore
new file: Gemfile
new file: Gemfile.lock
new file: README.md
new file: Rakefile
.
.
.
- ステージングエリアで控えている変更を本格的にリポジトリに反映 (コミット) するには、commitコマンドを使用する
-
$ git commit -m "Initialize repository"
[master (root-commit) df0a62f] Initialize repository
.
.
.
- コミットメッセージの履歴を参照
- $ git log
- 1.4.2 Gitのメリット
- app/controllers/ディレクトリを誤って削除してしまった場合
-
$ ls app/controllers/
application_controller.rb concerns/
$ rm -rf app/controllers/
$ ls app/controllers/
ls: app/controllers/: No such file or directory
- r:recurcive, f:force
- 現在の状態を確認
-
$ git status
On branch master
Changed but not updated:
(use "git add/rm
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory)
deleted: app/controllers/application_controller.rb
no changes added to commit (use "git add" and/or "git commit -a")
- この変更が行われたのは現在の「作業ツリー」内のみであり、コミットはされていない
- 前のコミットをcheckoutコマンド (と、現在までの変更を強制的に上書きして元に戻すための-fフラグ) でチェックアウトすれば削除前の状態に戻る
-
$ git checkout -f
$ git status
# On branch master
nothing to commit (working directory clean)
$ ls app/controllers/
application_controller.rb concerns/
- GitHub(原文はBitbucket)
- Gitリポジトリに公開鍵を登録
- Git - SSH 公開鍵の作成
- catコマンドで公開鍵を出力する
- リポジトリ追加とリポジトリへのプッシュ
- BitbucketをリポジトリのoriginとしてGitの設定ファイルに追加する
- $ git remote add origin https://github.com/ユーザー名/hello_app.git
- ローカルのリポジトリをリモートのoriginにプッシュ
- $ git push -u origin master
- 1.4.4 ブランチ、編集、コミット、マージ
- Branch (ブランチ)
- トピックブランチ (短期間だけ使う一時的なブランチ) はcheckoutと-bフラグを使って作成
-
$ git checkout -b modify-README
Switched to a new branch 'modify-README'
$ git branch
master
* modify-README
- Edit (編集)
- README.mdを編集
- Commit (コミット)
-
$ git status
On branch modify-README
Changes not staged for commit:
(use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
- この時点で、git add -Aを実行することもできるが、git commitには現存するすべてのファイル (git mvで作成したファイルも含む) への変更を一括でコミットする-aフラグがある
-
$ git commit -a -m "Improve the README file"
[modify-README 9dc4f64] Improve the README file
1 file changed, 5 insertions(+), 22 deletions(-)
- ※最後のコミット後に新しいファイルを追加した場合は、まずgit addを実行してバージョン管理下に置く必要がある
- Merge (マージ)
- masterブランチに変更をマージ
-
$ git checkout master
Switched to branch 'master'
$ git merge modify-README
Updating af72946..9dc4f64
Fast-forward
README.md | 27 +++++----------------------
1 file changed, 5 insertions(+), 22 deletions(-)
- トピックブランチを削除
-
$ git branch -d modify-README
Deleted branch modify-README (was 9dc4f64).
- トピックブランチの削除は必須ではない
- 残しておけば、トピックブランチとmasterブランチを交互に行き来して、キリの良い所で変更をマージする事ができる
- トピックブランチの変更を破棄する場合
-
# これはあくまで例です。ブランチでミスをした時以外は実行しないでください。
$ git checkout -b topic-branch
$ <ファイルを作成したり編集したり削除したり...>
$ git add -A
$ git commit -a -m "Make major mistake"
$ git checkout master
$ git branch -D topic-branch
- -dフラグと異なり、-Dフラグは変更をマージしていなくてもブランチを削除する
- Push (プッシュ)
- 既に1.4.3で一度プッシュを行ったので、大抵のシステムではgit pushを実行するときにorigin masterを省略できる
- $ git push