Gitをいじる
git initして初めのリポジトリを作成しても初めのコミットを作成するまではファイルを作成してもgit関連コマンドに何も表示されないので混乱する。
初めてのコミット後、reflogを見ると以下のように表示される。
$ git reflog
311b7c7 (HEAD -> master) HEAD@{0}: commit (initial): this is the first commit
前から「あれ?」と思うことが多かったが、変更をcommitやstashせずに別ブランチに移動すると、その変更は新しいブランチに自動的に移動されてしまうらしい。はじめはそれで結構やらかした記憶がある。
ORIG_HEADはひとつ前のコミットを指す。
正確にはHEADを大きく変えるコマンド実行後にその一つ前のコミットを指すようだ。
is created by commands that move your HEAD in a drastic way (git am, git merge, git rebase, git reset), to record the position of the HEAD before their operation, so that you can easily change the tip of the branch back to the state before you ran them.
MERGE_HEADはマージする際にマージしようとしているブランチのコミットを記録するっぽい。
records the commit(s) which you are merging into your branch when you run git merge.
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
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")
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
git diff を引数なしで実行すると、常にインデックスと作業ディレクトリが比較される。
マージについてなんとなく操作しているが、正確な動作は理解できていない。
Gitは未マージであるという印をインデックス内で管理している。git status、git ls-files -u で未マージ対象のファイルを確認できる。
競合が発生したファイルを開くと、マージのマーカーが追加されている。
JDD+takafumi.ueda@JDDC-P190213857 MINGW64 ~/git/training/test_1 (test-1|MERGING)
$ cat test.txt
<<<<<<< HEAD
Line1
Line2
Line3
======
LINE1
>>>>>>> test-2
手動でファイルを修正して競合をかいけつする場合は、マージマーカーは削除する。
このサイトの「マージ作業中のdiffとファイル取得」のイメージは分かりやすい。
リモートのブランチをローカルにコピーする。
git checkout -b dev origin/dev
ブランチ間のコミット差分の確認
git log --no-merges 基準とするブランチ..取り込みたいブランチ --oneline
リリースブランチにStagingブランチの更新分をリベース
$ git rebase staging
ワークツリーの変更をすべて削除
git checkout -- .
変更を一時的に退避する
git stash -u
退避した変更を確認する
git stash list
退避した変更をすべてクリア
git stash clear
退避した作業を元に戻す
git stash apply stash@{0}
WSLでのGit Pushができない。
WSLからGit Pushがうまく行かない件、WSLで以下のコマンドを実行したら通った
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-wincred.exe"