deleted branch on remote, now cannot push it from local back to remote

Viewed 56

so essentially i made a bunch of accidental commits that got pushed to my remote branch. I wanted to remove them from the history so i used commands:

git push my-branch :master

git reset --hard [COMMIT HASH]

this deleted the branch my-branch on remote, but kept it locally where the head was set to the commit I wanted it to be.

Now when i try to use command

git push my-branch master

I get two errors (not sure if they're related) the first is commiter's email does not follow the pattern. and the second is remote rejected my-branch -> my-branch (pre-receive hook declined)

The commiter's email that the error lists is not my email (who made the commit), When i check the commit details it says i am the author and lists my email, so i'm not sure where it's seeing this other email. I've already tried setting the global config username and email and amending, but nothing changes.

As for the second error, Master is a protected branch, but i have had no problem pulling and pushing this entire time until now.

1 Answers

First of all, as @torek pointed out, the first argument to git push should be a remote (usually origin). You can think of a remote as a nickname for a server (usually when you run git clone <URL>, it sets up origin to be a 'nickname' for <URL>). For git push, this first argument tells git which server to push to. So, git push my-branch :master is actually telling git to delete the master branch on the my-branch server.

Since it's unlikely that you'd have a my-branch remote set up, I'm assuming one of two things happened:

  1. You've mistaken origin for a branch name, and ran git push origin :master. This would have deleted master on the server. At the time, master must not have been a protected branch, or this would have produced an error. Presumably, someone re-created master on the server after you did this, and this time around immediately went and designated master as a protected branch. If this is what happened, you might want to apologize to your team. After addressing the issues with the committer's email (below), you may still have issues that need to be resolved, but there isn't enough information in your question to help with those.
  2. This is a mistake you made writing up the question and what you actually ran was git push origin :my-branch and then git push origin my-branch is what gave the errors.

Now, as for the errors you got:

remote rejected my-branch -> my-branch (pre-receive hook declined)

This indicates that there is some restriction imposed by the server that you're not meeting.

commiter's email does not follow the pattern.

This is a custom error message the server is spitting out to explain WHAT restriction it is that you're not meeting.

When i check the commit details it says i am the author and lists my email

Git commits have separate committer and author information. You can see both by using git show --format=fuller

This allows people to create and push commits from other people without taking credit for the work. This is especially useful in open-source repositories where you might not be given commit/push access until you meet certain criteria (e.g. on LLVM you need to have contributed at least 3 commits before getting commit access).

Another way this is useful is when you edit, rebase or cherry-pick a commit. The author and author date will remain the same to reflect who originally made this change and when, but the committer and commit date will get updated to reflect that it was you who made the edited/cherry-picked/rebased version of it.

The error message might be telling you it needs the COMMITTER email to match the pattern, not the AUTHOR. Assuming there aren't any merge commits between the problematic commit and my-branch, you should be able to update the committer email to your email by doing git rebase --force-rebase <problem commit>~1

Related