Skip to content
gigatune edited this page Jun 5, 2013 · 20 revisions

git のいろいろ

git merge で変更点が消えた

ブランチは

  • master
  • hoge
  • feature の3つのブランチが存在。
  1. master から hoge を派生
  2. hoge を修正
  3. master から feature を派生
  4. feature に機能追加
  5. master に feature の修正点をマージ
  6. hoge に master をマージ したたころ、うまくいかなかった。

解決しました。

git checkout 08841b3 -b aaa   # hogeの最終状態を aaa ブランチとして取り出し
git checkout 351f83e0b22a55b1967811ac6cb18eb5b1ed -b master_featured   # masterブランチの最新状態を master_featured として取り出し
git checkout aaa
git merge master_featured
(コンフリクトを手動で修正)

aaa <-> animation, master <-> master_featured と対比させれば同じ事をしてるんだけど、なんで最初はダメだったのだろう・・・・・

日付 featureでの変更 hogeでのコマンド
2013/05/30 11:00 git checkout -b hoge
2013/05/31 09:00 a.txt を編集 / git commit / (4f3)
〜〜〜 〜〜〜
2013/06/01 12:00 xxxx.txt を編集 / git commit (d8e)
2013/06/03 08:00 git merge branch (6ef)
()内は、コミットのハッシュ

結果、a.txt が master の内容に戻っていた。

git reflog と、hogeブランチの git log のハッシュキーを付きあわせてみると、以下の通り。

--- --- --- reflog hogeブランチのlog --
015 HEAD@{} commit: (merge): Merge branch master into hoge
014 HEAD@{} checkout: moving from master to hoge
013 HEAD@{} commit comment_013 comment_013 featureブランチでの変更コメント
012 HEAD@{} merge: feature: Fast-forward featureブランチでの変更コメント
011 HEAD@{} checkout: moving from feature to master
010 HEAD@{} commit: comment_010 comment_010 featureブランチでの変更コメント
d8e HEAD@{} commit: xxx.txtを編集 xxx.txtを編集 featureブランチでの変更コメント
009 HEAD@{} merge: master Fast-forward
008 HEAD@{} checkout: moving from master to feature
007 HEAD@{} checkout: moving from 007 to master
006 HEAD@{} commit: comment_006 hogeブランチでの変更コメント
005 HEAD@{} commit: comment_005 hogeブランチでの変更コメント
4f3 HEAD@{} commit: a.txtを編集 hogeブランチでの変更コメント
004 HEAD@{} commit: comment_004 hogeブランチでの変更コメント
003 HEAD@{} commit: moving from hoge to 003
003 HEDA@{} commit: comment_003 comment_003
002 HEAD@{} commit: comment_002 comment_002
001 HEAD@{} commit: comment_001 comment_002

hogeブランチでmasterをmergeしたところ、004〜006の変更が消えたように見える。。。。。

reset hard で変更点を把握できることは確認済み

git reflog
6ef00a2 HEAD@{27}: commit (merge): Merge branch 'master' into hoge
5c4d8ff HEAD@{28}: checkout: moving from master to hoge
351f83e HEAD@{29}: commit: ---
c57c92f HEAD@{30}: merge feature: Fast-forward
247180d HEAD@{31}: checkout: moving from feature to master
c57c92f HEAD@{32}: commit: ---- (featureの変更3)
552491b HEAD@{33}: commit: ----	(featureの変更2)
d8e6ecf HEAD@{34}: commit: ----	(featureの変更1)
247180d HEAD@{35}: merge master: Fast-forward
82017dd HEAD@{36}: checkout: moving from master to feature
247180d HEAD@{37}: checkout: moving from 08841b3a4bcf5437cb0532e61696197404363205 to master
08841b3 HEAD@{38}: commit: hogeの変更
(snip)
6720ba0 HEAD@{54}: commit: hogeの変更 (a.txt)
5c4d8ff HEAD@{55}: checkout: moving from animation to 5c4d8ff23c65b0
5c4d8ff HEAD@{56}: commit: ---
ef5412a HEAD@{57}: commit: ---
b38ccd2 HEAD@{58}: commit: ---

git reset --hard 08841b3 をすると、a.txt は、修正済みの状態になる。

git log --graph --pretty=oneline --abbrev-commit

*   6ef00a2 Merge branch 'master' into hoge
|\
| * 013 comment_013
| * 010	comment_010
| * d8e	xxx.txt	を編集
* | 003	comment_003
* | 002	comment_002
git log --graph --decorate --pretty=oneline --abbrev-commit

*   6ef00a2 (HEAD, hoge) Merge branch 'master' into hoge
|\
| * 351f83e (origin/master) comment
| * c57c92f (feature) ---
| * 552491b ---
| * d8e6ecf ---
| * 247180d ---
| * 187c192 ---
| * 8984ee7 ---
* | 5c4d8ff (origin/hoge) ---
* | ef5412a ---
* | 4f356e3 ---
* | 27f3db7 ---
* | b750707 ---
|/
* 2acf350 ----

ローカル管理していたgitリポジトリを、新たに作ったWeb-Davのgitリポジトリにpushする

ローカル管理しているgitディレクトリは local.git 、http://dummy.com/git/hoge.git がbareレポジトリ 、Web-Davのユーザ名は USER とする。

