29
20

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 2016-03-10

TL;DR

現在のブランチを、あるブランチで上書きするには

$ git reset --hard 上書き元ブランチ

詳細

例えばフォークした個人用リポジトリー (例 github.com/phanect/some-repo) の master を、フォーク元 (例: github.com/growaspeople/some-repo) の master で上書きしたい (全く同じ状態にしたい) 時。

普段は git rebase を使って以下のようにやっていました。

$ git remote add upstream git@github.com:growaspeople/some-repo.git
$ git fetch upstream
$ git rebase upstream/master

しかし、時としてコンフリクトが発生してしまうことがあります。この場合、余計な merge コミットが入ってしまいます。

$ git rebase upstream/master
First, rewinding head to replay your work on top of it...

...

CONFLICT (content): Merge conflict in data/app_mobile/Controller/EntryController.php
Failed to merge in the changes.
Patch failed at 0002 Throw exception object so not only traces are logged
The copy of the patch that failed is found in:
   /path/to/some-repo/.git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

そういった際には、

$ git remote add upstream git@github.com:growaspeople/some-repo.git
$ git fetch upstream
$ git reset --hard upstream/master

としてあげると、現在のブランチの内容は完全に破棄され、upstream/master の内容で上書きされます。

29
20
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
29
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?