git pull keeping local changes

Git

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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.

Solution 3 - Git

I use

git pull -X ours

which will instruct git to use the local version when merging.

Solution 4 - Git

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

Assuming that:

  1. You're on the branch master
  2. The upstream branch is master in origin
  3. You have no uncommitted changes

.... 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.

Solution 5 - Git

We can also try git pull with rebase

git pull --rebase origin dev

Solution 6 - Git

To answer the question : if you want to exclude certain files of a checkout, you can use 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 :

    /* !/config.php

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

    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)

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

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


> 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)

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

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.

Solution 7 - Git

Incase their is local uncommitted changes and avoid merge conflict while pulling.

git stash save
git pull
git stash pop

refer - https://happygitwithr.com/pull-tricky.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnny EversonView Question on Stackoverflow
Solution 1 - GitGoZonerView Answer on Stackoverflow
Solution 2 - GitKurzedMetalView Answer on Stackoverflow
Solution 3 - GitPeter CottonView Answer on Stackoverflow
Solution 4 - GitMark LongairView Answer on Stackoverflow
Solution 5 - GitBhavesh ManiyaView Answer on Stackoverflow
Solution 6 - GitPierre-Olivier VaresView Answer on Stackoverflow
Solution 7 - GitUjjwal RoyView Answer on Stackoverflow