简体   繁体   中英

How to safely undo commits that already in remote master branch & move them into another branch?

I'm working on a feature right now which I suppose to do it in a featureX branch BUT I forgot to create that branch and checkout into it. That cause a problem right now because that 2 commits already in my team remote master branch (we're using BitBucket) like so:

在此处输入图片说明

How do I move d40ef79 and 5e13fd7 into featureX branch so that I can continue working on that featureX branch and remove that 2 commits from master branch without losing recent commits created by my teammates ( 29ac4fb..1606c1a )?

I try first option inside this answer and it doesn't work for me. I also try git cherry-pick inside featureX branch (branch off from master) - also doesn't work for me.

The most simple way is to do revert.

git revert <SHA-1>

Undo the git commit. Revert the changes made by the given commit.

reset and force push

# reset the branch to the desired commit
git reset <SHA-1>

There are other ways as well but they are less recommended ( git push -f , git rebase , git filter-branch )


How to add the desired commit into different branch.

git cherry-pick <SHA-1>...<SHA-1>

Apply the change introduced by the commit at the tip of the master branch and create a new commit(s) with this change.

The syntax of the ... is a commit range. grab all commits from start (exclude) to the last one.

在此处输入图片说明


Read out the full git cherry-pick documentation for all the options you can use

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM