git pull 保持本地更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10414769/
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-10 13:35:15  来源:igfitidea点击:

git pull keeping local changes

git

提问by Johnny Everson

How can I safely update (pull) a git project, keeping specific files untouched, even if there's upstream changes?

我如何安全地更新(拉取)一个 git 项目,保持特定文件不变,即使有上游更改?

myrepo/config/config.php

myrepo/config/config.php

Is there a way, of, even if this file was being changed on remote, when I git pull, everything else is updated, but this file is unchanged (not even merged)?

有没有办法,即使这个文件被远程更改,当我 git pull 时,其他所有内容都会更新,但这个文件没有改变(甚至没有合并)?

PS. I need to do what I am asking because I'm only writing git-based deploy scripts. I cannot change config files to templates.

附注。我需要做我所要求的,因为我只编写基于 git 的部署脚本。我无法将配置文件更改为模板。

so, I need way to write update scripts that does not lose what was locally changed. I was hoping for something as simple as:

因此,我需要编写不会丢失本地更改内容的更新脚本的方法。我希望像这样简单的事情:

git assume-remote-unchanged file1
git assume-remote-unchanged file2

then git pull

然后 git pull

回答by GoZoner

There is a simple solution based on Git stash. Stash everything that you've changed, pull all the new stuff, apply your stash.

有一个基于 Git stash 的简单解决方案。把你改变的所有东西都藏起来,拉出所有的新东西,应用你的藏品。

git stash
git pull
git stash pop

On stash pop there may be conflicts. In the case you describe there would in fact be a conflict for config.php. But, resolving the conflict is easy because you know that what you put in the stash is what you want. So do this:

在 stash pop 上可能会有冲突。在您描述的情况下,实际上会发生冲突config.php。但是,解决冲突很容易,因为您知道放入藏匿处的就是您想要的。所以这样做:

git checkout --theirs -- config.php

回答by KurzedMetal

If you have a file in your repo that it is supposed to be customized by most pullers, then rename the file to something like config.php.templateand add config.phpto your .gitignore.

如果您的 repo 中有一个文件,它应该由大多数 puller 自定义,那么将该文件重命名为类似的名称config.php.template并添加config.php到您的.gitignore.

回答by Mark Longair

Update: this literally answers the question asked, but I think KurzedMetal's answeris really what you want.

更新:这从字面上回答了所提出的问题,但我认为KurzedMetal 的答案确实是您想要的。

Assuming that:

假如说:

  1. You're on the branch master
  2. The upstream branch is masterin origin
  3. You have no uncommitted changes
  1. 你在树枝上 master
  2. 上游分支masterorigin
  3. 您没有未提交的更改

.... you could do:

....你可以这样做:

# Do a pull as usual, but don't commit the result:
git pull --no-commit

# Overwrite config/config.php with the version that was there before the merge
# and also stage that version:
git checkout HEAD config/config.php

# Create the commit:
git commit -F .git/MERGE_MSG

You could create an alias for that if you need to do it frequently. Note that if you have uncommitted changes to config/config.php, this would throw them away.

如果您需要经常这样做,您可以为此创建一个别名。请注意,如果您对 进行了未提交的更改config/config.php,这会将它们丢弃。

回答by Bhavesh Maniya

We can also try git pull with rebase

我们也可以尝试使用 rebase git pull

git pull --rebase origin dev

回答by Pierre-Olivier Vares

To answer the question : if you want to exclude certain files of a checkout, you can use sparse-checkout

回答问题:如果要排除结帐的某些文件,可以使用sparse-checkout

1) In .git/info/sparse-checkout, define what you want to keep. Here, we want all (*) but (note the exclamation mark) config.php :

1) 在 中.git/info/sparse-checkout,定义您要保留的内容。在这里,我们想要所有 (*) 但(注意感叹号) config.php :

 /*
 !/config.php

2) Tell git you want to take sparse-checkout into account

2) 告诉 git 你想考虑 sparse-checkout

 git config core.sparseCheckout true

3) If you already have got this file locally, do what git does on a sparse checkout (tell it it must exclude this file by setting the "skip-worktree" flag on it)

3)如果你已经在本地得到了这个文件,做 git 在稀疏结账时所做的事情(告诉它必须通过在它上面设置“skip-worktree”标志来排除这个文件)

git update-index --skip-worktree config.php

4) Enjoy a repository where your config.php file is yours - whatever changes are on the repository.

4) 享受一个你的 config.php 文件是你的存储库 - 无论存储库上有什么变化。



Please note that configuration values SHOULDN'T be in source control :

  • It is a potential security breach
  • It causes problems like this one for deployment

This means you MUST exclude them (put them in .gitignore before first commit), and create the appropriate file on each instance where you checkout your app (by copying and adapting a "template" file)

请注意,配置值不应该在源代码管理中:

  • 这是一个潜在的安全漏洞
  • 它会导致部署出现这样的问题

这意味着您必须排除它们(在第一次提交之前将它们放在 .gitignore 中),并在您检出应用程序的每个实例上创建适当的文件(通过复制和调整“模板”文件)

Note that, once a file is taken in charge by git, .gitignore won't have any effect.

请注意,一旦文件由 git 负责, .gitignore 将不会产生任何影响。

Given that, once the file is under source control, you only have two choices () : - rebase all your history to remove the file (with git filter-branch) - create a commit that removes the file. It is like fighting a loosing battle, but, well, sometimes you have to live with that.

鉴于此,一旦文件处于源代码控制之下,您只有两个选择 () : - 重新设定所有历史记录以删除文件(使用git filter-branch) - 创建删除文件的提交。这就像打一场失败的战斗,但是,有时你必须忍受它。