0

Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes (e.g., 'git pull ...') before pushing again.

Yes, I know but that's the point of pushing. I have deleted some files on local branch and need them to be deleted on the remote as well. What's the best way to do so?

  • https://stackoverflow.com/search?q=%5Bgit%5D+Updates+were+rejected+because+the+remote+contains+work+that+you+do+not+have+locally – phd Aug 11 '20 at 12:42

2 Answers2

3

Solution 1(gentle way to do it)

  1. You can call git pull --rebase before you do git push
  2. If git pull can't merge cleanly, you need to fix the conflicts between local and remote repository.
  3. After you fix the conflict and sync the repo, it will be possible to git push cleanly.

Solution 2(brute way to do it)

  1. If you don't care about the difference(e.g. you own the repository and it's only used by yourself), you can do git push --force to overwrite the remote repository.
  2. After you overwrite the remote repository, git pull from another local repo will break, you can use git fetch && git reset --hard origin/master to force sync it.
Kassian Sun
  • 609
  • 4
  • 8
  • Thanks so much! First solution did the trick. I didn't even hear about rebase until now – Оксана Мулярчук Aug 11 '20 at 13:28
  • You might want to take a look at all subcommands supported by git: https://man7.org/linux/man-pages/man1/git.1.html No need to master them or know the details, just get familiar with basic concepts and operations. – Kassian Sun Aug 11 '20 at 14:05
0
  1. stash or commit your changes
  2. Take a pull
  3. if conflicts are found resolve the same
  4. push your changes