3

I cloned the remote repository which is having three files

file1.txt 
file2.txt
file3.txt

changes are made locally in my machine into file3.txt. I don't want the changes to go into remote repo and vice versa.

I read that using .gitignore it is not possible to ignore the already commited and tracked file.

I decided to use .git/info/exclude file with the following line

exclude file

file3.txt

But still changes made in file3.txt are shown as unstaged file. I want this file3.txt to be ignored in commit and staging and also need to prevent file3.txt to update from remote repo

Thanks in advance for any help

Mohamed Hussain
  • 7,433
  • 14
  • 55
  • 85
  • `.git/info/exclude` works identically to `.gitignore` files, just that the former doesn’t need to be within the working directory. – poke Feb 16 '15 at 07:48

2 Answers2

8

I read that using .gitignore it is not possible to ignore the already commited and tracked file.

It is, with the .gitignore or .git/info/exclude.

You just need to record the deletion of that file first.

git rm --cached file3.txt

As soon as it is deleted (from the cache only, not from the hard drive), a git status should not show it (since it is already in the .gitignore)

See "git rm --cached x” vs “git reset head — x”?": "cache", also called "index" or "staging area", removes the file from index alone and keeps it in your working copy.

file3.txt will no longer be versionned in the repo.
And that will propagate to other clones once that deletion will have been committed and push.


If you only need to ignore the file locally for a while, you can try instead:

git update-index --assume-unchanged -- file3.txt

Note: that doesn't involve .gitignore at all.

To stop locally ignoring changes:

git update-index --no-assume-unchanged -- file3.txt
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks For the help.will using this command affect the remote repo's file.txt? Please help me understand what is cache is doing here? – Mohamed Hussain Feb 16 '15 at 07:51
  • @MohamedHussain cache, or index, is where you are preparing your next commit. I have edited the answer. – VonC Feb 16 '15 at 07:54
  • Thanks for the update. One more doubt Will running the comment affect other teammates file3.txt. i don't want to modify anything in central remote repo or in others file3.txt. – Mohamed Hussain Feb 16 '15 at 08:08
  • if want to do this for entire folder and its content instead of single file file3.txt, how to do it? – Mohamed Hussain Feb 16 '15 at 08:10
  • 1
    @MohamedHussain yes, it will affect other repos once you have committed and push the deletion of file3.txt – VonC Feb 16 '15 at 08:12
  • i don't want that to happen, i am changing file3.txt for my own test purpose, if it is deleted in other's repo if i pushed then it will affect the project. is there any other better way to do this? – Mohamed Hussain Feb 16 '15 at 08:15
  • 1
    @MohamedHussain I just edited the answer to indicate an alternative method. – VonC Feb 16 '15 at 08:15
  • Thanks for your patience help, i am new to this GIT Please don't mind if i ask some silly questions to you. Do I need to run this "git update-index --assume-unchanged [...]" every time i start working on the Git repo? or one time is enough.In the given link http://stackoverflow.com/questions/1753070/git-ignore-files-only-locally/14462290#14462290 flag -- is not preceded before the file name is it correct? How to ignore a folder completely since i need to ignore locally multiple files – Mohamed Hussain Feb 16 '15 at 08:25
  • 1
    @MohamedHussain one time is enough, unless you reset (see http://stackoverflow.com/a/8047030/6309) or you check out another branch (see http://stackoverflow.com/a/9816844/6309) – VonC Feb 16 '15 at 08:29
  • 1
    @MohamedHussain For ignoring a folder that way, try `git update-index --no-assume-unchanged -- afolder/` (note the final '`/`'). If it doesn't work, then you will have to do it on all files in the folder: http://stackoverflow.com/a/12288918/6309 – VonC Feb 16 '15 at 08:34
  • 1
    @MohamedHussain the double hyphen (`--`) is just here to separate the command and options from the arguments (files or folder): see http://stackoverflow.com/a/1192194/6309 – VonC Feb 16 '15 at 08:35
  • Thanks for your patience help, i tried now it is working good. I learnt lot from this one question. once again thanks Vonc – Mohamed Hussain Feb 16 '15 at 08:40
  • @MohamedHussain did `git update-index --assume-unchanged -- afolder/` (with the final '`/`') worked? – VonC Feb 16 '15 at 08:50
  • I tried that but Git giving me the message "Ignoring path afolder/", but files are still in unstaged status. – Mohamed Hussain Feb 16 '15 at 14:00
  • 1
    @MohamedHussain ok. At least http://stackoverflow.com/a/12288918/6309 should work. – VonC Feb 16 '15 at 14:04
  • It is working after did as in SO post above. but how to undo this command git ls-files -z | xargs -0 git update-index --assume-unchanged? – Mohamed Hussain Feb 17 '15 at 13:06
  • @MohamedHussain same command, with `--no-assume-unchanged` instead of `--assume-unchanged`. – VonC Feb 17 '15 at 13:42
0

edit the .gitignore file adding the line: file3.txt

satchcoder
  • 797
  • 5
  • 11
  • the .gitignore file itself is tracked and i don't want to include the file3.txt in .gitigore, I made changes for my local alone in file3.txt, if i add in .gitignore in other teammates clone's also this file is excluded. I don;t want it to happen ,more over i read in net that it is not possible to ignore the tracked file using .gitignore – Mohamed Hussain Feb 16 '15 at 07:48