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 3 years have passed since last update.

masterにコミットしてしまった修正をブランチに移動したい

Last updated at Posted at 2021-01-10

目的

ブランチを切って作業すべきところを、間違えてmaster上で作業してコミットしてしまった。

 $ git branch
* master
 $ git log --oneline 
9491e2d add Z
877a87e add Y
5301865 add X
0846a50 initial commit

このままだと pull requestも出せないし、自部署の gitリポジトリは masterへの pushを禁じているのでどうにかしたい、という話

手順

masterからブランチ branchXYZを作成。

 $ git checkout -b branchXYZ
 $ git log --oneline 
9491e2d add Z
877a87e add Y
5301865 add X
0846a50 initial commit

次にmasterのコミットを巻き戻す

 $ git checkout master
 $ git reset --hard HEAD~3
HEAD is now at 0846a50 initial commit
 $ git log --oneline    
0846a50 initial commit

あとは branchXYZを pushして pull requestを出せば解決

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?