0

I was working on two different branches A and B and I had one prod branch MASTER, I created a new branch C combining A and B.

I have accidentally pushed Cto MASTER, and now I do not know what to do how to go back to the prod state, as there are too many commits in place.

commit 92e15c92d543e436bd5804ab6c9
Author: user_A
Date:   Thu Sep 22 15:51:47 2016 +0530

    fixing user report

commit dfd1f4e0d56f50d263d6e
Merge: 1a6bc83 f9b0a35
Author: user_A
Date:   Thu Sep 22 15:47:01 2016 +0530

    Merge branch 'MASTER' of git_repo_branch into B

commit 1a6bc83c55cf1b3d7f88d
Author: user_A
Date:   Thu Sep 22 15:46:46 2016 +0530

    fixing user report

commit dd11a998f380c70b579d2a0
Author: user_A
Date:   Thu Sep 22 13:09:29 2016 +0530

    fixing rake task

commit c194f900e58f93b0a06ed
Author: user_A
Date:   Thu Sep 22 13:08:40 2016 +0530

    fixing rake task

commit f9b0a35430d29826622d95
Author: user_B
Date:   Thu Sep 22 11:36:50 2016 +0530

    jobs save as draft fix

commit 3a1b233b365a96886e2d73
Merge: 2eac96c c4f61b1
Author: user_A
Date:   Thu Sep 22 11:35:20 2016 +0530

    Merge branch 'A' into B

commit dfd1f4e0d56f50d263d6eb5
Merge: 1a6bc83 f9b0a35
Author: user_A
Date:   Thu Sep 22 15:47:01 2016 +0530

    Merge branch 'MASTER' of git_repo_branch into B

commit 1a6bc83c55cf1b3d7f88da697411d5c5acb5b9c4
Author: user_A
Date:   Thu Sep 22 15:46:46 2016 +0530

    fixing user report

commit dd11a998f380c70b579d2a0b
Author: user_A
Date:   Thu Sep 22 13:09:29 2016 +0530

    fixing rake task

commit c194f900e58f93b0a06eddf8
Author: user_A
Date:   Thu Sep 22 13:08:40 2016 +0530

    fixing rake task

commit f9b0a35430d29826622d9
Author: user_B
Date:   Thu Sep 22 11:36:50 2016 +0530

    jobs save as draft fix

commit 3a1b233b365a96886e2d7334
Merge: 2eac96c c4f61b1
Author: user_A
Date:   Thu Sep 22 11:35:20 2016 +0530

    Merge branch 'A' into B

commit c4f61b151c28ce36b0c7987276df90ddc91c84fc
Merge: 0f18c62 fb70961
Author: user_A
Date:   Thu Sep 22 11:35:04 2016 +0530

commit f9b0a35430d29826622d950b9e149c05cf937644
Author: user_B
Date:   Thu Sep 22 11:36:50 2016 +0530

    jobs save as draft fix

commit 3a1b233b365a96886e2d73
Merge: 2eac96c c4f61b1
Author: user_A
Date:   Thu Sep 22 11:35:20 2016 +0530

Author: user_A
Date:   Thu Sep 22 13:08:40 2016 +0530

    fixing rake task

commit f9b0a35430d29826622d950b9
Author: user_B
Date:   Thu Sep 22 11:36:50 2016 +0530

    jobs save as draft fix

commit 3a1b233b365a96886e2d733403fa66e8dd4992fc
Merge: 2eac96c c4f61b1
Author: user_A
Date:   Thu Sep 22 11:35:20 2016 +0530

    Merge branch 'A' into B

commit c4f61b151c28ce36b0c79872
Merge: 0f18c62 fb70961
Author: user_A
Date:   Thu Sep 22 11:35:04 2016 +0530

    Merge branch 'MASTER' of git_repo_branch into A

commit 0f18c62cbae31b5f6a76d
Author: user_A
Date:   Thu Sep 22 11:34:06 2016 +0530

    changing name to slug

commit 56fbcd2e544d97aef2bb
Author: user_A
Date:   Thu Sep 22 11:28:38 2016 +0530

    changing name to slug

