0

I am a relatively new developer. I recently made an application and deployed it using Heroku. It had some bugs, so I fixed them up. Yesterday I entered the usual commands in the terminal; namely git add ., git commit -m "Fixed some bugs", and git push heroku master. When I entered the last command, it gave an error saying

Error: failed to push some refs to '<someUr>l' 
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 searched for this error and found that this problem here was similar. So I did what the answers said; git pull, and git pull heroku master. I would have done these five commands a few times in some order (sorry for the unclearness of the question, this happened yesterday). Then when I once did git push heroku master, it didn't give any errors. So I headed over to my site and it gave me an application error. When I went back and checked my files, I found that they had been modified.

Somehow, >>>>>>>HEAD and a few equal signs and another strange combination which looked like a hash were all around the files. Other than that, it seemed to have added some of my previous code which I had removed.

This was a disaster. At the end, I entered git diff in the terminal and modified 200 lines of code to their previous state checking the differences in the terminal.

Now, after spending some good time, I have deployed it successfully. I want to know what caused this and how to solve it if it happens again. Thanks in advance.

Safwan Samsudeen
  • 1,645
  • 1
  • 10
  • 25
  • See https://stackoverflow.com/q/161813/10871900 – dan1st Jul 29 '20 at 11:07
  • @Safwan Try to run this `git pull heroku master --allow-unrelated-histories`. This command will show/add some unmerged conflicts which you need to resolve and commit again. – Ankit Jindal Jul 29 '20 at 13:31

1 Answers1

2

Somehow, >>>>>>>HEAD and a few equal signs and another strange combination which looked like a hash were all around the files.

These are merge conflicts that you need to resolve manually.
On the remote git repo you might have the history:

A -> B -> C -> D

But locally you only have the git history:

A -> B -> C

When you do git pull your local git repo will synchronize to the remote git. All fine. However if you have done some work your git history looks like:

A -> B -> C -> E

Now if you git pull it cannot tell if D or E is correct. You need to merge it. This leads to:

A -> B -> C -> D -> F
            -> E ^

And here is where you will see >>>>>>>HEAD.

To not let this happen you have to make sure that when you do git pull you either don't have any modified/staged files. (Do git reset, this will get rid of your staged files which haven't been committed to git history.)
And you have to make sure that the local branch you are developing on does not differ in git history. (This can happen if someone else is working on the branch as well.)

Tin Nguyen
  • 5,250
  • 1
  • 12
  • 32
  • I think you're thinking of `git reset` rather than `git revert`, towards the end of your answer. But one must be careful with reset. – torek Jul 29 '20 at 15:50
  • I don't understand what I exactly must do. Can you edit the answer to be a little more concise? I am new to git. – Safwan Samsudeen Jul 30 '20 at 02:30