42

No matter what I put in .gitignore I can not get git to ignore the UserInterfaceState.xcuserstate file below:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   .gitignore
    modified:  CalFoo.xcodeproj/project.xcworkspace/xcuserdata/wcochran.xcuserdatad/UserInterfaceState.xcuserstate

I am using/editing the .gitignore file listed on this post. I tried everything to match the pattern including the exact pathname: CalFoo.xcodeproj/project.xcworkspace/xcuserdata/wcochran.xcuserdatad/UserInterfaceState.xcuserstate to no avail.

This particular problem arises from the workflow where Xcode is used to create the initial git repo and the .gitignore file from here is added afterwards. A more general answer to ignoring previously tracked files in git can be found from this question (I guess I never found this post in my search since it didn't have "gitignore" in the title).

Community
  • 1
  • 1
wcochran
  • 10,089
  • 6
  • 61
  • 69
  • Possible duplicate of [Stop tracking and ignore changes to a file in Git](http://stackoverflow.com/questions/936249/stop-tracking-and-ignore-changes-to-a-file-in-git) – Unicornist Nov 11 '15 at 17:50
  • @Unicornist not a duplicate -- i want a specific file removed and ignored *everywhere* whereas the other question wants certain files to not be tracked globally. – wcochran Jul 04 '17 at 16:39

5 Answers5

72

You can only ignore unversioned files but that file is already known to git.

If you want git to track that file, there is no need to tell git to ignore it.

If you don't want git to track that file use git rm and your ignore rule will start working.

Caution: git rm will remove the file. Use git rm --cached to remove from the repo but not from the disk.

John Weisz
  • 30,137
  • 13
  • 89
  • 132
michas
  • 25,361
  • 15
  • 76
  • 121
  • 18
    Caution, `git rm` will remove the file. Use `git rm --cached` to remove from the repo but not from the disk. – Darkhogg May 17 '14 at 22:23
  • Thanks @michas and Darkhogg -- it is now not being tracked by get and is removed from the repo. – wcochran May 19 '14 at 00:08
  • 2
    This answer corresponds to what I used to believe: but today, I have a file which is in .gitignore, it is not controlled, and it still shows up in "git status" as an untracked file (its filename is 'build'). No amount of "git rm build" or touching the .gitignore helps. Any ideas? – Stabledog Dec 01 '14 at 15:45
  • Same issue here w/ @Stabledog – Allen Linatoc Jun 13 '16 at 02:17
  • 1
    @Stabledog @AllenLinatoc There are multiple possible ignore files, which might contain exclude rules. Try `git check-ignore` and maybe ask a new question to find out what is going on. – michas Jun 13 '16 at 06:00
27

I had same problem.

First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore working now"

To undo git rm --cached filename, use git add filename.

Gaurav Sharma
  • 291
  • 3
  • 7
1

If you have done something like echo node_modules >> .gitignore, it won't work.

The windows terminal saves the file in UCS-2 LE BOM and git doesn't seem to accept that.

You can open the file with Notepad and save it with UTF-8 encoding

notepad save utf-8 encoding

It Works now.

I think they need to fix this since echo "filetoignore" >> .gitignore actually seems a handy thing to do

Abraham
  • 12,140
  • 4
  • 56
  • 92
  • 1
    This is the only thing that worked for me. I tried removing the cache, add, commit, push, etc... and nothing worked. Just doing this simple fix worked wonders. – irishman Jan 16 '22 at 19:07
0

One of the easy ways to ignore files on GitHub is perfectly explained here https://help.github.com/en/articles/ignoring-files

Before you do any push to GitHub, your project folder should be blank.

1) open terminal and write cd

and drag your project folder over terminal. After that it will look like this:

docmacbook$ cd /Users/docmacbook/Downloads/MyProjectFolder

2) Open .gitignore file and add files you want to ignore. For example my .gitignore file looks like this:

MyAppName.xcodeproj/xcuserdata
MyAppName.xcworkspace/xcuserdata
Pods
report.xml

3) Commit and Push to GitHub.

After this move your project or create a new one in the folder where .gitignore file is and now files you want to be ignored will not be pushed to your GitHub.

Note: These steps will work for an existing project too I think, did not tested.

Egzon P.
  • 4,498
  • 3
  • 32
  • 31
0

For me, I had to add my list to the xcode source control Ignored Files. setting -> source control -> git -> + in Ignored Files

xcode git ignore For good measure make sure to reset so the new git rules work.

git rm -r --cached .
git add .
jameseronious
  • 219
  • 2
  • 5