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

1
  • .git/info/exclude works identically to .gitignore files, just that the former doesn’t need to be within the working directory. Commented Feb 16, 2015 at 7:48

2 Answers 2

9

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
Sign up to request clarification or add additional context in comments.

17 Comments

@MohamedHussain yes, it will affect other repos once you have committed and push the deletion of file3.txt
@MohamedHussain I just edited the answer to indicate an alternative method.
@MohamedHussain one time is enough, unless you reset (see stackoverflow.com/a/8047030/6309) or you check out another branch (see stackoverflow.com/a/9816844/6309)
@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: stackoverflow.com/a/12288918/6309
@MohamedHussain the double hyphen (--) is just here to separate the command and options from the arguments (files or folder): see stackoverflow.com/a/1192194/6309
|
0

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.