Git

When Git Push Rejected

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:

cd "C:\Users\nazma\Git\demo1"

Then, run the “touch” command to create a new file in the working area:

touch file3.txt

Next, add the newly created file to the tracking area by using the “git add” command:

git add file3.txt

Now, save staged changes into the Git repository through the “git commit” command along with the commit message by using the “-m” option:

git commit -m "file3.txt added"

After that, check the Git remote URL by running the “git remote -v” command:

git remote -v

Now, push the local branch changes to the remote branch with the help of the “git push” command:

git push --mirror origin

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:

git switch main

Then, again run the “git push” command to push the duplicate copy of our local repository to the remote repository:

git push --mirror origin

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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.