commit 2eac96cc7c56aa1f3bda29b9257e045c3b923dfe
Merge: 0caa9d1 f794d6e
Author: user_A
Date:   Wed Sep 21 17:08:37 2016 +0530

    added A_to_B

commit 0caa9d1076ee83d53b0586f2e7cd79f16320ac0e
Merge: 5df1d6d fb70961
Author: user_A
Date:   Wed Sep 21 15:49:40 2016 +0530

    removing conflict

commit f794d6ee277f0ba5964dc534c05cfe234761e569
Author: user_A
Date:   Wed Sep 21 15:41:21 2016 +0530

    fixing users name

commit cc97e8749cccdfee47bb67bbe6470639afbd1d28
Author: user_A
Date:   Wed Sep 21 15:15:53 2016 +0530

    raising 404 if user not found

commit fb709619d0a9f39c6b2a2c7b5a08c47835970c0f
Author: user_B
Date:   Wed Sep 21 12:44:32 2016 +0530

    usercount yopwise report

commit a332dfe71fd7de74eadcce43337699bb4205e29d
Author: user_A
Date:   Tue Sep 20 17:29:35 2016 +0530

    code error fix

commit 323fa5ed6b2002a664adcfc1ae6b295198258fe2
Author: user_A
Date:   Tue Sep 20 17:22:50 2016 +0530

    limiting 

commit ea94a85e1ad259162d4a883cda08635697012602
Author: user_A
Date:   Tue Sep 20 17:15:17 2016 +0530
Sahil
  • 3,338
  • 1
  • 21
  • 43
  • make sure other collaborators aren't accessing your wrong push and follow this link to fallback to the last known stable commit -- http://stackoverflow.com/a/6655126/6527796 – not_again_stackoverflow Sep 22 '16 at 11:00
  • Should I do the change in remote repo directly or from my local? – Sahil Sep 22 '16 at 11:01
  • Do the changes to your prod branch. you want to revert pushes to the MASTER right? find out the last stable commit of prod and fallback to it, force push it to overwrite the changes to master. – not_again_stackoverflow Sep 22 '16 at 11:06
  • How can I unmerge the two branches A and B from MASTER? – Sahil Sep 22 '16 at 11:07
  • paste your master branch logs: `git log`, should not be difficult to switch to an earlier commit. – tokhi Sep 22 '16 at 11:09
  • I have added the logs, I want to move to this commit in my remote MASTER branch, `f9b0a35430d29826622d950b9e14`, `jobs save as draft fix`. – Sahil Sep 22 '16 at 11:24
  • @sahil do this in ur master branch - `git push -f origin :master` – not_again_stackoverflow Sep 22 '16 at 11:32
  • @tokhi I have fixed the code in my local branch, how can I push this code to MASTER such that in the remote branch it rollbacks to the push which I am pushing? – Sahil Sep 22 '16 at 11:48
  • @not_again_stackoverflow, normally I push code using this command otherwise it does not work, `git push origin HEAD:MASTER`, how can I use the `git push -f origin :master` command in the same fashion? – Sahil Sep 22 '16 at 11:50
  • Possible duplicate of [Undoing a 'git push'](http://stackoverflow.com/questions/1270514/undoing-a-git-push) – Flows Sep 22 '16 at 11:54
  • `-f` signifies `force push`. We found the head of the last stable commit of your prod branch which we want to fall back to. `git push -f :` will simply negate all your commits ahead of the specified head and pushes it. – not_again_stackoverflow Sep 22 '16 at 11:57
  • @not_again_stackoverflow I created a `new_branch` with the correct code and pushed it to a `new_ branch`, then I pulled that code from `new_branch` to affected branch in remote MASTER repo, and now when I do `git push -f :`, i get `Everything up-to-date`. – Sahil Sep 22 '16 at 12:02

1 Answers1

1

You can roll back to an earlier commit, check git log for more details, then force your commit to the master branch:

git push origin +<shaofcommit>:master

+ will force the commit, or you can also use -f option.

tokhi
  • 21,044
  • 23
  • 95
  • 105