1

I want to update my repository which isn't tracked, and I made changes to the file locally.

What I did is.

git fetch

git branch -a - List all the branches

git diff --stat --color master origin/master - This compares my local to the remote.

then I do git push which gives me this output.

Updates were rejected because the tip of your branch is behind...

but I don't want to pull the changes from the remote repo, because I made some changes locally. and I want to push my changes to the remote repo.

Guillaume D
  • 2,202
  • 2
  • 10
  • 37
  • 1
    If you dont care about overwriting changes in the remote repo, use `git push -f` but BEWARE: if you dont really understand what you are doing, it could have irreversible consequences. – Ojasvi Monga Jul 18 '19 at 07:29
  • I think branch you are trying to push is behind from the remote branch. You should pull the remote changes and then solve the conflicts if any and then you are good to push. – Let's_Create Jul 18 '19 at 07:32
  • you should first pull, then try pushing – Ojasvi Monga Jul 18 '19 at 07:34
  • Possible duplicate of [Updates were rejected because the tip of your current branch is behind](https://stackoverflow.com/questions/39399804/updates-were-rejected-because-the-tip-of-your-current-branch-is-behind) – phd Jul 18 '19 at 11:53
  • The solution is either to `git push -f` or `git pull`. – phd Jul 18 '19 at 11:54

2 Answers2

0

BEWARE: if you don't really understand what you are doing, it could have irreversible consequences.

If you don't care about overwriting changes in the remote repo, use git push -f

Ojasvi Monga
  • 4,437
  • 2
  • 18
  • 35
0

This happens when there have been changes to both your local branch and the remote branch and the reason why you are not allowed to push your changes to the remote branch is because the remote changes could get overwritten by your local changes if you were to just push the changes.

One way to solve this is to use a feature in git called rebase. What it does is that it replays your changes on top of the remote branch, to ensure that your branch is up-to-date and then builds on the remote branch.

You should be able to solve this problem by updating your local branch by running this command: git pull --rebase. This will pull the remote changes and then replay your local changes on top of those.

You can read more on git-rebase here: https://git-scm.com/book/en/v2/Git-Branching-Rebasing

If, however, you really want to overwrite the remote branch, there is also a --force option to git push. I would not recommend it unless you really know what you are doing. More information on what options are available for pushing can be found here: https://git-scm.com/docs/git-push

Frost
  • 11,121
  • 3
  • 37
  • 44
  • I tried rebase, but I'm still getting error. I fixed it. What I did was to clone the repo and track the changes to it and then paste the changes that I've made to that folder and commit and push the changes. – Aldrin Espinosa Jul 19 '19 at 05:21