Git is the most commonly used version control system that manages the project source code files, such as creating, deleting, updating, removing, pushing, pulling, and fetching changes among local and remote repositories. While performing these tasks, sometimes users often encountered different errors like “[remote rejected] main” which occurs when users are trying to push the local branch change into different remote branches.
This guide will explain when a Git push is rejected.
When Git Push Rejected?
“[remote rejected] main” error often occurred when users push the mirror copy of a local repository to the remote host. One more reason is when users work on a large project, it often happens, someone else pushes a commit to the same branch to which you are pushing changes, but your repository is not updated.
Let’s move ahead and check out the provided procedure to know about when the “Git push rejected” error occurs and how to fix it!
Initially, move to the Git local repository by providing its path along with the “cd” command:
Then, run the “touch” command to create a new file in the working area:
Next, add the newly created file to the tracking area by using the “git add” command:
Now, save staged changes into the Git repository through the “git commit” command along with the commit message by using the “-m” option:
After that, check the Git remote URL by running the “git remote -v” command:
Now, push the local branch changes to the remote branch with the help of the “git push” command:
Here, the “–mirror” option is utilized for making a mirror(duplicate) copy of the repository with all information, and the “origin” is our remote name:
Note: According to the above-given output, our Git push operation has been rejected, and encountered the “[remote rejected] main” error
Now, to resolve the above-encountered error, we will first switch to the “main” branch by executing the “git switch” command:
Then, again run the “git push” command to push the duplicate copy of our local repository to the remote repository:
As you can see, the duplicate copy of our local repository has been pushed successfully:
For verification, redirect to your GitHub repository and check whether changes have been pushed or not:
That’s all! We have described when the “Git push rejected” error comes and how to fix it.
Conclusion
The Git push is often rejected and encounters the “[remote rejected] main” error when users are trying to push a copy of the local repository to the GitHub repository, This usually happens when users work on different local branches and push local content to different remote branches. To fix this error, it is required to work on the same branch remotely as well as locally. This guide illustrated the procedure to resolve the Git push reject error.