Open22

Gitをいじる

usuushiusuushi

git initして初めのリポジトリを作成しても初めのコミットを作成するまではファイルを作成してもgit関連コマンドに何も表示されないので混乱する。

初めてのコミット後、reflogを見ると以下のように表示される。

$ git reflog
311b7c7 (HEAD -> master) HEAD@{0}: commit (initial): this is the first commit
usuushiusuushi

前から「あれ?」と思うことが多かったが、変更をcommitやstashせずに別ブランチに移動すると、その変更は新しいブランチに自動的に移動されてしまうらしい。はじめはそれで結構やらかした記憶がある。

usuushiusuushi

ORIG_HEADはひとつ前のコミットを指す。

usuushiusuushi

git restoreでワーキングディレクトリでの変更をクリアすることができる。

$ echo "AAAAA" >> test.txt 
$ git status
On branch main
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:   test.txt

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

 git restore test.txt 

$ git status
On branch main
nothing to commit, working tree clean
usuushiusuushi

git restore -S でインデックスからワーキングスペースへ戻すことができる。

$ echo "BBBB" >> test.txt
$ git status
On branch main
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:   test.txt

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

$ git add test.txt 

$ git status 
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.txt


$ git restore -S
fatal: you must specify path(s) to restore

$ git restore -S test.txt 

$ git status
On branch main
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:   test.txt

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

usuushiusuushi

gitコマンドのエイリアスを設定する。

$ git config --global --edit
$ git config --global --list
user.name=utakafumi
user.email=takafumi.ueda@japan-d2.com
core.autocrlf=false
alias.b=branch
alias.s=switch
alias.c=commit -m
alias.a=add .
alias.l=log --oneline
alias.ch=checkout
alias.sta=status

usuushiusuushi

コミットグラフというものがあるみたいだが、まだ見方がよくわからない。

usuushiusuushi

git diff を引数なしで実行すると、常にインデックスと作業ディレクトリが比較される。

usuushiusuushi

マージについてなんとなく操作しているが、正確な動作は理解できていない。

usuushiusuushi

Gitは未マージであるという印をインデックス内で管理している。git status、git ls-files -u で未マージ対象のファイルを確認できる。

usuushiusuushi

競合が発生したファイルを開くと、マージのマーカーが追加されている。

JDD+takafumi.ueda@JDDC-P190213857 MINGW64 ~/git/training/test_1 (test-1|MERGING)
$ cat test.txt 
<<<<<<< HEAD
Line1
Line2
Line3

======
LINE1
>>>>>>> test-2

usuushiusuushi

手動でファイルを修正して競合をかいけつする場合は、マージマーカーは削除する。

usuushiusuushi

リモートのブランチをローカルにコピーする。

git checkout -b dev origin/dev
usuushiusuushi

ブランチ間のコミット差分の確認

git log --no-merges 基準とするブランチ..取り込みたいブランチ --oneline
usuushiusuushi

リリースブランチにStagingブランチの更新分をリベース

$ git rebase staging
usuushiusuushi

ワークツリーの変更をすべて削除

git checkout -- .
usuushiusuushi

変更を一時的に退避する

git stash -u

退避した変更を確認する

git stash list

退避した変更をすべてクリア

git stash clear

退避した作業を元に戻す

git stash apply stash@{0}