Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore specific files when pulling from forked upstream origin

Tags:

git

pull

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

like image 452
Charlie Weems Avatar asked Aug 14 '14 18:08

Charlie Weems


1 Answers

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.

like image 147
pratZ Avatar answered Nov 14 '22 19:11

pratZ