git push origin testing 

To git@git.site.com: cgp / project.git

! [rejected] testing -> testing (non-fast-forward)

error: failed to push some refs to 'git@git.site.com: cgp / project.git'

hint: Updates were rejected because of a remote branch

hint: counterpart. Check out this branch and integrate the remote changes

hint: (eg 'git pull ...') before pushing again.

hint: See the git push - help for the details.

How to push?
From master'a last changes are poured into my branch of development. I created my own for development, I upload it to a remote branch for testing.

  • one
    I often have this error, moreover, no one except me rules the repositories, and I don’t make edits through the web interface. I solve the problem with the git push -f command. Before that, just in case, I make a backup of the .git folder in the local repository, since the history of commits can be deleted (but it should not, once it was only). git pull , as everyone here advises, will overwrite files on your local repository, files from a remote one having the same names and paths, so I do not use git pull on this error. Thank. - Sasha Chernykh
  • git pull does not grind files :) And you do not accidentally use any source tree / tortolize and similar crafts with hui? - KoVadim
  • @ SashaBlack You are wrong. First, it is better not to do git push -f thoughtlessly. Secondly, for any git push local history cannot be removed, local copies of remote branches can only be rewritten, but you see, you want to do this. Thirdly, git pull does not "rewrite files", it adds a new commit history without deleting the existing one. - Roman
  • @KoVadim, I use plugins for Sublime Text. For most actions, I use the Git package to quickly push- easygit . Thank. - Sasha Chernykh
  • one
    exactly. Many such utilities do in silent synchronization - that is, they do pull. And this may be the cause. But of course it is better to look in the log and find out. - KoVadim

5 answers 5

At the moment you are trying to push into a branch on which you are working not only. You have encountered this problem, as there are changes on the server that you do not have.

Your sequence of actions should be as follows:

  1. Pick up changes from a remote branch:

     git pull 
  2. Further, it is necessary to resolve conflicts (abolish changes), if any. To do this, use the following command:

     git mergetool 
  3. If there were conflicts and you resolved them, then you will need to create a new commit that will contain the correction of conflicts:

     git commit 
  4. You can submit your changes:

     git push 

If you do not have conflicts, then step 2 and 3 can be skipped.

  • 2
    In general, git pull defaults to git fetch + git merge. so either fix the first step, or write in the second that it is executed automatically. - KoVadim
  • @KoVadim, I agree, my mistake. It should be a mergetool :) - Umed
  • How does the git mergetool command work? - Timur Musharapov
  • one
    @TimurMusharapov, to work with Git, for more convenience, you need to configure some kind of utility to merge changes. For example, kdiff3, beyond compare or something from this series. These are utilities that show you visually conflicts, they will show two files side by side and their difference. - Umed
  • Correct all the same rebase . - 0andriy

Someone managed to add changes before you. You need to deflate these changes, integrate them into your branch and then upload it again.

 git pull origin testing разрешение конфликтов (commit) git push origin testing 
  • But will the changes made by the author of the question in the local repository disappear in this case? Thank. - Sasha Chernykh
  • But after all, in the testing branch you can be a lot of superfluous, which is still being tested. - Timur Musharapov
  • @ SashaBlack If you complete the 2nd item, then nothing will be lost. - Mikhail Vaysman
  • @TimurMusharapov you can specify what exactly you want to upload to a branch using git add . Only the specified files and directories will be added to the branch. - Mikhail Vaysman
  • In this case, I’m talking about the git pull command - having done it, will I not upload what I’m testing (a lot of different functionalities) into my dev-branch? - Timur Musharapov

Most likely on the remote branch there are new commits that you lack. It can be solved like this

 git pull origin ветка 

If it does not help (it means there are conflicts), then you can

 git pull --rebase origin ветка 

But this problem could appear if you overwritten something in the history of Git, for example, zakommitel through git --amend or did a rebase or cherry-pick ... In such cases, it is necessary to push changes in repository "forcibly". This can be done in this way.

 git push -f origin ветка 

    If you do not know what has been added to the testing branch (It is possible that the branch may break with other code). Split off a new thread from testing git checkout -b testing2. Therefore, in the testing2 branch, do git pull origin testing. Then the most difficult thing remains - to resolve conflicts and check the performance of your code. Log in your code (git commit -a -m "SOME COMMENT"). If everything works, go to the testing (git checkout testing) branch and download the branch from testing2 (git merge testing2). Since the HEAD testing2 branch doesn’t test the conflicts above.

      In general, such an answer is still possible if a particular branch is protected from push. In this case, the fill in it is possible only through merge requests.