どんどん賢くなっていく。
リモートで消したブランチをローカルでも消す
GitのTip。GitHubとか使って開発しているとき、Pull Requestがマージされてブランチも消したらローカルのブランチも消したい。この作業、これまでは一つ一つブランチを調べてローカルでコマンド打って消していたんですが、そんなことしなくても良かった。
$ git fetch --prune
でOK。
今まで手作業でしていたことは何だったんだ。
どんどん賢くなっていく。
GitのTip。GitHubとか使って開発しているとき、Pull Requestがマージされてブランチも消したらローカルのブランチも消したい。この作業、これまでは一つ一つブランチを調べてローカルでコマンド打って消していたんですが、そんなことしなくても良かった。
$ git fetch --prune
でOK。
今まで手作業でしていたことは何だったんだ。
今日は打ち合わせもないし、じっくりとドキュメントを読んだりコード書いたりするぞ!って思っていたのに色々とトラブルが...
今までGitでconflictした場合ちまちまと手で直していたんですが、「自分のもので上書きしたい」とか「リモートに上がっているもので上書きしたい」という場合にはgit checkout --ours
やgit checkout --theirs
が使えるよということを知りました。きっかけは以下のQiitaの記事。
自分でもやってみました。
まずは、準備。2つのブランチで同じファイルを編集・コミットしておく。
$ mkdir git_checkout_test $ cd git_checkout_test/ $ git init Initialized empty Git repository in /Users/miyohide/work/tmp/git_checkout_test/.git/ $ git commit --allow-empty -m "initial commit" [master (root-commit) f46fa43] initial commit $ touch file $ git add . $ git commit -m "add file" [master 1b32c4a] add file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 file $ git checkout -b a_branch Switched to a new branch 'a_branch' $ echo "a branch 1" >> file $ git commit -a -m "a branch commit 1" [a_branch aea8c0c] a branch commit 1 1 file changed, 1 insertion(+) $ echo "a branch 2" >> file $ git commit -a -m "a branch commit 2" [a_branch 76fb789] a branch commit 2 1 file changed, 1 insertion(+) $ echo "a branch 3" >> file $ git commit -a -m "a branch commit 3" [a_branch 1321b44] a branch commit 3 1 file changed, 1 insertion(+) $git checkout -b b_branch master Switched to a new branch 'b_branch' $ echo "b branch 1" >> file $ git commit -a -m "b branch commit 1" [b_branch ec7500b] b branch commit 1 1 file changed, 1 insertion(+) $ echo "b branch 2" >> file $ git commit -a -m "b branch commit 2" [b_branch 7242dbc] b branch commit 2 1 file changed, 1 insertion(+) $ echo "b branch 3" >> file $ git commit -a -m "b branch commit 3" [b_branch 96e1d8a] b branch commit 3 1 file changed, 1 insertion(+) $ git checkout master Switched to branch 'master' $ git log --graph --all --format="%x09%x09%h %d %s" * 96e1d8a (b_branch) b branch commit 3 * 7242dbc b branch commit 2 * ec7500b b branch commit 1 | * 1321b44 (a_branch) a branch commit 3 | * 76fb789 a branch commit 2 | * aea8c0c a branch commit 1 |/ * 1b32c4a (HEAD -> master) add file * f46fa43 initial commit $
ここでa_branch
にb_branch
のものをmergeしようとするとconflictを起こします。
$ git checkout a_branch Switched to branch 'a_branch' $ git merge b_branch Auto-merging file CONFLICT (content): Merge conflict in file Automatic merge failed; fix conflicts and then commit the result. $ cat file <<<<<<< HEAD a branch 1 a branch 2 a branch 3 ======= b branch 1 b branch 2 b branch 3 >>>>>>> b_branch $
ここで今回学んだこと。git checkout --ours
を使うと、自分たちのもの、つまりa_branch
のものが採用されます。
$ git checkout --ours file $ cat file a branch 1 a branch 2 a branch 3 $
一方、git checkout --theirs
を使うと、他方の、つまりb_branch
のものが採用されます。
$ git checkout --theirs file $ cat file b branch 1 b branch 2 b branch 3 $
地味にTypoが多くなった気がする。
今日めっちゃはまったこと。
irb(main):009:0> e = Time.parse('2019-04-1 0:0:0') => 2019-04-01 00:00:00 +0900 irb(main):010:0> s = Time.parse('2019-03-1 0:0:0') => 2019-03-01 00:00:00 +0900 irb(main):011:0> s + 1.month == e => true irb(main):012:0>
ここまでは普通のことなんだけれども...
irb(main):012:0> (e - s) == 1.month => false irb(main):013:0>
え?なんで?
どうもe-s
のときの右辺にある1.month
は30日で計算しているみたい。一方でs + 1.month
のときの1.month
は単に月の数字を1つ足しているように見える。
明日ちゃんとソース読んでおこう。
出勤すると何故か仕事が降って湧いてくるのはなんでだろう。
ネタ記事だけれども。
忙しさにかまけてやる気が迷子な気がする。
Go言語のお勉強として、いろいろとコードを読んでいるんだけれども、以下のサイトに書かれているコードを読んではて?と思った。
func newHandler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "from pid %d.\n", os.Getpid()) }) return mux }
戻り値としてhttp.Handler
を指定しているのに、return
としてhttp.NewServeMux()
が返しているServeMux
を返すことができるんだろう...。
そんなことをウンウン悩んでいたら、以下のようなアドバイスを頂いた。鍋谷さん、ありがとうございます。
http.NewServeMux が返すServeMux型が、http.Handler interface が要求する関数 ServeHTTP(ResponseWriter, *Request) を実装しているからです。
— 鍋谷 武典 (@Nabetani) February 17, 2019
で、質問に答えてます?
なるほど。確かにhttp.Handler
の定義を見ると、
type Handler interface { ServeHTTP(ResponseWriter, *Request) }
とある。
一方で、ServeMux
型はServeHTTP
を実装している。
なるほど。Go言語、全然理解できてなかった。