git pull - 在再次推送之前合并远程更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4424684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:49:49  来源:igfitidea点击:

git pull - merge remote changes before pushing again

gitgithub

提问by sscirrus

I have recently changed my programming machine, which is leading to some startup issues in getting my new coding source to integrate seamlessly with my existing Github repo and my app on Heroku.

我最近更换了我的编程机器,这导致在让我的新编码源与我现有的 Github 存储库和 Heroku 上的应用程序无缝集成时出现一些启动问题。

I originally used git cloneto clone the Github repo onto my new machine. Since then, something has happened.

我最初习惯于git clone将 Github 存储库克隆到我的新机器上。从那以后,发生了一些事情。

  • When I do git add ., git commit -m "mychanges", and git push, the code gets sent straight to Heroku. It used to get sent to my own Github repo, but I understand that's a simple matter of redefining what is considered 'origin'.
  • After redefining origin to my Github repo's address, and typing git push origin master, I get the following error:
    error: failed to push some refs to '[email protected]:...'
    To prevent you from losinghistory, non-fast-forward updates were rejected. Merge the remote changes ('git pull') before pushing again.
  • 当我执行git add ., git commit -m "mychanges", 和 时git push,代码会直接发送到 Heroku。它曾经被发送到我自己的 Github 存储库,但我明白这是重新定义被认为是“起源”的一个简单问题。
  • 在将 origin 重新定义为我的 Github 存储库地址并键入 后git push origin master,我收到以下错误:
    错误:无法将一些引用推送到 '[email protected]:...'
    为防止您丢失历史记录,非快进更新是拒绝了。在再次推送之前合并远程更改('git pull')。

I am concerned that my last two days of coding will be lost if I do a git pull(I have backed everything up just in case).

我担心如果我这样做git pull(为了以防万一,我已经备份了所有内容),我最后两天的编码将会丢失。

My current idea: do the git pulland manually update the last two days' files from my backup, then finally do my git push. Is this the right way to go, or is there a more elegant solution?

我目前的想法是:git pull从我的备份中手动更新最近两天的文件,然后最后执行我的git push. 这是正确的方法,还是有更优雅的解决方案?

回答by Zeppomedio

git will not overwrite your data unless you use reset. git pullwill take the commits on origin/master(assuming your branch is master) and try to fast-forward your local branch to that point, merging any local changes. If you've committed locally, and those commits are meant to go after any more recent commits on the server, you can do git pull --rebaseinstead.

除非您使用重置,否则 git 不会覆盖您的数据。git pull将接受提交origin/master(假设您的分支是主分支)并尝试将您的本地分支快进到该点,合并任何本地更改。如果您已经在本地提交,并且这些提交是为了在服务器上进行任何最近的提交,那么您可以git pull --rebase改为这样做。