LoginSignup
0
0

現場で使っていたgitコマンドまとめ

Posted at

##概要
メモしてたやつを整理しただけです。

git add 取り消し

git reset HEAD

ローカルとリモートのブランチ名を変更する

git branch -m 古いブランチ名 新しいブランチ名
git push -u origin 現在のブランチ名      // $ git push -u origin HEAD とするとブランチ名を入力しなてもpushできるので便利です。
git push origin :リモートのブランチ名

直前のコミット操作を取り消す

git reset --soft HEAD^

PRでコードを埋め込みつつ、折りたたむ

必ず一行開けてあげる
<details><summary>必ず一行開けてあげる</summary><div>
ここにコードを各
</div></details>

操作の取り消し

git reflog

以下の様に表示される

02f11b7 HEAD@{0}: reset: moving to 02f11b7
0488e28 HEAD@{1}: merge develop: Merge made by the 'recursive' strategy.
6978c20 HEAD@{2}: checkout: moving from develop to master
32275f0 (develop) HEAD@{3}: commit: 制作事例を追加
0e95561 HEAD@{4}: checkout: moving from master to develop
6978c20 HEAD@{5}: commit: 3回目のコミット
0e95561 HEAD@{6}: commit: 2回目のコミット
02f11b7 HEAD@{7}: commit (initial): はじめてのコミット

「resetの操作をする前(1つ前)に戻したい」といった場合、コマンドは次のとおりです。

git reset --hard HEAD@{1}

commitの修正

tigを実行し、目的のcommitの一つ前のcommti IDをコピーして

git rebase -i commitID

編集画面へ飛ぶので、対象のcommitをpick→editにしてwq
目的のファイルを編集しaddして、以下のコマンドですでにあるcommitに修正を反映させる

git commit --amend

editを完了させる

git rebase --continue

git stash

作業中に別の作業をしたいときにコミットしていない変更を退避させるコマンド。

  • 変更を退避する
git stash save
  • 退避した作業一覧を見る
git stash list
  • 退避した作業を戻す(listで確認してから行う)
git stash apply stash@{0}

現在checkoutしているbranchへ退避した変更が書かれる。
変更を退避したときのbranchにも、それ以外のbranchにも戻すことができる。
stash名を指定しない場合は、直近に退避された作業を戻す。

  • 退避した作業をstashリストから消す
git stash drop stash@{0}
  • 退避した作業を戻すと同時にstashリストから消す
git stash pop stash@{0}
  • 作業を退避させる時にメッセージをつける
git stash save "stash message"

diff

branch間のdiff

https://github.com/cakephp/cakephp/compare/3.0...3.0-fix-log-scope

/compare/がポイント

commit間のdiff

https://github.com/cakephp/cakephp/compare/b5cbe4de58eedc1e10bb0ac06da8d134e38a1dc4...970cb81d03dc8f3db9efe7b9271f4b4e089dd3ae

レビューのこつ

GitlabとちがってGithub的にはPR上でReview作業を進めていくほうが楽なので、きちんとdescriotionにcheck pointなどをコメントしておく
また、意図が伝わりづらいcode部分に注釈を入れたり、commit追加したときにも修正内容をコメントするなど、reviewrを意識して運用していくと良い

conflict解消

ブランチで作業してるけど、作業中に他のメンバーがマスターにマージしてマスターの情報が新しくなった!
という時に、作業中の自分のブランチにマスターの最新情報を追加して自分がマージ
した時のコンフリクトを最小限に抑えたい&マージされた内容をもとに実装をしたい

git co master
git pull
git co 作業branch
git rebase master

# conflict箇所修正して
git ad .
git rebase --continue

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0