Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pull keeping local changes

Tags:

git

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

like image 921
Johnny Everson Avatar asked May 02 '12 13:05

Johnny Everson


People also ask

Will git pull keep local changes?

The reason for error messages like these is rather simple: you have local changes that would be overwritten by the incoming new changes that a "git pull" would bring in. For obvious safety reasons, Git will never simply overwrite your changes.

Can you git pull with unstaged changes?

Cannot pull with rebase: You have unstaged changes. Please commit or stash them. Git stash command is kind of commit stored changes to temporary space and clean working copy. Then your working copy will be reverted to HEAD.It is a good place to store uncommitted changes.

Does git pull update all local branches?

git fetch. On its own, git fetch updates all the remote tracking branches in local repository. No changes are actually reflected on any of the local working branches.


2 Answers

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 
like image 58
GoZoner Avatar answered Oct 23 '22 09:10

GoZoner


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.template and add config.php to your .gitignore.

like image 26
KurzedMetal Avatar answered Oct 23 '22 10:10

KurzedMetal