33

I have a tracked file in a git repository. How do I untrack this file without deleting it using magit?

deadghost
  • 807
  • 1
  • 7
  • 13

3 Answers3

24

Hitting i will gitignore the file and untrack it from the git repository.

Behind the scenes, it does git rm --cached - More info about this git command from a StackOverflow question

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • Note that you don't need to add it to the gitignore file. Just discard(I believe by default "k") changes on gitignore after i. – deadghost Feb 17 '15 at 14:51
  • Discarding (using `k`) just reverts the file contents to its last committed state; it does not untrack it. To test it, modify the file you just discarded, hit `g` in the magit buffer to refresh the contents and you will see that file appear once again in the *Unstaged* section. – Kaushal Modi Feb 17 '15 at 14:56
  • I mean discarding the changes on .gitignore. – deadghost Feb 17 '15 at 14:58
  • Alright, I now get what you are saying. First hit `i` on the file you want to untrack and then hit `k` on `.gitignore`. For my use case, if there's a file I want to untrack, I usually want to gitignore it too. That way my magit buffer stays clean and empty :) – Kaushal Modi Feb 17 '15 at 15:01
  • 3
    If you don't need to add the file to gitignore, use K (that's uppercase k). – Patrick Fournier May 05 '16 at 16:26
  • Where do you view the files in the first place? – young_souvlaki May 20 '21 at 23:35
  • @young_souvlaki In the Magit status buffer. – Kaushal Modi May 21 '21 at 03:37
  • @KaushalModi My status buffer shows only untracked files when there are no changes. – young_souvlaki May 24 '21 at 15:30
19

In the Magit buffer, hit K (uppercase k) on the file. It works with magit 2.3; I don't know for earlier versions.

  • [f6a6f40fe8321f3ceb4b8ea9581e450149fea66a](https://github.com/magit/magit/commit/f6a6f40fe8321f3ceb4b8ea9581e450149fea66a), looks like it's since 2.1 – npostavs Feb 17 '16 at 22:12
  • 4
    I feel like this should be the accepted answer for the question asked, even though the others were helpful. – salotz May 23 '16 at 21:19
10

Use M-x magit-file-untrack from a buffer visiting a tracked file or from a Magit buffer. When the buffer visits a file then that file is offered as default choice.

When point is on a file in a Magit buffer, then that is also offered as default. You might also want to add the new magit-insert-tracked-files to magit-status-sections-hook, then a tree of all tracked files is inserted into the status buffer (but doing so might not be good for performance in big repositories).

tarsius
  • 25,298
  • 4
  • 69
  • 109