LoginSignup
35
31

More than 5 years have passed since last update.

Gitの使い方備忘録 (git merge --squash)

Posted at

はじめに

いい感じにGitを使うための備忘録。
Githubにリポジトリを置いときたいけど、細かいコミットは上げたくない人用。

流れ

  1. topic branchを切ってその下にさらにtopic-develop branchを切る
  2. topic-develop branchはローカルだけに置いといて、適当にコミットを生やす
  3. いい感じのところでtopic branchにgit merge --squashする。

こうすると2.の部分でコミットメッセージに悩まなくて済む。
2.で生やしたコミットは3.でSquashされる(まとめられる)ので、
Githubから見たときに変にコミットが増えなくて済む。

いい感じ。

topic branch

2つのbranchを切る。

$ git branch
* master
$ git checkout -b topic-branch
Switched to a new branch 'topic-branch'
$ git checkout -b topic-branch-dev
Switched to a new branch 'topic-branch-dev'
$ git branch
  master
  topic-branch
* topic-branch-dev

コミットを生やす

topic-branch-devへのコミットはフランクにしていい。
とりあえずコンパイルが通ったコードは(どんなものであれ)ここにコミットするようにしてる。

$ touch hoge
$ git add .
$ git commit -m "ほげした"
[topic-branch-dev 3321e9d] ほげした
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hoge
$ 
$ touch fuga
$ git add .
$ git commit -m "ふがした"
[topic-branch-dev db4e233] ふがした
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 fuga

topic-branch に merge --squash

$ git checkout topic-branch
Switched to branch 'topic-branch'
$ git merge --squash topic-branch-dev
Updating 0332922..db4e233
Fast-forward
Squash commit -- not updating HEAD
 fuga | 0
 hoge | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 fuga
 create mode 100644 hoge

この状態ではまだコミットされないので、git commitすることによってsquash mergeが完了する。

$ git commit

コミットメッセージは、1行目にかっこいいコミットメッセージを書いて、
それ以下は自動でててくるやつをそのまま残しておく。

Implement hoge & fuga

Squashed commit of the following:

commit db4e233fb2f58211d2364dca66afc0112470cda5
Author: Kenta Hamanaka <hamanaka@cs.info.mie-u.ac.jp>
Date:   Wed Feb 8 12:31:18 2017 +0900

    ふがした

commit 3321e9d63eef8f956073a72bd6170ac6a4e0c373
Author: Kenta Hamanaka <hamanaka@cs.info.mie-u.ac.jp>
Date:   Wed Feb 8 12:30:58 2017 +0900

    ほげした

そしてこれをpush。

$ git push origin topic-branch
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 1.02 KiB | 0 bytes/s, done.
Total 7 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To git@github.com:kentahama/test.git
   0cd8dc9..cc82330  topic-branch -> topic-branch

まとめ

git merge --squashが便利だった。

開発端末にしかtopic-branch-devが残らないので、逆に言うと、他の端末から、mergeしてないtopic-branch-devのコミットを引き継ぎたいみたいなことはできない。
そういう時は両方の端末からアクセスできるリモートサーバでgit init --bareしてそこにpushすればいい。もしくはgithubにプライベートリポジトリを作ろう。なんか本末転倒な気がする(?)

35
31
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
35
31