1

I keep getting an error over one file after trying to push a repo to my github after a minor change was pushed from another computer, even after pulling the update. Here is the error I get when I try to git push origin master:

 To https://github.com/[me]/[project].git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/[me]/[project].git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

what is going on here?

haybales
  • 15
  • 1
  • 6

2 Answers2

3

This has happened because some changes have been pushed to the remote branch since you last downloaded it. Your local branch was not up-to-date with the remote branch. You have to bring them to the same level and then push your local branch to the remote repository.

This can be done using either of them:

1. Fetch + Rebase:

git fetch
git rebase origin/branch_name

There are 2 steps in this approach. You first fetch all the changes made in the remote repository. After fetching, you then rebase your branch with the remote branch.

2. Fetch + Merge

git fetch
git merge origin/branch_name

In this, you first fetch all the changes but instead of rebasing, you merge the remote changes onto your local branch.

3. Pull

git pull

It is basically git fetch followed by git merge in a single command. You can use this command and git will automatically perform a fetch and then merge the remote changes into your local branch.

4. Pull with Rebase

git pull --rebase origin branch_name

This tells git that rebase be used instead of merge. Git will fetch first and then rebase the remote changes with your local changes.

After this has been done, some conflicts may occur. Resolve this conflicts and then you can push the changes to remote repository using the below command.

git push origin branch_name

For merge vs rebase: When do you use git rebase instead of git merge?

Community
  • 1
  • 1
Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
1

Some changes have been made to the remote codebase (often referred to as origin: this is the local alias for the Github repository) since the last time you downloaded it. That means you have to download the most recent version of the remote code with git fetch and tell Git to integrate the new code with git merge. There is an alias command for these:

git pull

You'll have to resolve any conflict that could arise between the new code from the repo and your fresh edits. Then finally you can:

git push 

to send back to the server the new, edited code to the remote repository.

Pierre Prinetti
  • 9,092
  • 6
  • 33
  • 49