The Git error “updates were rejected because the remote contains work that you do not have locally” is triggered when you initialize a new Github repo with a readme file or a license. This article dives into the solutions to this error so new updates from local machines can be pushed into the Github repository.
- Pull and Push
Sometimes a simple pull from the origin repo branch to the local project to obtain the missing files before a push is all one needs to fix the error. Assuming the branch is the master branch, the below commands will suffice.
git pull origin master git push origin master
- Remote Add, Pull, and Push
To communicate to outside repositories, Git uses the word “remote”. By adding an address we are telling git how to reach a certain repo.
git remote add origin <input github url> git pull origin master --allow-unrelated-histories git push origin master
Notice how we are pulling from master in the second command. This can be done only if you have already initialized a repo in github and have committed changes locally. If not, we can replace the second command with
git pull origin master.
git pull origin master --allow-unrelated-histories command is there if an initialized repo already exists because by default we cannot merge the histories of projects that did not initiate together. This command overrides this protected default.
- Pull, Push, and Force Commit
git push -f is short for
git push --force. This command is used to force Git to push a commit when it would normally otherwise throw an error, an error that could be related to two different projects without the same anester, or projects that began their lives independently from each other. Use caution as this command also overrides the remote history with on a local repo.
git pull origin master git push -f origin master
- Combining Solutions 2 and 3
If none of the solutions above have worked for you yet, try combining both
git pull --allow-unrelated-histories and
git push -f origin master in the command line.
git pull --allow-unrelated-histories git push -f origin master
- Fetching, Sending, and Merging
The final possible solution is to fetch from the remote, send from the local, and attempt to merge the two.
git fetch git branch --set-upstream-to origin/master git merge --allow-unrelated-histories
If an error occurs after
git fetch, try appending the repo name or url to the command.
git branch --set-upstream-to sets the remote branch for the local branch, allowing for future fetch, pulls, pushes and merges. In lieu of set upstream, we can also use the command
git push -u origin where the
-u also means to send upstream.
The Git error “updates were rejected because the remote contains work that you do not have locally” is triggered when we initialize a new Github repo with a readme file or a license. Errors like this are also common when trying to get to repositories who began their lives independently, to communicate with each other. Using a few commands to help override this issue can help the local and remote repos communicate.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.