0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gitについて勉強する。第五弾 (ブランチとマージについて 編)

Last updated at Posted at 2019-10-03

ブランチを作成する

  • git branch "ブランチ名"

作成するだけで、切り替えは行いません。

ブランチの一覧を表示する

  • git branch -a

現在のブランチの一覧を表示します。
自分がいるブランチはアスタリスクが付いて、緑色になります。

ブランチを切り替える

  • git checkout "ブランチ名"

存在するブランチに切り替える

Gitバージョン2.23.0 から「git switch」コマンドが追加されたようです。
これは、「git checkout」にできることが多すぎるから、分割されたということらしいです。(以前の記事参照)

ブランチを新規作成して切り替える

  • git checkout -b "ブランチ名"

ブランチの名前を変更する

  • git branch -m "新ブランチ名"

ブランチを削除する

  • git branch -d "ブランチ名"

masterにマージされてない変更に関しては、削除されません。
強制的に削除する場合は、下記のコマンド。

  • git branch -D "ブランチ名"(強制削除)

マージには3つの種類がある

  1. Fast Forword
    ブランチが枝分かれしていないときに、使用されます。
    ブランチのポインタ(コミット)を一つ前に進めます。

  2. Auto Merge
    一般的なマージ
    ブランチのポインタ(コミット)を一つ前に進めます。

  3. Concreft
    変更が競合したときに、使用するマージ。
    マージした際に、メッセージが出ます。

マージする

  • git merge "ブランチ名"
  • git merge "リモート名/ブランチ名"

コンクリフトの解決

「CONFLICT (content): Merge conflict in "ファイル名"
Automatic merge failed; fix conflicts and then commit the result.」
とメッセージが出るので、対象のファイルを見てみると競合している内容が記載されています。
ファイル内容をきれいにして、コミットしなおせばOKです。
(スクショ忘れた...)

コンクリフトが起きないようにするには

複数人で同じファイルを変更しない。

当たり前ですね。

pullやmergeをする前に、stash、commitをしておいて変更中のファイルをなくす。

最近はIDEが警告を出してくれることが多い気がします。

次・第六弾

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?