2063

I have cloned a project that includes some .csproj files. I don't need/like my local csproj files being tracked by Git (or being brought up when creating a patch), but clearly they are needed in the project.

I have added *.csproj to my LOCAL .gitignore, but the files are already in the repo.

When I type git status, it shows my changes to csproj which I am not interested in keeping track of or submitting for patches.

How do I remove the "tracking of" these files from my personal repo (but keep them in the source so I can use them) so that I don't see the changes when I do a status (or create a patch)?

Is there a correct/canonical way to handle this situation?

Rishabh
  • 3,752
  • 4
  • 47
  • 74
Joshua Ball
  • 23,260
  • 6
  • 27
  • 29
  • 22
    A very useful question, but I'm curious as to why you wouldn't want to track changes to the `.csproj` file, which is very much an important part of any project. Changes to the `.csproj.user` file or any `.Publish.XML` files I can totally understand not tracking, but I'm intrigued as to why you wouldn't want to track the `.csproj`… – Owen Blacker May 03 '12 at 10:26
  • 9
    Maybe they use a different IDE? – Jarrett Apr 02 '13 at 01:17
  • 4
    Ironically, I came to this thread because I'm looking to remove .suo files from a repo but keep them locally. For posterity, .Net development requires you to keep .csproj files in the repo and those changes should always be tracked unless you'd like to feel the wrath of any other developers on your project. If ever unsure, take a look at the gitignore files repo on GitHub: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore – longda Sep 22 '13 at 20:39

20 Answers20

2421

Just calling git rm --cached on each of the files you want to remove from revision control should be fine. As long as your local ignore patterns are correct you won't see these files included in the output of git status.

Note that this solution removes the files from the repository, so all developers would need to maintain their own local (non-revision controlled) copies of the file

To prevent git from detecting changes in these files you should also use this command:

git update-index --assume-unchanged [path]

What you probably want to do: (from below @Ryan Taylor answer)

  1. This is to tell git you want your own independent version of the file or folder. For instance, you don't want to overwrite (or delete) production/staging config files.

git update-index --skip-worktree <path-name>

The full answer is here in this URL: http://source.kohlerville.com/2009/02/untrack-files-in-git/

WonderLand
  • 5,494
  • 7
  • 57
  • 76
