14
9

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でローカルリポジトリのmasterを削除し、origin/masterから再度masterを作成する

Posted at

masterブランチに要らないコミットをしてしまったときの対処法などとして使えます。
※masterブランチが汚くなってしまったときの最終手段です。

master以外のブランチにcheckoutし、masterを削除。

git branch -D master

origin/masterが最新かどうかを確認する。

git log origin/master

最新でない場合はfetchする。

$ git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
From github.com:XXXXXXX/XXXXXXX
   04b9e144..19268083  master     -> origin/master

再度ローカルリポジトリのmasterを作成し、checkoutする。

$ git checkout -b master origin/master
Switched to a new branch 'master'
Branch master set up to track remote branch master from origin.
14
9
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
14
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?