//tips
/gitコマンド操作理解
//git resetの利用
ステージング環境から外したい場合、つまりaddコマンドを取り消したい場合git resetを用いる。
exampleブランチに新しくaファイルを作成。
touch a.txt
lsで現在下記のファイルがあることがわかる。
README.md a.txt index.html
git add a.txtによって登録を行った後に、
git status
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: a.txt
git reset a.txt
を入力することで、
git status
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
とすることができる。
Untracked filesとなった作業中のファイルa.txtを消去するにはcleanを用いる。
ただ、消去後に慌てないために、消去するものを事前に教えてくれるgit clean -d -nを使うと、ハプニングはなくなる。
git status
On branch example
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
git clean -d -n
Would remove a.txt
抹消コマンド。
git clean -f -d
これにより、ブランチexampleのgit statusは
On branch example
nothing to commit, working tree clean
と表示される。
//ファイルの修正前後の同期の流れを確認する
echo "hello" > a.txt
helloテキストを登録し、ステージングにあげる
git add a.txt
作業スペースのテキストにhioを上書きする
echo "hio" > a.txt
git status
On branch example
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: a.txt
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: a.txt
ステージングエリアの内容を作業エリアの内容に上書し、hio→helloに戻す。
git checkout a.txt
cat a.txt
hello
git status
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: a.txt
git commit -m "メッセージ"
git push origin example
hello
ファイル名の変更はmvを用いて、
git mv a.txt aaa.txt
On branch example
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: a.txt -> aaa.txt
のように、元の名前 新しい名前の順に記載する。
名前変更は、作業中エリアだけでなく、ステージングエリアにも反映される。
//ブランチの作成や編集
developmentブランチの作成。
git branch development
exampleからdevelopmentへのブランチ切り替え。
git checkout development
ブランチの名称変更development→develop。
git branch -m develop
* develop
example
master
developブランチを削除する際に削除対象のブランチにいる場合はエラーが出てしまうのでcheckoutしてから
git branch -d develop
を行う。
ちなみに、ブランチ内のコミット履歴を簡易的に見るためにgit log --onelineを用いると、
exampleブランチでは下記のようなことをしていることがわかる。
git log --oneline
8d28644 (HEAD -> example, origin/example) メッセージ
65eb6da 修正版
7e2da4d add new README file
d3016da add memo
9a3baef Create index.html
git statusを行うと、
On branch example
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: a.txt -> aaa.txt
aaaの編集はaddされているが、コミットされていないので、コミット&プッシュによりリモートにも反映させる。
git commit -m "aaa.txt"
git push origin example
//リモートのexampleブランチで変更を加え、git fetchを利用してローカルに同期させる
リモートのexampleブランチにomg.txtを追加した。
その情報をターミナルに認識させるために
git fetch
により最新情報へ更新する。
これにより
git log origin/example --oneline
bcdee66 (origin/example) Create omg.txt
e3184fd (HEAD -> example) aaa.txt
8d28644 メッセージ
65eb6da 修正版
7e2da4d add new README file
d3016da add memo
9a3baef Create index.html
がリモートのコミット履歴として現れる。
bcdee66 (origin/example) Create omg.txtが追加したomgテキストであることがわかる。
これをローカルのexampleブランチに取り込むために
git merge origin/example
を行うことでローカルのexampleブランチにもomgテキストが組み込まれる。
リモートからローカルへの取り込みは、差分のみ取得するプルも使えるが、リモートに他の人が作業していたファイルがプッシュされていた場合には、未知の差分も入手していることがあり、ベースとしていた内容がいつの間にか変わっていたりするので注意が必要。
git fetch、git status, git diffをこまめに確認して差分箇所を理解することが肝要となる。
//ブランチ切り替え時のgit checkout エラーへの対処
Exampleブランチから新たなブランチを作成し、そちらに移動。
git branch test
git checkout test
Testブランチにテキストファイルを追加し、コミット。
echo "test" > hello.txt
git add hello.txt
git commit -m "test"
コミットしたファイルの内容修正を作業スペースで行っている途中に、
echo "alert" > hello.txt
別のブランチに移動しようとすると
git checkout example
下記のようなエラーが生じる。
error: Your local changes to the following files would be overwritten by checkout:
hello.txt
Please commit your changes or stash them before you switch branches.
Aborting
この状態をgit statusで見ると
On branch test
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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
となっており、作業スペースにあるmodified: hello.txtをどうにかしなければ他のブランチに移動できない。
この際に、
git stash
Saved working directory and index state WIP on test: 5a55bf8 test
を行い、作業スペースの情報とインデックススペースの情報を保存することで、git checkout exampleを可能にする。
git checkout testに戻り、
cat hello.txt
を行うと、修正前のテキスト”test”が表示されてしまう。
これを修正後の"alert" に戻すために
git stash pop
On branch test
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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (dd2b0c55f242ce09f56cdaad9b267d45629ecb2a)
を行う。
Catで元の状態に戻っていることを確認する。
注意しておきたい点は、git stash popは他のブランチとも共有で使われること、最後に保存されたものが最初に取り出されてしまうこと。
取り出しを間違えないように、git stash listやgit stash showを使用して、git stashで保存した内容を取り出す際に確認するようにする。