Developers deal with different errors while using Git. Sometimes, when they push the mirror copy of a local repository to the remote repository, they often encounter the Git push “[remote rejected] main” error. This happens when their working branch and the target branch are not the same. So, in order to resolve this error, it is required to work on the same local and remote branches.
This article will demonstrate the solution to fix the Git push “[remote rejected] main” error.
How to Fix Git Push Error “[remote rejected] main”?
To resolve the Git push error, developers need to switch their current working branch using the “git switch” or “git checkout” command.
Here, first, we will show the above-discussed Git push error encounters. Then, we will provide the solution!
Step 1: Redirect to Local Repository
First, switch to the desired local repository through the below-stated command:
Step 2: Verify Remote Origin
Next, ensure whether the local repository is linked to the remote repository or not:
It can be observed that the remote is added to the local repository:
Step 3: Create a New File
Then, run the “echo” command to generate a new file in the directory:
Step 4: Track File
To track the newly created file, add it to the Git staging area:
Step 5: Commit Changes
Type out the “git commit” command with the “-a” option to save the staged changes to the local repository:
Here, the “-a” option is used to automatically stage all the files to be committed and the “-m” flag is used for the commit message:
Step 6: Push Local Changes to Remote Repository
Now, push the local content to the GitHub repository by running the below-provided command:
Here, the “–mirror” option is used to create a mirror(duplicate) copy of the repository with all information:
Note: As you can see, when we tried to push the duplicate copy of the local repository to the remote repository, the “[remote rejected] main” error was encountered.
To resolve this error, follow the provided steps.
Step 7: Switch to Another Branch
Use the “git switch” command and check out to another branch:
Step 8: Push Copy of Local Changes to Remote Server
Now, push the copy of local changes to the remote server:
It can be observed that the Git push error has been removed successfully.
Conclusion
When developers try to push their duplicate local repository to the GitHub repository, they often encounter the Git push “[remote rejected] main” error. This usually happens when you work on different local branches and push local content to different remote branches. To resolve this error, it is needed to work on the same local and remote branches. This article demonstrated the procedure to fix the Git push “[remote rejected] main” error.