问题
In git, how do you exempt certain files when pulling from an upstream origin (i.e. the original project)?
I have a project that I'm working on that was originally forked from a repository that is very active. I've added the original as a remote named "upstream" so that it's possible to run:
git pull upstream
and update the project to the most recent commit.
The problem: there are some files (e.g. my Gruntfile.js
) that I do not want to update alongside the original project. Whenever I pulled these commits, I would get merge conflicts because I've changed the files in my own version.
I do want to track these files for my own local commits and pull changes from my own origin, so adding these to .gitignore
permanently isn't an option.
My current solution is to have a grunt task temporarily add Gruntfile.js
and others to .gitignore
, pull from upstream, and then remove them from .gitignore
so that I can track my own changes and push to my origin. However, this feels hacky.
Is there any way I can ignore these files only when pulling from "upstream"?
回答1:
Yes you can do that.
git pull is shorthand for git fetch followed by git merge FETCH_HEAD
So first you can do a,
git fetch upstream
followed by,
git merge --no-log --no-ff --no-commit upstream/branch
Git will stop before committing. So, you should be able to modify the merge, and exclude the required files.
UPDATE
The commands can be combined using git alias.
Create an alias inside ~/.gitconfig
[alias]
fnm = !git fetch upstream && git merge --no-log --no-ff --no-commit upstream/branch
You can further add,
[alias]
fnm = !git fetch upstream && git merge --no-log --no-ff --no-commit upstream/branch && git reset file/path/not/to/be/updated && git checkout file/path/not/to/be/updated
Use git fnm
for the complete operation.
回答2:
However, this feels hacky
A less hacky way could be to avoid keeping a forked version of the file while wanting its updates. I don't know what your changes are, but what about proposing a merge request with your modifications?
If your modifications are rather personal (eg: local urls), what about proposing a merge request where you introduce a local configuration file?
That's what is done for example with https://github.com/fabelier/Doorbell: it provides a config.js.template file which is used by default, but the users can provide a config.js
file to add their own specificities ; and this file isn't kept in Git.
来源:https://stackoverflow.com/questions/25315687/ignore-specific-files-when-pulling-from-forked-upstream-origin