7

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"?

gturri
  • 13,807
  • 9
  • 40
  • 57
Charlie Weems
  • 1,682
  • 1
  • 16
  • 28

2 Answers2

12

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.

pratZ
  • 3,078
  • 2
  • 20
  • 29
  • 1
    Thanks, that makes sense. Is there a way to configure this in something like a gitignore file or is using a task runner like grunt/gulp the best way to go about doing this? fetching/merging in the command line isn't a huge burden, but if there's a way to configure this that's the preferable option. – Charlie Weems Aug 14 '14 at 20:18
  • @weems you can use aliases. – pratZ Aug 14 '14 at 20:53
-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.

gturri
  • 13,807
  • 9
  • 40
  • 57