2

I accidentally push branch instead of master.

So is there any way to revert that push so that I can bring back the master code? I am supposed to execute this command to go another branch from master:

git checkout ProductBranch

but accidentally I executed this:

git push origin ProductBranch
Raptor
  • 53,206
  • 45
  • 230
  • 366
Beginner Coder
  • 208
  • 3
  • 11
  • Not clear what was pushed. `origin` refers to main branch in your remote repo, and in most cases it is `master`. What local branch name do you have? And to which branch name did you push it (origin - is not branch name)? – kosist Oct 30 '20 at 06:20

1 Answers1

0
"git push origin ProductBranch"

If ProductBranch was previously pushed, you could force pushed its last state with:

git log ProductBranch
# make sure the history is the correct one, without your new commits
git push --force origin ProductBranch:ProductBranch

If ProductBranch was never pushed before, then you might consider deleting it on the remote side:

git push --delete origin ProductBranch

Then, simply switch to master and push that:

git switch master
git push -u origin master

If you had committed to the wrong branch, you can fix those commits back to master before pushing.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • i was in master and i supposed to hit this "git checkout ProductBranch" but i accidelnlty git push ProductBranch .So i donot want to push not ProductBranch – Beginner Coder Oct 30 '20 at 07:30
  • @BeginnerCoder OK. I have updated the answer accordingly. – VonC Oct 30 '20 at 08:23
  • At first i update my work in branch like ProductBranch and the i checkout into master and git merge ProductBranch and git push origin master but when i hit git push origin ProductBranch instead of git checkout PorductBranch .So i donot know which step i need to follow of yours – Beginner Coder Oct 30 '20 at 09:33
  • Was productBranch pushed before at any time? Do you want to see a branch productBranch on the remote side? – VonC Oct 30 '20 at 09:37
  • Yes ProductBranch was Pushed after i completed my given task then i execute git checkout master and git merge ProductBranch and again git push origin master. but mistaleky i executed git push origin ProductBranch in master so i just need to undo my master – Beginner Coder Oct 30 '20 at 09:51
  • i am just worried that my whole project will get messed up so . i hope you understand my problem – Beginner Coder Oct 30 '20 at 09:52
  • @BeginnerCoder Then you should follow the push --force option, provided git log origin/productBranch shows you the right commits (the one you originally pushed, before pushing master by error onto that branch). – VonC Oct 30 '20 at 10:15
  • So it will undo git push origin PurchaseBranch ? – Beginner Coder Oct 30 '20 at 10:27
  • @BeginnerCoder If your local PurchaseBranch is correct, force pushing it as shown in the answer will indeed "undo" (overwrite) the push done from master to PurchaseBranch by mistake. – VonC Oct 30 '20 at 10:28