//tips
//Github
testブランチのマージを行う。
作業中ブランチをtestブランチからmasterブランチに切り替える。
git checkout master
git branch
example
* master
test
マージすることでhello.htmlファイルをmasterブランチに追加する。
git merge test
これでhello.htmlがmasterブランチに追加されたので
git push origin master
でリモートレポジトリに反映する。
この状態ではマージ元のtestブランチは残ったままとなる。
もしマージ元のブランチを削除したい場合は
git branch -d test
で行う。
リモートのmaster branchにhi.htmlを追加したので、ローカルにプルする。
現在のローカルのmasterは
ls
hello.html
index.html
これにリモートの変化分をプルするため
git pull origin master
ローカルでプルがきちんとできているか確認する。
再度lsすることで
hello.html
hi.html
index.html
が表示されることがわかる。
Exampleブランチに切り替える。
git checkout example
中のファイルであるindex.htmlをvisual studioで開き、編集する。
中身に下記を追加した。
Hello World</br>
Live as if you were to die tomorrow. Learn as if you were to live forever.</br>
If you can’t, you must. If you must, you can.</br>
Better hazard once than always be in fear.</br>
ローカルのファイル更新をリモートに反映させる。
変更ファイルを保管庫に追加。
git add index.html
変更ファイルをadd memoとして保存。
git commit index.html -m "add memo"
ローカルからリモートのexampleブランチにプッシュ。
git push origin example
これでリモートの変更が確認できた。
Exampleブランチにreadmeファイルを追加する。
echo '# hello,git!' > README.md
readmeファイルの中身を確認。
cat README.md
# hello,git!
lsでブランチにきちんとreadmeファイルができたか確認。
README.md index.html
ここで
git status
を行うと、Untracked filesとしてREADME.mdが表示される。
On branch example
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
これは、README.mdが作業スペースにあるだけで、gitの保存対象にも、リモートにあげる対象にもなっていないgit管理外ファイルということを示す。
これをgitの管理対象に加えるためにaddコマンドがあり、
git add README.md
を入れると、
On branch example
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: README.md
という状態になる。
また、全てのファイルを登録したいときは、ファイル名は必要なく、ドットで代用できる。
git add .
この登録された状態が、ステージングエリアに登録されている状態という。
登録後にREADME.mdを書き換えてみる。
echo 'Hi,Gityyy!' > README.md
この状態をgit statusで確認する。
そうすると、ステージングのREADME.mdは登録され、作業中のREADME.mdは未登録と表示される。
On branch example
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: README.md
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
ここで作業中のものとステージングのもので生じている差分を確認するために
git diff README.md
を行うと、
diff --git a/README.md b/README.md
index c6f6e38..a614a87 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-# hello,git!
+Hi,Gityyy!
と表示される。
ステージングのファイル内容をベースに-# hello,git!で中身を消し、+Hi,Gityyy!で中身を足していることがわかる。
hello,git!のままの保存で良いので
git commit
を押し、vimをコミットメッセージ編集用エディタとして呼び出す。
add new README file
修正したものはそのままの状態で作業スペースにあり。
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch example
# Changes to be committed:
# new file: README.md
#
# Changes not staged for commit:
# modified: README.md
このようにメッセージを入力したらEscキーを押して、:wqを入力することでメッセージ内容を保存させ、元の画面に戻ることができる。
私もこのgit commit画面から抜け出せなくなったので下記を参考にした。
https://qiita.com/Adan2JP/items/7c0bb0d81759a1da77f4
登録し、コミットした README.mdを一旦削除する。
git rm --cache README.md
下記の状態となる。
On branch example
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
その後、
git add README.md
git commit -m "修正版"
で再度ステージ環境にアップ。
ローカルではなく、過去のコミットを確認するために、コミット履歴を表示する。
git log
これにより、
「 修正版」
「 add new README file 修正したものはそのままの状態で作業スペースにあり。」
「add memo」
といった過去のコミットのメッセージ、時間、保存主などが表示される。