anthony
  • 40,424
  • 5
  • 55
  • 128
  • 6
    According to the manual: git-rm - Remove files from the working tree and from the index That is exactly what I don't want to do. Am I missing something? However, looking at the documentation for git rm, I see --cached --cached: Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone. I think that is what I need, right? – Joshua Ball Jun 01 '09 at 19:40
  • 213
    "git rm --cached " would remove from version control, while keeping it in the working repository. Whether it is what you want... – Jakub Narębski Jun 02 '09 at 00:03
  • yeah good call on --cached. i'm so used to having most build related files be generated. I'll edit my response. – anthony Jun 02 '09 at 02:22
  • 55
    But when other will pull the repository, will their own *.csproj file will be removed ? Because if we want the file to be untracked, but not deleted. – FMaz008 Jun 01 '11 at 13:07
  • 23
    If you are trying to remove ALL the files in a directory, combine it with git ls-files: `git ls-files | xargs git rm --cached` -- that will remove everything from the git index in a given directory without deleting the actual files. – Marco Nov 10 '11 at 19:51
  • 7
    here is a complete example to remove say all war files from getting tracked. $ git ls-files | grep "\.war" | xargs git rm --cached – Gmu Sep 22 '12 at 02:33
  • 135
    `git rm --cached -r ` works recursively on a folder and all files in it. – Krista K Feb 15 '13 at 01:29
  • 3
    FMaz008 If you git rm -cached .csproj then it will remove it from version control. Then you want to add it to the project's .gitignore file. That should allow everyone to keep their own .cdproj file even if they pull. – Statuswoe May 01 '13 at 14:47
  • 1
    @Chris K.: or on unix you can do `find . -name "*.csproj" -exec git rm --cached {} \;` – Sergey Orshanskiy Oct 06 '13 at 04:47
  • 1
    araqnid's answer [below](http://stackoverflow.com/a/936325/751061) is much closer to what you are asking!! – NHDaly Oct 18 '13 at 02:08
  • 2
    @Statuswoe my colleague just said it deleted his version of the file. He's not very happy. – jackocnr Jan 22 '14 at 21:51
  • 51
    This will stop tracking of the file, preserve it locally, but cause it to be *deleted* for anyone who pulls – Edward Newell Feb 14 '14 at 00:58
  • 8
    Not only will it delete the file for anyone that pulls, it will delete your file if you check out a revision from before the deletion and then check out a revision from after the deletion. – HotN Mar 14 '14 at 14:26
  • 11
    Can somebody please come up with a solution/link that doesn't delete the file(s) from other team members' repositories (as FMaz008 and jackocnr noted). – Yngvar Kristiansen Apr 23 '14 at 08:24
  • 6
    I don't understand why this question has been accepted as an answer, because it is not an answer. I would mark araqnids answer as the correct one. – OrangeTux May 07 '14 at 12:25
  • 2
    I've been using @araqnids answer for years and it works exactly as requested to solve this problem. Any of the other answers are just not right. – NHDaly May 27 '14 at 04:31
  • To think it was this easy. donato. – JohnMerlino Jun 28 '14 at 03:55
  • 1
    with simple experiment, I saw that after doing `git rm --cache file1`, you should add file1 to the .gitignore list and do `git add .gitignore` (if .gitignore was not already being tracked). Once this has been done, if anybody pulls later, his/her local file1 will not be removed. – Chan Kim Sep 13 '14 at 09:47
  • 3
    @ChanKim This doesn't work. As pointed out, this marks the file for removal and when someone pulls/merges later, it will get removed. – the_new_mr May 11 '15 at 10:13
  • 1
    `git ls-files -z myFolderToIgnore/ | xargs -0 git update-index --assume-unchanged` will do it recursively for all indexed files in `myFolderToIgnore` (copied from @Chronial's [answer](http://stackoverflow.com/a/16346756/1168315)) – Dominik Jun 09 '16 at 11:25
  • I also had to do a `git reset HEAD` before doing this. [Toby Beresford](https://stackoverflow.com/users/1048196/toby-beresford)'s [answer](https://stackoverflow.com/questions/12920652/git-update-index-assume-unchanged-returns-fatal-unable-to-mark-file) helped me – Srichakradhar Jun 27 '17 at 09:44
  • 3
    This answer is wrong - the OP wants "How do I remove the "tracking of" these files from my personal repo (**but keep them in the source so I can use them**)" – Reversed Engineer Jul 03 '17 at 11:25
  • 1
    this change go to remote repository or just works on my local repository!? – Milton Jacomini Neto Feb 13 '19 at 17:45
  • 3
    I feel like I use this answer on a weekly basis 5+ years into my career. – Taylor Ackley Apr 30 '20 at 16:14
  • I think .gitignore should suffice, else change cam affect remote too – AdityaKapreShrewsburyBoston Aug 07 '21 at 16:33
  • Why is it that every time I'm forced to use Git the answer to my question involves some arcane command? Mercurial is objectively better in so many ways and yet Git is way more popular... – Clonkex Aug 22 '23 at 20:52
472

There are 3 options; you probably want #3

  1. This will keep the local file for you, but will delete it for anyone else when they pull.

    git rm --cached <file-name> or git rm -r --cached <folder-name>

  2. This is for optimization, like a folder with a large number of files, e.g. SDKs that probably won't ever change. It tells Git to stop checking that huge folder every time for changes, locally, since it won't have any. The assume-unchanged index will be reset and file(s) overwritten if there are upstream changes to the file/folder (when you pull).

    git update-index --assume-unchanged <path-name>
    
  3. This is to tell Git that you want your own independent version of the file or folder. For instance, you don't want to overwrite (or delete) production/staging config files.

    git update-index --skip-worktree <path-name>
    

    It's important to know that git update-index will not propagate with Git, so each user will have to run it independently.


Note that to undo either #2 or #3, you can use the [no-] variant of the respective commands:

git update-index --no-assume-unchanged <path-name>
git update-index --no-skip-worktree <path-name>
zcoop98
  • 2,590
  • 1
  • 18
  • 31
Ryan Taylor
  • 12,559
  • 2
  • 39
  • 34
  • 25
    This answer is the most complete - it offers various solutions with the ramifications of each. The specific case I am working with has a password embedded in a config file. I want to propagate a template file then add the password to my copy. The copy with the password should be ignored and not overwritten. – bmacnaughton Nov 20 '16 at 16:23
  • 3
    How can i check, in my local, which files apply for 'assume-unchanged' or 'skip-worktree'? – Supawat Pusavanno Sep 19 '17 at 01:44
  • 4
    @SupawatPusavanno to see which files you previously selected for assume-unchanged or skip-worktree look at this answer https://stackoverflow.com/questions/42363881/how-to-list-files-ignored-with-skip-worktree -- it uses `grep` and `git ls-files` – Ryan Taylor Sep 19 '17 at 17:15
  • 1
    Very good answer. But git throws error when I try to switch to a different branch:error: "Your local changes to the following files would be overwritten by checkout ....." and the solution is to stash the changes before switching and un-stash when you come back to the branch. – PhantomReference Dec 13 '17 at 05:28
  • @RyanTaylor: I have tried assume unchanged first(did not work) and skip worktree command(did not work), when i check with git status the files are not showing means the command worked. But when i pull the code again from git origin repo `it gives me error that your local changes would be overwritten` in those 2 files as well so that means it is not untracking right? – NeverGiveUp161 Sep 12 '19 at 09:59
  • 1
    @RyanTaylor is it possible to keep your own local version of the file, not pushing your own changes, while still pulling in changes that are pushed to the repo? – Marc Sloth Eastman May 18 '21 at 21:17
  • @RyanTaylor I can't get this to work for github desktop I ran C:\Users\user>cd C:\Users\user\Dropbox\MastersAtWaterloo\Apps\Overleaf\project C:\Users\user\Dropbox\MastersAtWaterloo\Apps\Overleaf\project>git update-index --skip-worktree C:\Users\user\Dropbox\MastersAtWaterloo\Apps\Overleaf\project\ipco2021\ Ignoring path ipco2021/ C:\Users\user\Dropbox\MastersAtWaterloo\Apps\Overleaf\project> but git desktop still detects changes in the folder – Hao S Jul 01 '21 at 17:57
  • @PhantomReference Which option are you using? That is, for `--skip-worktree`, I'm pretty sure there's nothing to stash and you end up in NeverGiveUp161's situation. The only fix I can figure is to run a `git update-index --no-skip-worktree path/to/myFile.file`, stash _that_, and then change branches. – ruffin Sep 27 '21 at 14:44
  • To know differences between `--assume-unchanged` and `--skip-worktree`, see https://stackoverflow.com/questions/13630849/git-difference-between-assume-unchanged-and-skip-worktree#:~:text=%2D%2Dassume%2Dunchanged%20assumes%20that,because%20developers%20should%20change%20it. – shivamag00 Oct 20 '22 at 16:13
263

If you do git update-index --assume-unchanged file.csproj, git won't check file.csproj for changes automatically: that will stop them coming up in git status whenever you change them. So you can mark all your .csproj files this way- although you'll have to manually mark any new ones that the upstream repo sends you. (If you have them in your .gitignore or .git/info/exclude, then ones you create will be ignored)

I'm not entirely sure what .csproj files are... if they're something along the lines of IDE configurations (similar to Eclipse's .eclipse and .classpath files) then I'd suggest they should simply never be source-controlled at all. On the other hand, if they're part of the build system (like Makefiles) then clearly they should--- and a way to pick up optional local changes (e.g. from a local.csproj a la config.mk) would be useful: divide the build up into global parts and local overrides.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
araqnid
  • 127,052
  • 24
  • 157
  • 134
  • 8
    csproj is a C# project file, which keeps track of which files are included in your project and other few configurations, it MUST be source controlled for the project to work – SparK Dec 04 '13 at 15:20
  • 5
    This is the only right answer here! I've been using @araqnids answer for years and it works exactly as requested to solve this problem. – NHDaly May 27 '14 at 04:31
  • What is the meaning of the 'file' prefix in the argument to the command? Why isn't it just `.csproj`? – GreenAsJade Nov 27 '14 at 02:30
  • @GreenAsJade: It is not a prefix, `file.csproj` is a file name. – Amadan Nov 27 '14 at 02:30
  • 1
    Is there some way you can detect that this has been done for a file, or for what files this has been done in a repo? I'm a bit nervous about forgetting that I did this, then wondering why the heck this file isn't getting updated, later on! – GreenAsJade Nov 27 '14 at 02:41
  • 4
    @GreenAsJade: `git ls-files -v` will show files that are assumed unchanged with a lowercase indicator (e.g. `h` instead of usual `H` for cached files). – Amadan Nov 27 '14 at 03:42
  • 1
    Thanks - this little pearl almost deserves a question of its own! ... done http://stackoverflow.com/questions/27162966/how-can-i-tell-which-files-have-been-marked-as-assume-unchanged-in-git – GreenAsJade Nov 27 '14 at 04:12
  • @araqnid if the file set as unchanged locally is also in the repository and is changed by another person will a pull update the file? I have a file that has information not required by others, but the underlying code might be changed at some point remotely and I'll need to pick up the change – mtpultz Mar 11 '15 at 21:38
  • @mtpultz I haven't tried it, tbh. It seems unlikely to me that git will preserve your version when you pull, though- I'd expect the usual "dirty workspace" warning, or possibly it will just overwrite your changes. Experiment? – araqnid Mar 13 '15 at 22:23
  • I was looking for this also. `git rm --cached` removes it from the Repo. So, when I push my Repo, the build breaks for all those who don't get the file I just did `git rm --cached` to – The Vivandiere May 07 '15 at 21:02
  • I don't know if it happens in practice but maybe a word of caution on this approach. According to this answer http://stackoverflow.com/questions/23097368/git-ignore-vs-exclude-vs-assume-unchanged from Junio Hamano (the maintainer of Git) "if Git can determine a path that is marked as assume-unchanged has changed without incurring extra lstat(2) cost, it reserves the right to report that the path has been modified". Basically saying there is no guarantee that Git will abide by the --assume-unchanged flag. – joesk May 05 '16 at 21:57
  • I can confirm that this still tells git to ignore changes to the file even when you switch branches – GreensterRox Feb 16 '17 at 14:45
  • When I try to checkout to a branch who has a different version of this file it says: error: Your local changes to the following files would be overwritten by checkout: file.csproj Please commit your changes or stash them before you switch branches. Aborting – denis_lor Feb 21 '17 at 12:22
  • I had to modified an ignored file so a I did a `git add -f ` and since then that file appears as tracked. I have tried `git clean` `git rm --cached ` and none of them worked. `git update-index --assume-unchanged ` worked for me! – csabapap Mar 08 '17 at 18:18
  • Here is how to do it for folders and all their contents: https://stackoverflow.com/a/12288918/1970830 – Haider Jul 01 '19 at 09:18
163

This is a two step process:

  1. Remove tracking of file/folder - but keep them on disk - using

    git rm --cached 
    

    Now they do not show up as "changed" but still show as

        untracked files in  git status -u  
    
  2. Add them to .gitignore

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
rjha94
  • 4,292
  • 3
  • 30
  • 37
  • 67
    No, this will remove the file from tracking, preserve it locally, *but cause it to be deleted for anyone who pulls*. – Edward Newell Feb 14 '14 at 01:00
  • 1
    In my case, I accidentally added a folder that I didn't want tracked, so this is what I needed. – Sonny Jun 04 '14 at 15:18
  • 4
    Yes, it is indeed the wrong answer to the question as asked - but it's probably the right answer for most people that find this question in search results (like me). – Andrew Spencer Aug 10 '17 at 13:16
116

The accepted answer still did not work for me

I used

git rm -r --cached .

git add .

git commit -m "fixing .gitignore"

Found the answer from here

Buddhika Hasthanayake
  • 1,269
  • 1
  • 8
  • 8
  • That link is very useful, especially to recursively remove all the files in the .gitignore – rmcsharry Apr 13 '16 at 10:44
  • 12
    I have returned here 3 times, hopefully i can commit to memory before the next time! – Harry Bosh Apr 20 '16 at 01:09
  • 1
    @Edward Newell 's comment as in the [above answer](https://stackoverflow.com/a/6913650/3162788) applies here as well: "this will remove the file from tracking, preserve it locally, _but cause it to be deleted for anyone who pulls_". – ToJo May 05 '20 at 20:17
56

Forgot your .gitignore?

If you have the entire project locally but forgot to add you git ignore and are now tracking some unnecessary files use this command to remove everything

git rm --cached -r .

make sure you are at the root of the project.

Then you can do the usual

Add

git add .

Commit

git commit -m 'removed all and added with git ignore'

Push

git push origin master

Conclusion

Hope this helps out people who have to make changes to their .gitignore or forgot it all together.

  • It removes the entire cache
  • Looks at your .gitignore
  • Adds the files you want to track
  • Pushes to your repo
Gangnus
  • 24,044
  • 16
  • 90
  • 149
Joe Lloyd
  • 19,471
  • 7
  • 53
  • 81
  • 5
    When you are speaking on removing or adding, you are forgetting say when and where. Removing from the track list? From the repository? From the local project space? Removing at pull? At commit? At push? Alas, all authors here have the same problem. – Gangnus Mar 11 '16 at 08:50
  • 4
    @Gangnus I don't think anyone 'clarified' the point you're trying to make because it's completely obvious that the file isn't actually being removed from disk or the repository. This answer specifies the chronological order of the commands. It's not mysterious or ill-explained as your comment would suggest. – Anthony Sep 26 '16 at 22:07
  • @Edward Newell 's comment as in the [above answer](https://stackoverflow.com/a/6913650/3162788) applies here as well: "this will remove the file from tracking, preserve it locally, _but cause it to be deleted for anyone who pulls_". – ToJo May 05 '20 at 20:19
  • 1
    exactely what I was looking for.. thanks for sharing!! ;) – Fernando Meneses Gomes Jul 10 '20 at 20:28
43

To prevent monitoring a file or path by git

git update-index --assume-unchanged [file-path]

And to revert it back use

git update-index --no-assume-unchanged [file-path]

A repo to refer for similar use cases https://github.com/awslabs/git-secrets

shijin
  • 2,998
  • 1
  • 24
  • 30
41

As pointed out in other answers, the selected answer is wrong.

The answer to another question suggests that it may be skip-worktree that would be required.

git update-index --skip-worktree <file>

That answer links to an article (http://fallengamer.livejournal.com/93321.html) and quotes the article with a nice summary of the difference between --assume-unchanged and --skip-worktree as follows:

--assume-unchanged assumes that a developer shouldn’t change a file. This flag is meant for improving performance for not-changing folders like SDKs.

--skip-worktree is useful when you instruct git not to touch a specific file ever because developers should change it. For example, if the main repository upstream hosts some production-ready configuration files and you don’t want to accidentally commit changes to those files, --skip-worktree is exactly what you want.

All credit to borealid for their research and answer. Including this info here purely for the convenience of others.

the_new_mr
  • 3,476
  • 5
  • 42
  • 57
  • 2
    No, not really: `--skip-worktree` is used to **keep the file on the repository but stop tracking its changes**. As your answer says: _--skip-worktree is useful when you instruct git not to touch a specific file ever because developers **should** change it_ – Erdal G. Nov 09 '15 at 12:29
  • 6
    @ErdalG. Exactly. According to the question, they want to ignore any changes in the file but keep the file in the repo – the_new_mr Aug 31 '16 at 11:05
  • 1
    Agree with @the_new_mr , `--assume-unchanged` and `--skip-worktree` have similar effect but their purposes are entirely different. The former one is for speeding up git performance by fooling git *not to check particular files*, while the latter one is for *ignoring future changes on particular files*, which are suitable for runtime but essential files. – Victor Wong Sep 15 '16 at 04:00
27

To save some time the rules you add to your .gitignore can be used for removing multiple files/folders i.e.

git rm --cached app/**/*.xml

or

git rm --cached -r app/widgets/yourfolder/

e.t.c.

Peter
  • 4,493
  • 6
  • 41
  • 64
  • This is a very good solution cause in any chance you want to fix the gitignore step by step – cutiko May 04 '16 at 20:35
17

Lots of people advise you to use git update-index --assume-unchanged. Indeed, this may be a good solution, but only in the short run.

What you probably want to do is this: git update-index --skip-worktree.

(The third option, which you probably don't want is: git rm --cached. It will keep your local file, but will be marked as removed from the remote repository.)

Difference between the first two options?

  • assume-unchanged is to temporary allow you to hide modifications from a file. If you want to hide modifications done to a file, modify the file, then checkout another branch, you'll have to use no-assume-unchanged then probably stash modifications done.
  • skip-worktree will follow you whatever the branch you checkout, with your modifications!

Use case of assume-unchanged

It assumes this file should not be modified, and gives you a cleaner output when doing git status. But when checking out to another branch, you need to reset the flag and commit or stash changes before so. If you pull with this option activated, you'll need to solve conflicts and git won't auto merge. It actually only hides modifications (git status won't show you the flagged files).

I like to use it when I only want to stop tracking changes for a while + commit a bunch of files (git commit -a) related to the same modification.

Use case of skip-worktree

You have a setup class containing parameters (eg. including passwords) that your friends have to change accordingly to their setup.

  • 1: Create a first version of this class, fill in fields you can fill and leave others empty/null.
  • 2: Commit and push it to the remote server.
  • 3: git update-index --skip-worktree MySetupClass.java
  • 4: Update your configuration class with your own parameters.
  • 5: Go back to work on another functionnality.

The modifications you do will follow you whatever the branch. Warning: if your friends also want to modify this class, they have to have the same setup, otherwise their modifications would be pushed to the remote repository. When pulling, the remote version of the file should overwrite yours.

PS: do one or the other, but not both as you'll have undesirable side-effects. If you want to try another flag, you should disable the latter first.

belka
  • 1,480
  • 1
  • 18
  • 31
13

To tell Git not to track changes to your local file/folder (meaning git status won't detect changes to it), do:

git update-index --skip-worktree path/to/file

And to tell Git to track changes to your local version once again (so you can commit the changes), do:

git update-index --no-skip-worktree path/to/file
Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39
4

Apply .gitignore to the present/future

This method applies the standard .gitignore behavior, and does not require manually specifying the files that need to be ignored.

Can't use --exclude-from=.gitignore anymore :/ - Here's the updated method:

General advice: start with a clean repo - everything committed, nothing pending in working directory or index, and make a backup!

#commit up-to-date .gitignore (if not already existing)
#this command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"

#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#this command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached

#optionally add anything to the index that was previously ignored but now shouldn't be:
git add *

#commit again
#optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.

git commit -m "re-applied modified .gitignore"

#other devs who pull after this commit is pushed will see the  newly-.gitignored files DELETED

If you also need to purge the newly-ignored files from the branch's commit history or if you don't want the newly-ignored files to be deleted from future pulls, see this answer.

Community
  • 1
  • 1
goofology
  • 914
  • 2
  • 10
  • 21
3

one line answer git update-index --assume-unchanged [path]

Use this whenever you have a file that is in central repo and also local repo.you need to make changes in that file but must not be staged/committed to the central repo. This file should not be added in .gitignore. because new changes in the file if introduced by system administrators, senior developers needs to be distributed among all local repos.

Best Example: config file for DB connections. In a central repo, you will have all the username,password, host,port with values of a production DB server. But in local dev, you should use only a local or any other development DB server(that your team has setup). In this case you wanna make changes in config file but should not be committed to central repo.

Best

Seeni
  • 1,518
  • 1
  • 14
  • 22
2

git rm --fileName

git ls-files to make sure that the file is removed or untracked

git commit -m "UntrackChanges"

git push

0

I am assuming that you are asking how to remove ALL the files in a specific folder or the bin folder, Rather than selecting each files separately.

You can use this command:

git rm -r -f /<floder-name>\*

Make sure that you are in the parent directory of the of that directory.
This command will, recursively "delete" all the files which are in the bin/ or build/ folders. By the word delete I mean that git will pretend that those files are "deleted" and those files will not be tracked. The git really marks those files to be in delete mode.

Do make sure that you have your .gitignore ready for upcoming commits.
Documentation : git rm

Vraj Pandya
  • 591
  • 1
  • 9
  • 13
0

I am assuming that you are trying to remove a single file from git tacking. for that I would recommend below command.

git update-index --assume-unchanged

Ex - git update-index --assume-unchanged .gitignore .idea/compiler.xml

0

To ignore any changes to all the files (of a certain type) in a directory, I had to combine some of these approaches, otherwise the files were created if they didn't previously exist.

In the below, "excludedir" is the name of the directory that I wish to not watch changes to.

First, remove any existing new files from your change tracking cache (without removing from your file system).

git status | grep "new file:" | cut  --complement -d " " -f1-4 | grep "^excludedir" | xargs git rm --cache

You can do the same with modified:. renamed: is a bit more complicated, as you'll have to look at the post -> bit for the new filename, and do the pre -> bit as described for deleted: below.

deleted: files prove a bit more complicated, as you can't seem to update-index for a file that doesn't exist on the local system

echo .deletedfiles >> .gitignore
git status | grep "deleted:" | cut  --complement -d " " -f1-4 | grep "^excludedir" > .deletedfiles
cat .deletedfiles | xargs -d '\n' touch
cat .deletedfiles | xargs -d '\n' git add -f
cat .deletedfiles | xargs -d '\n' git update-index --assume-unchanged
cat .deletedfiles | xargs -d '\n' rm

The last command in the list above will remove the files again from your file system, so feel free to omit that.

Then, block change tracking from that directory

git ls-files excludedir/ | xargs git update-index --skip-worktree
git update index --skip-worktree excludedir/
mpag
  • 531
  • 7
  • 19
0

An almost git command-free approach was given in this answer:

To ignore certain files for every local repo:

  1. Create a file ~/.gitignore_global, e.g. by using touch ~/.gitignore_global in your terminal.
  2. Run git config --global core.excludesfile ~/.gitignore_global once.
  3. Write the file/dir paths you want to ignore into ~/.gitignore_global. e.g. modules/*.H, which will be assumed to be in your working directory, i.e. $WORK_DIR/modules/*.H.

To ignore certain files for a single local repo:

  1. Do the above third step for file .git/info/exclude within the repo, that is write the file/dir paths you want to ignore into .git/info/exclude. e.g. modules/*.C, which will be assumed to be in your working directory, i.e. $WORK_DIR/modules/*.C.
Sandra
  • 374
  • 6
  • 17
Herpes Free Engineer
  • 2,425
  • 2
  • 27
  • 34
-1

The problem may be caused by the order of operation. If you modified the .gitignore first, then git rm --cached xxx,you may have to continue to encounter this problem.

Correct solution:

  1. git rm --cached xxx
  2. modified the .gitignore

Order invariant!

The .gitignore reload after modification!

thearyong
  • 49
  • 3
-2

after search a long time , find a way do this . alias a git command in .gitconfig.like in android studio project,before checkout branch revert config file and then skip it ,after checkout branch use sed change config file to my local config. checkoutandmodifylocalproperties = !git update-index --no-skip-worktree local.properties && git checkout local.properties && git checkout $1 && git update-index --skip-worktree local.properties && sed -i '' 's/.*sdk.dir.*/sdk.dir=\\/Users\\/run\\/Library\\/Android\\/sdk/g' local.properties && :

chinaanihchen
  • 396
  • 2
  • 5