Home > front end >  git push origin in another remote branch by mistake
git push origin in another remote branch by mistake

Time:10-22

by mistake I pushed some commits using:

git push origin local_branch_name

but I was not in the main, I was actually in another different branch. Is it possible to correct the mistake? Thanks!

CodePudding user response:

Running:

git push origin foobranch

pushes any required new commits you have, that the Git over on origin lacks, and then asks the Git software on origin to set their branch named foobranch to point to the same commit that is the last commit (the tip commit, to use the Git terminology) of your branch foobranch.

It does not matter whether you are currently "on" branch foobranch locally: the effect of this git push command is to find new foobranch commits—commits you have, but origin does not have—and send those to origin and ask them to create-or-update their foobranch.

You added, in a comment, that:

I mean that before pushing to the origin I did not checkout in the main branch but I was still in another different branch, so now I have two branches with the commits mixed

Had you run git checkout main or git switch main before your git push origin foobranch operation, there would be no difference.

Most Git commits are on multiple branches! This is not an error. A branch name, in Git, simply locates the most recent commit that we wish to say is "on" that branch. All earlier commits reachable from that last commit are also on that branch, even if they are also on some other branch.

  • Related