简体   繁体   中英

How to roll back all commits from certain branch in GIT?

I created new branch from master on 08/01 "12345-Fix"

  • 08/01 Commit 1 in local
  • 08/02 Commit 2 in local
  • 08/03 Commit 3 in local

I created new branch from/ master on 08/04 "56789-Fix"

  • 08/04 Commit 4 in local
  • 08/05 Commit 5 in local

On 08/05 I pushed the "56789-Fix" branch code to remote master branch so this pushed both commit 4 and commit 5 in master branch.

Started working back on "12345-Fix" on 08/06

  • 08/06 Commit 6 in local
  • 08/07 Commit 7 in local

On 08/07 I pushed the "12345-Fix" branch code to remote master so this pushed all 5 commits (1,2,3,6,7) in master branch.

Now when I looked up in Github, the commit listing is showing as

  • Commit 7 08/07
  • Commit 6 08/06
  • Commit 5 08/05
  • Commit 4 08/04
  • Commit 3 08/03
  • Commit 2 08/02
  • Commit 1 08/01

My Question is if I want to rollback only "12345-Fix" branch code from master branch, how I can do that? I know I can roll back to certain commit "Commit 1", but than it will rollback Commit 4 and Commit 5 for "56789-Fix", which I don't want to roll back.

Any suggestions?

At first rest to HEAD - 2

Remove 6, 7

  1. git reset HEAD~2
  2. git checkout .

Record 4, 5 with git stash

  1. git reset HEAD~1
  2. git stash
  3. git reset HEAD~1
  4. git stash

Reset to the commit before 1 with git reset HEAD~3(Remove 1, 2, 3 )

  1. git reset HEAD~3
  2. git checkout .

Pop recorded commit(4, 5) with git stash pop

  1. git stash pop
  2. git commit -am "commit 4"
  3. git stash pop
  4. git commit -am "commit 5"

git push origin master -f

Since the commits have been pushed, you will need to create one or more new commits that undo the effects of commits 1,2,3,6, and 7. The command for this is git revert .

I would want to do something like

git revert --no-commit 1 2 3 6 7

then check that the changes made by git revert are the ones you intended, and then commit these undos with a normal git commit .

You will need to identify each of the commits properly in the command and cannot use the numbers as shown. I suggest using the first few characters of each of the commit ID's in place of each of the small integers I show above, or you can use revision ranges for example if you feel confident.

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