git-en

What to do when you commit to master by mistake in Git

0
0

Getting into trouble with Git

When you start a new implementation in git, you usually create a new branch, checkout to it, and start working on it. Then, when you're done, you commit and push to the new branch.

But sometimes, you forget to create a new branch. Here's how to deal with it.
This is a workaround for that.

The idea is to move the commit to the newly created branch (while keeping it) and revert the master branch to the previous commit.

The flow is as follows

Translated with www.DeepL.com/Translator (free version)

 

Implemented in master

↓

Commit and push to master

↓

Delete

↓

Create a new branch and checkout

↓

Checkout

↓
Return to master

↓

Return to master

↓
reset in master

 

Create a new branch

After you coomit to master, you can create a new branch

 

git checkout -b NewBranch
Switched to a new branch 'NewBranch'

 

Check NewBranch

A new branch called NewBranch has been created, and you can see that you are now on the master branch

 

git branch
NewBranch
* master

 

Checkout and return to master.

 

At this point, master and NewBranch should be in the exact same state.

From this state, let's go back to master with checkout.

 

git checkout master

 

Resetting while in the master branch

When you get back to master by checkout, use git reset to cancel the master commit.

 

git reset --hard HEAD~1

Done!

Now, NewBranch will have the latest commit, and master will have the previous commit.

Pocket
LinkedIn にシェア

りょすけ
毎月3万人以上のプログラマーが訪れるoff.tokyoで、プログラマーさん向けのアンケートを実施しています、アンケートに無料回答すると、他の人の投票も見ることが出来ます!

学びたいプログラミング言語は何ですか?

 vaaaval@gmail.com



  • この記事を書いた人
  • 最新記事

藤沢瞭介(Ryosuke Hujisawa)

りょすけと申します。18歳からプログラミングをはじめ、今はフロントエンドでReactを書いたり、AIの勉強を頑張っています。off.tokyoでは、ハイテクやガジェット、それからプログラミングに関する情報まで、エンジニアに役立つ情報を日々発信しています!

-git-en

Copyright© off.tokyo , 2021 All Rights Reserved Powered by AFFINGER5.