git pull keeping local changes
How can I safely update (pull) a git project, keeping specific files untouched, even if there's upstream changes?
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)?
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.
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
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 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:
git checkout --theirs -- config.php
如果你的repo文件中有一个文件应该由大多数文件创建者定制,那么将文件重命名为config.php.template
,并将config.php
文件添加到你的.gitignore
。
Update: this literally answers the question asked, but I think KurzedMetal's answer is really what you want.
Assuming that:
master
master
in origin
.... 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.
下一篇: 保持本地变化