(git)最近使ったコマンドメモ

投稿者: | 2016年10月16日
こんにちはヒロウミです。 自分なりのGit利用の心構えと、コマンドのメモをしていきます。(※随時更新予定)

心構え
・1変更1コミットの粒度にする
いつでもそのコミットを取り消せるようにして他の変更との依存関係を減らす

(2016/10/16)
git config --list
git clone https://github.com/oceanhiro/myshells ディレクトリ名
git checkout mergetest
git status
git log -p -2

-p 差分を表示
-数値 表示するコミットログ数
git remote -v
git branch -a

表示されないときは以下のコマンドで、リモートから情報を取得
git fetch

ブランチの移動
git checkout -b mergetest2 origin/mergetest2
git commit --amend
エディタが開くのでコメントを編集
git remote set-url origin  https://oceanhiro@github.com/oceanhiro/myshell
git log
ハッシュ値をコピー
git reset --hard ハッシュ値
git mv true-test/false-unit-test.js test/
(2016/10/17)
マージを取り込むブランチに移動
# git checkout mergetest

マージ
# git merge mergetest2

同一ファイルを編集していてコンフリクトが発生した場合
# git merge mergetest2
Auto-merging conflict-file.txt
CONFLICT (content): Merge conflict in conflict-file.txt
Automatic merge failed; fix conflicts and then commit the result.

コンフリクトを起こしているファイルの修正
# vim conflict-file.txt
# git add conflict-file.txt
# git commit -m 'コンフリクトの修正'

マージを依頼された人のローカルリポジトリ
# git fetch origin
# git checkout -b conflict-fetch origin/conflict-fetch
# git merge conflict
Auto-merging conflict-file.txt
CONFLICT (content): Merge conflict in conflict-file.txt
Automatic merge failed; fix conflicts and then commit the result.

コンフリクトしたファイルはこんな感じになっている
# cat conflict-file.txt 
this is conflict file!!!
this is conflict file!!!
this is conflict file!!!
this is conflict file!!!
<<<<<<< HEAD
not conflict!!
not conflict!!
not conflict!!
not conflict!!
not conflict!!
not conflict!!
=======
conflict!!
conflict!!
conflict!!
conflict!!
conflict!!
conflict!!
>>>>>>> conflict

コンフリクトの修正
# vim conflict-file.txt

修正内容をステージング
# git add conflict-file.txt
# git commit -m 'リモートコンフリクトの修正'

# git checkout conflict

# git merge --no-ff conflict-fetch

↓このタイトルを修正(コミットログ)
Merge branch 'conflict-fetch' into conflict
 
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

修正内容をリモートにpush
# git push origin conflict


(2016/10/18)
消されるファイルの確認
# git clean -n
Would remove clean.txt
Would remove hello.txt

untracked fileの削除
# git clean -f
Removing clean.txt
Removing hello.txt
(2016/10/30)
ブランチの作成/移動
# git checkout -b checkout-test
(2016/12/11)
$ git rm -r --cached node_modules/
$ git commit -m 'node_modeleを管理対象外'
(2016/10/30)
ブランチの作成/移動
# git checkout -b checkout-test
(2016/12/11)
$ git rm -r --cached node_modules/
$ git commit -m 'node_modeleを管理対象外'
(2019/02/20)
※デフォルのエディタが開く
こんな感じ↓メールの件名と本文みたいな感じ
何のための変更かを詳細に伝えるのにとても有用なのでぜひ活用したいところ
※タイトル
※空行    <--これ決まりらしいので必ず空行入れましょう
※コミットメッセージの詳細
※ ....

# git commit
===========================================================
  1 Add test file for several line commit message↲
  2 ↲
  3 I try several line commit message.↲
  4 This space is message area.↲
  5 Write more commit message here.↲
  6 Enjoy Git!!↲
  7 # Please enter the commit message for your changes. Lines starting↲
  8 # with '#' will be ignored, and an empty message aborts the commit.↲
  9 #↲
 10 # On branch master↲
 11 # Your branch is ahead of 'origin/master' by 1 commit.↲
 12 #   (use "git push" to publish your local commits)↲
 13 #↲
 14 # Changes to be committed:↲
 15 #»--new file:   testfile.txt↲
 16 #↲
===========================================================
※過去に書いた変なコミットメッセージを修正したり、後から気付いた補足事項などを追記します。
流れ
1. git log で修正したいコミットより1つ前(古い)のコミットIDを取得
2. git rebase -i `1で取得したID`
3. デフォルトのエディタが開くので「pick -> edit」に変更して保存
4. git commit --amend
5. コミットメッセージを修正
6. git rebase --continue で元のコミットまで戻る

(変更の開始)
# git rebase -i 817a6ff
===========================================================
  1 pick 0215c0e Add for check git commit message↲
     ↓以下のように変更して保存
  1 edit 0215c0e Add for check git commit message↲
  2 pick 752aea3 Add test file for several line commit message↲
  3 pick e48702d Add test file for several line commit message↲
  4 ↲
  5 # Rebase 817a6ff..e48702d onto 817a6ff (3 commands)↲
  6 #↲
  7 # Commands:↲
~~snip~~
===========================================================

(editとしてしたコミットまで遡ってきた)
# git rev-parse HEAD
0215c0e7f4732b28c3c9cb25f36f563203b68ec8

(このコミット状態でコミットメッセージを編集する)
===========================================================
  1 Add for check git commit message↲
  2 ↲
  3 This commit is test.I want to check commit message 3 line.↲
  4 Fix commit message for test.↲ <-- こいつを追記した
~~snip~~
===========================================================

(変更を確定させて元のコミットまで戻る)
# git rebase --continue
Successfully rebased and updated refs/heads/master.