Git储藏未提交的文件

5
我有两台服务器用于开发和生产。在开发服务器上,我已经将一个新的分支与原始分支合并。现在我想去生产服务器并拉取这些更改。但是,在生产服务器上,我修改了两个文件,这些文件仅适用于此服务器。
因此,当我执行以下命令时:
git pull
我会得到:
error: Your local changes to the following files would be overwritten by merge:
app/code/local/NextBits/DependentCustomOptionsImage/Block/Adminhtml/Template/Edit/Tab/Options/Option.php
app/design/adminhtml/default/default/layout/dependentcustomoptionsimage.xml
Please, commit your changes or stash them before you can merge

这是我需要的两个文件。

我该如何拉取源代码而不覆盖这些文件?

或者我该如何保留这些文件,拉取更改并将它们还原回去?


https://stackoverflow.com/search?q=%5Bgit%5D+error%3A+Your+local+changes+to+the+following+files+would+be+overwritten+by+merge - phd
@phd,你提到的这个问题对我没有帮助。事实上,我丢失了一些文件。我使用的git是为一个Magento网站准备的,并不是所有的文件都在origin中。 - G. G.
1个回答

6

至少有三种不同的解决方案可以实现这一目标:

  • You can commit your changes on these two files in the production server, before running git pull:

    git add -v -u
    git commit -m "Save prod config"
    git pull
    

    This will typically create a merge commit combining the local branch (with the commit "Save prod config") and the changes from your remote.

    If there is some conflict, you'll just need to resolve it with the help of git status, git diff, and git commit.

    For subsequent updates, it should suffice to run git pull.

  • Like the previous solution, you can commit your changes on the two production config files, but run git pull -r afterwards:

    git add -v -u
    git commit -m "Save prod config"
    git pull -r
    

    The -r option stands for --rebase and means that the local commit(s) specific to the production server will be rebased on top of the pulled commits.

    Subsequent updates can be performed by running git pull -r.

    The main advantage of this solution is just the ability to keep a "linear history": there is no merge commit created and the "Save prod config" commit will always be replayed at the 'end' of the history.

  • or you can use the git stash command (which may have some drawbacks in case of conflict):

    git stash
    git pull
    git stash pop
    

在执行了 git commit -m "Save prod config" 之后,我得到了这样的提示:Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded。为什么会出现这种情况? - G. G.
@G.G. 一般来说,“your branch is behind... and can be fast-forwarded”的信息并不是一个警告,而只是表明“git pull”不会创建合并提交,因为master恰好是origin/master的直接祖先,所以“git pull”只会通过将本地分支移动到最新的上游提交来更新本地分支。然而,根据你在问题中描述的用例,我认为这种情况不应该发生。也许你应该检查是否确实运行了“git add ...”,并且commit已经成功地被创建在正确的分支上。 - ErikMD
你可以使用git fetchgit merge --no-ff --no-commit来查看生产环境的更改,然后再进行更改,而不是直接使用git merge - Christoph
@ErikMD 我明白了。但是我在这个服务器上所做的唯一更改就是我已经提到的文件。如果我快进主分支,会有什么丢失吗? - G. G.
@G.G. 快进合并不会导致文件丢失。通常情况下,使用 Git 时,只要更改已经提交过,就不会丢失更改。例如,您可以在使用 git reflog 检查历史记录后,始终将 HEAD 回滚到某个先前的状态。但是,您在之前的评论中提到您已经丢失了更改(也许是使用了 git checkout -- filenames 命令?这确实是一种破坏性的命令),因此希望您已经保存(提交)了帖子中提到的文件。 - ErikMD
@ErikMD 我使用了git checkout命令导致文件丢失,你是对的。我会保留这两个文件并进行快进以进行检查。 - G. G.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接