Cannot push to GitHub - keeps saying need merge

后端 未结 30 2265
天命终不由人
天命终不由人 2020-11-22 00:09

I\'m new to GitHub. Today I met some issue when I was trying to push my code to GitHub.

Pushing to git@github.com:519ebayproject/519ebayproject.git
To git@gi         


        
相关标签:
30条回答
  • 2020-11-22 00:46

    I mentioned this in my tutorial, How To Use GitHub: A tutorial for beginners.

    When you create a new repository on GitHub, GitHub may ask you to create a readme file. If you create a readme file directly on GitHub, then you will need to first make a ‘pull’ request before the ‘push’ request will be successful. These commands will ‘pull’ the remote repository, merge it with your current files, and then ‘push’ all the files back to GitHub:

    git pull https://github.com/thomas07vt/MyFirstRepo.git master
    
    git push https://github.com/thomas07vt/MyFirstRepo.git master
    
    0 讨论(0)
  • 2020-11-22 00:47

    Just had the same issue but in my case I had typed the wrong branch on the remote. So, it seems that is another source of this issue... double check you're pushing to the correct branch.

    0 讨论(0)
  • 2020-11-22 00:47

    Another solution is to advance the head of the remote by making another commit if you can. After you pull this advanced head into the local subtree then you will be able to push from it again.

    0 讨论(0)
  • 2020-11-22 00:47

    Another option: locally rename your branch to something new.

    You will then be able to push it to the remote repository, for example if that is your way of keeping a copy (backup) and making sure nothing gets lost.

    You can fetch the remote branch to have a local copy and examine the differences between (i) what the remote had (with the old branch name) and (ii) what you have (with the new branch name), and decide what to do. Since you weren't aware of the remote's differences in the first place (hence the problem), simply merging or forcing changes somewhere is far too brutal.

    Look at the differences, pick which branch you want to work on, cherry pick changes you want from the other branch, or revert changes you don't want on the branch you've got etc.

    Then you should be in a position to decide whether you want to force your clean version onto the remote, or add new changes, or whatever.

    0 讨论(0)
  • 2020-11-22 00:48

    If you are certain that no one made changes to your git repository and that you are working on the latest version, git pull doesn't make sense as a solution in your heart...

    Then this is probably what happened, you used git commit --amend

    It lets you combine staged changes with the previous commit instead of committing it as an entirely new snapshot. It can also be used to simply edit the previous commit message without changing its snapshot.

    ATLASSIAN tutorial: rewriting history

    However, it is not recommended to perform git commit --amend if you have already pushed the commit to GitHub, this is because "amending doesn’t just alter the most recent commit—it replaces it entirely. To Git, it will look like a brand new commit" which means to other developer on your GitHub, history looks like A->B->C but to you it looks like A->B->D, if GitHub let you push, everyone else will have to manually fix their history

    This is the reason why you get the error message ! [rejected] master -> master (non-fast-forward), if you know that no one has pulled your latest change, you can do git push --force, this will alter the git history in your public repo. Otherwise...you can perform git pull, but I believe this will have the same result as you didn't go through git commit --amend,it will create a new commit (ie: git history after git pull: A->B->C->D)

    for more detail: How to change your latest commit

    0 讨论(0)
  • 2020-11-22 00:49

    git push -f origin branchname

    Use the above command only if you are sure that you don't need remote branch code otherwise do merge first and then push the code

    0 讨论(0)
提交回复
热议问题