5

I'm trying to push my code to my remote branch but keep receiving this error:

! [rejected] (non-fast-forward)
error: failed to push some refs to 'git@github.com:
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details

I think what may have caused this was the fact that I fixed a typo in my README.md on github instead of through my local environment.

I've looked all over stack overflow and tried the commands they mentioned for this problem such as git pull , git pull --rebase , git pull origin master , git push --f and many others with no avail.

I've also tried the answers in the following questions, with no success:

Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g [duplicate]

Cannot push to GitHub - keeps saying need merge

Git pull a certain branch from GitHub

I also tried git pull upstream master and I get this error:

fatal: 'upstream' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Molly
  • 1,887
  • 3
  • 17
  • 34
Meikay
  • 79
  • 7
  • 1
    so what error do you get when `git pull` ? – ymonad Jan 28 '19 at 01:31
  • Possible duplicate of [Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g](https://stackoverflow.com/questions/25237959/updates-were-rejected-because-the-tip-of-your-current-branch-is-behind-hint-its) – Matt Ke Jan 28 '19 at 01:32
  • I get this `Already up-to-date`. but it won't let me push the code. – Meikay Jan 28 '19 at 01:34
  • Have added a potential solution which is commonly run into. Let me know if that worked or need elaboration. – Siavas Jan 28 '19 at 02:05
  • I followed to steps and when I merged it it said already up-to-date. But it didn't update anything. – Meikay Jan 28 '19 at 02:25
  • From the log, we can see the local branch doesn't have any commits new to the remote branch. In this case, you can't push the local branch to update the remote branch because you don't provide anything new. Maybe you didn't make any new commits. Maybe you made some, but they are not on the current branch. – ElpieKay Jan 28 '19 at 02:33
  • 3
    Possible duplicate of [Cannot push to GitHub - keeps saying need merge](https://stackoverflow.com/questions/10298291/cannot-push-to-github-keeps-saying-need-merge) – phd Jan 28 '19 at 04:37
  • 1
    @ElpieKay that part is actually confusing as even if there are new commits on remote and we didn't `fetch` yet, it will not say that the branches have diverged. I have tried this just before answering. – Siavas Jan 28 '19 at 13:43
  • I resolved it, for some reason the merge conflict wasn't showing up until I made another change to the file and then I did `git stash` and then followed on from there to push my code. – Meikay Jan 28 '19 at 15:37

3 Answers3

1

Problem resolved:

I added another change and saved it in my local environment and did the following:

git stash

git pull origin <branch-name> -v

git add .

git commit

git push origin <branch-name>

Meikay
  • 79
  • 7
0

First of all use command to take backup of your code : git stash.

Then use command git pull or git pull upstream master if you have any upstream set (I assume you have).

Merge the changes from master to your local Git merge upstream/master.

git push

Now if you open your branch in browser you will see the message that “your branch is in sync with master”. ** It should always remain in sync. **

Now use command git stash pop to get back your changes and commit.

Josh Abraham
  • 959
  • 1
  • 8
  • 18
Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33
0

Based on your description I have the assumption that you are on a different branch that was derived from master, and master is set to track origin/master.

Try switching back to it first:

git checkout master
git fetch
git pull origin master

You might have to merge your changes as there has been a new commit on the remote and you have something new on your side as well.

Siavas
  • 4,992
  • 2
  • 23
  • 33