$ cd local.git/
$ git remote add origin http://dummy.com/git/hoge.git
$ vi .git/config     # USER を追記
-----
[remote "origin"]
        url = http://USER@dummy.com/git/hoge.git
        fetch = +refs/heads/*:refs/remotes/origin/*
-----
$ git push origin master
$ git push origin branch-1

.gitconfig で、名前/メールアドレスが間違っていた場合の対処

git rebase -i (修正したい一番最初のコミットの一つ前のmd5の値)
# コミットログが出てくるので、修正したいコミットの行の pick を e に変更して保存
git commit --amend --author="名前 <メールアドレス>"
git rebase --continue

git push origin master でエラー

$ git push origin master
To git@xxxxxxxxxxxxxxxxxxxx
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'xxxxxxxxxxxxxxxxxxxxxxxxx'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

というエラーが出てoriginにpushできず。

reflog は以下の通り

$ git reflog
3a6ca23 HEAD@{0}: commit: modify : UIをボタンに変更。選択肢も修正 #27
ade42d6 HEAD@{1}: reset: moving to HEAD@{1}
112a5a7 HEAD@{2}: commit: modify : スライダーを削除
ade42d6 HEAD@{3}: commit: modify : word spec の変更
2c32bed HEAD@{4}: rebase -i (finish): returning to refs/heads/master
2c32bed HEAD@{5}: commit (amend): add : ワイルドカードをサポート #25
f0ad517 HEAD@{6}: cherry-pick
69b8a18 HEAD@{7}: checkout: moving from master to 69b8a188c344741eb39ffb3493f961fafb6c6d9e
f0ad517 HEAD@{8}: rebase: aborting

git show-branch は以下の通り

$ git show-branch master origin/master
* [master] modify : UIをボタンに変更。選択肢も修正 #27
 ! [origin/master] debug : cookieを修正 #22
--
*  [master] modify : UIをボタンに変更。選択肢も修正 #27
*  [master^] modify : word spec
*  [master~2] add : ワイルドカードをサポート #25
 + [origin/master] debug : cookieを修正 #22
 + [origin/master^] add : ワイルドカードをサポート #25
 + [origin/master~2]  debug : スライダーに関する css が読み込まれていない不具合を修正
*+ [master~3] debug : phpページに、スライダーの js/css がインクルードされていない不具合を修正

gitのコミットメッセージ修正でハマった事例

コミット f0ad517 のauthorがおかしくなってる。 ※ git では、author と commit は違うので、挙動としては正しかった(追記)

gigatuneユーザが実行したコマンド履歴

  516  git status
  517  git commit -a -m "add : ワイルドカードをサポート"
  518  git push origin master
  519*
  520  git push origin master
  521  git reset --soft HEAD^
  ...
  525  git push -f origin HEAD^:master
  526  git status
  527  git commit --amend -m "add : ワイルドカードをサポート #25"
  528  git status
  529  git push -f origin HEAD^:master
  530  git push origin master

bitbucketで見ると

other User     f0ad517    add : ワイルドカードをサポート #25
Gigatune       69b8a18    debug : phpページに、スライダーの js/css がインクルードされていない不具合を修正
Gigatune       eadadc7    debug : 速度調整のスライダー対応が、PHPページに適用されていなかったのを修正

git logを見ると

ommit f0ad517cd9b479f054df61cd2a1aa54f621133f6
Author: other user
Date:   Fri Apr 5 02:02:24 2013 +0900

    add : ワイルドカードをサポート #25

commit 69b8a188c344741eb39ffb3493f961fafb6c6d9e
Author: gigatune
Date:   Fri Apr 5 01:53:42 2013 +0900

    debug : phpページに、スライダーの js/css がインクルードされていない不具合を修正

commit eadadc7caf862b4281231c19a73e348da62f79e9
Author: gigatune
Date:   Fri Apr 5 01:49:42 2013 +0900

    debug : 速度調整のスライダー対応が、PHPページに適用されていなかったのを修正

git reflogを見てみる

f0ad517 HEAD@{0}: commit (amend): add : ワイルドカードをサポート #25
fc9a121 HEAD@{1}: reset: moving to HEAD^
59da757 HEAD@{2}: commit (amend): add : ワイルドカードをサポート #25
5334c93 HEAD@{3}: commit: add : ワイルドカードをサポート
fc9a121 HEAD@{4}: pull: Fast-forward
69b8a18 HEAD@{5}: commit: debug : phpページに、スライダーの js/css がインクルードされていない不具合を修正
eadadc7 HEAD@{6}: commit: debug : 速度調整のスライダー対応が、PHPページに適用されていなかったのを修正

対処方法

「なぜそうなったか」を理解するのは重要だけど、ちょっと時間の余裕がないので、暫定対応として、ログを強制的に修正。

$ git rebase -i HEAD~1  # (f0ad517 コミットを editに変更して保存)
$ git commit --amend --author='gigatune'
$ git rebase --continue

# bitbucketレポジトリに反映
$ git push -f origin master

(git rebase -i HEAD~1の画面)

pick f0ad517 add : ワイルドカードをサポート #25

# Rebase 69b8a18..f0ad517 onto 69b8a18
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out