master (fetch first)" error when pushing to GitHub?">
Software Development

How do I resolve the "rejected master -> master (fetch first)" error when pushing to GitHub?

LI Asked by Lisa Scott · 05-01-2024
0 upvotes 15,894 views 0 comments
The question

I am trying to push my local commits to a remote repository, but I keep getting an error stating that the push was rejected because the remote contains work that I do not have locally. This usually happens when I've made changes on another machine or a teammate has merged a pull request. What is the safest way to integrate these remote changes into my local branch without causing major merge conflicts or losing my recent work?

3 answers

0
SA
Answered on 07-01-2024

This error occurs because your local branch is behind its remote counterpart, and Git prevents you from overwriting history. The standard solution is to perform a git pull to fetch and merge the remote changes into your local branch. However, to keep a clean, linear project history, many developers prefer using git pull --rebase. This command takes your local commits, temporarily sets them aside, applies the remote commits, and then re-applies your commits on top of the new head. If there are conflicts, Git will pause and ask you to resolve them manually before finishing the rebase process. Once the rebase is complete, you will be able to push your code successfully.

0
CH
Answered on 09-01-2024

Are you working on a shared branch where others are pushing frequently, or is this a solo project where you might have edited a file directly through the GitHub web interface?

TH 11-01-2024

Charles, that's exactly what happened. I updated the README.md file directly on the GitHub website and forgot to pull those changes to my local machine before I started my new feature. I was worried that a simple pull might create a messy merge commit. Would you suggest using the rebase method Sandra mentioned, or is there a way to just "force" my local version to be the master version if I know for a fact the web edit was insignificant?

0
ST
Answered on 13-01-2024

You should run git fetch origin followed by git merge origin/master. This updates your local tracking branches and merges the work before you attempt to push again.

LI 15-01-2024

I agree with Steven. Explicitly fetching and merging gives you more control over the process than a blind pull. It allows you to see exactly what changed on the remote before you integrate it.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

master (fetch first)" error when pushing to GitHub?">
Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session