0

I am using git inside a project. The project contains a file which contains some configuration specific to the local machine of each developer. Git already has a version of this file. I have the updated copy of that file as per my need and now I do not want to change the content of this file by any pull/push/checkout, merge.

I can not do git remove to that file and move it to outside the project . So this can not solve my problem

Deepak Dixit
  • 1,510
  • 15
  • 24
  • The proper way to do this is to copy the config files on install to somewhere outside the repo, e.g., `/etc/projectname/projectname.conf`, and edit it there. – SIGSTACKFAULT Apr 19 '18 at 13:43
  • 3
    Possible duplicate of [How to ignore certain files in git?](https://stackoverflow.com/questions/4308610/how-to-ignore-certain-files-in-git) – Fabián Montero Apr 19 '18 at 13:45
  • @SpiritualDixit Why can't you do git remove? Isn't that what you actually want to do? – Fabián Montero Apr 19 '18 at 14:29
  • No, I do not want to remove the file . I am not the project maintainer, I just want to ignore the changes of this file for my local. – Deepak Dixit Apr 19 '18 at 14:31
  • 1
    Possible duplicate of [What's the easiest way to deal with project configuration files?](https://stackoverflow.com/questions/6782017/whats-the-easiest-way-to-deal-with-project-configuration-files) – phd Apr 19 '18 at 14:38
  • 1
    @DeepakDixit git remove **does not** remove the file in the way you might be thinking. It removes files from the index, or from the working tree and the index. (This just means it stops tracking the file) Check [out the docs](https://git-scm.com/docs/git-rm). – Fabián Montero Apr 19 '18 at 14:38

5 Answers5

1

You can add the path of the file to your .gitignore.

However, it seems that git is already tracking the file. To solve this, use git reset fileName.txt to unstage the file. After that, use git rm --cached fileName to remove the file from the repository.

After this, you can commit and push changes and you will see that the file is no longer being tracked by git.

Edit: as I understand from comments, it is advisable to commit, push and pull all changes before you run those commands.

pishpish
  • 2,574
  • 15
  • 22
Fabián Montero
  • 1,613
  • 1
  • 16
  • 34
  • 1
    You would not be allowed to pull changes if the file is still in your working tree. – pishpish Apr 19 '18 at 14:50
  • @destoryer I agree. Before running the commands, it is necessary to commit, push and pull all changes. I'll add that to my answer. Thanks! – Fabián Montero Apr 19 '18 at 14:53
  • 1
    If you do commit, you will overwrite the file on the server :) – pishpish Apr 19 '18 at 15:42
  • That is exactly what op wants, isn't it? Since the file is different for every developer, it shouldn't be in the repository. It's a configuration file that changes depending on the machine that runs the code. – Fabián Montero Apr 19 '18 at 19:54
  • 1
    No, the op wants the file kept LOCALLY, but NOT IN THE REPOSITORY. Your suggestions would make changes in the repository, which means the file would end up on the remote when pushing, and all other developers would get his configuration file. – pishpish Apr 20 '18 at 08:49
  • @destoryer this is exactly what my solution does. My answer keeps the file locally and not in the repository. If you follow my instructions, the configuration file will be kept locally but not pushed to the remote. I advice you to test it yourself before commenting any further. – Fabián Montero Apr 20 '18 at 14:13
  • 1
    @destoryer May I recommend opting for a somewhat higher level of language? – Yunnosch Apr 20 '18 at 20:04
  • @destoryer test my solution, after that, I'll answer any questions you have. Also, please don't insult me, I have been respectful throughout all this discussion. – Fabián Montero Apr 20 '18 at 22:42
-1

Try adding the file path to .gitignore file.

For more information check

https://git-scm.com/docs/gitignore

https://www.atlassian.com/git/tutorials/saving-changes/gitignore

After adding the file path to .gitignore file, the added file will not be shown in the result for git status in case of modification

Anurag Dhunna
  • 544
  • 1
  • 5
  • 17
-1

If I understood the question correctly, you want to keep your edited file locally, and to not have it interfere with fetching/pushing to server.

To achieve that, you mustn't commit the file to git (or it will get pushed), but having it changed in the working tree won't allow merging (since it's already tracked and merge conflicts might arise).

The problem could be solved by committing all files normally except for the edited file, stashing the file prior to fetching and popping the stash after it.

pishpish
  • 2,574
  • 15
  • 22
  • Ideally you'd automate the process. – pishpish Apr 19 '18 at 14:26
  • yes, thats the only problem I see with your answer. It takes time to do that on every commit and push. Thats why I propose to simply stop tracking and removing the file from the repo. – Fabián Montero Apr 19 '18 at 14:27
  • @machetazo `[alias] p = !git stash && git pull && git pop` – pishpish Apr 19 '18 at 14:33
  • @destoryer yes, I see how that would work. However, I still think that solution might be too convoluted. Isn't it simpler to just `git reset` and then `git rm --cached`? There's no need to create an alias or a bash script, all you need is to run those 2 commands. – Fabián Montero Apr 19 '18 at 14:36
-2

I think you're looking for:

git merge --abort

If you're looking to ignore the file, you should place it in your gitignore file

-2

This is not, strictly, a direct answer to the question; however, this is the proper way™ to do this.

Let's say your project has a setup script, setup.sh

Add this somewhere in setup.sh:

confdir=/etc/projectname
cp settings.conf $confdir/settings.conf

That way, the settings file is outside the repo, and you can modify it to your heart's content without having to worry about git tracking the changes.

If you want to modify the settings.conf that comes with the project, i.e., the default settings, just commit to the original in the repo.


enter image description here

SIGSTACKFAULT
  • 919
  • 1
  • 12
  • 31
  • It will be in the history, but it won't get checked out on any later commit. – pishpish Apr 19 '18 at 13:55
  • Yes; that's what I intended. – SIGSTACKFAULT Apr 19 '18 at 13:57
  • 1
    I think the op wants to keep his file locally, and to not have it overwritten when fetching from the server. – pishpish Apr 19 '18 at 14:01
  • 1
    Adding a file to .gitignore won't stop tracking changes to that file, that's not what .gitignore is for. If the file is already added to, and thus tracked by, the repository then it will be tracked the same way as any other file. Changes to it will be shown as "not staged for commit", "git add ." will add those changes, etc. – Lasse V. Karlsen Apr 19 '18 at 14:01
  • Wait, cluster foxtrot. Would the `git reset` thing from @machetazo work? – SIGSTACKFAULT Apr 19 '18 at 14:03
  • That command is missing a branch or `--` to imply HEAD; in any case, he'd lose his local edits. – pishpish Apr 19 '18 at 14:07