4

I have some platform specific modifications to checked-in files. How do I force git to keep local version and ignore the remote one when I merge from a remote branch? Basically, like git-ignore, but for files that are already tracked by repository.

The use-case is that I'm pushing to one branch, and then pulling this branch on different platforms for testing. Doing testing the first time on each platform required running ./configure script which modified some files with platform-specific customizations. Since those changes are automatically generated, I don't want to commit them to history. Normally I add those files to .gitignore, however, some of those files are part of the original repository, so git ignore would be ignored.

Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197

3 Answers3

4

This is possibly a duplicate of git pull keeping local changes as the first answer there in particular seems to be the best solution if the platform-specific changes aren't to be committed.

Community
  • 1
  • 1
Todd Knarr
  • 1,255
  • 1
  • 8
  • 14
  • By the way, if you happen to be on a Mac, this (`git stash && git pull && git stash pop`) is what Tower does with your staged and unstaged changes when pulling. – Taylor D. Edmiston Jun 14 '16 at 00:17
  • @tedmiston how did you know that's what Tower does? I tried to figure out how to see Tower command log with no luck – Yaroslav Bulatov Jul 20 '16 at 17:01
  • @YaroslavBulatov They don't seem to print everything to the activity log, though I'm trusting the prompts that describe doing these commands. I've found their customer support to be really good, and if you wanted an explicit confirmation, I'm sure they'd provide it. – Taylor D. Edmiston Jul 20 '16 at 17:32
0

Here's one possible solution.

Suppose the original HEAD before git merge is A. After the merge, run git checkout A -- path_of_the_file_you_want_to_keep, which restores the file to the status of A.

But this makes the work tree unclean. If you run git status, you'll find the file changed in the index. I think this is unavoidable unless you make another commit.

Since the original HEAD is referred to by the variable ORIG_HEAD in git, you could always run git checkout ORIG_HEAD -- path_of_the_file_you_want_to_keep regardless of A's actual sha1.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Do i need to commit all the platform specific modifications that happened after ./configure run before I can check out those files? – Yaroslav Bulatov Jun 11 '16 at 05:27
  • I'm not clear about your demand, but I think it depends. You could commit any change that you want to keep and restore. – ElpieKay Jun 11 '16 at 06:44
0

Don't check them in. Check in a template under a different name, and ignore the name of the active file.

If your project needs a file called (for example) config.txt, put that filename in .gitignore, and check a file with a different name like config.sample.txt into the repository. If you want to get fancy, you could make your application automatically copy the file from config.sample.txt to config.txt if that file doesn't exist yet.

  • I'm forking an existing project, and those files are already checked in. They are getting modified by the ./configure script so presumably something needs to be there already – Yaroslav Bulatov Jun 13 '16 at 02:19
  • So `git mv` the file from its current name to the new name, and modify the `configure` script to copy the file like I explained in my answer. –  Jun 13 '16 at 02:20
  • This is the [configure script](https://github.com/tensorflow/tensorflow/blob/14ac2235699509f512b44b71160239c153ab413d/configure) btw – Yaroslav Bulatov Jun 13 '16 at 02:24