Skip to content

Instantly share code, notes, and snippets.

@mcrd25
Last active April 12, 2025 20:37
Show Gist options
  • Save mcrd25/1b2718054b65f4720fa582c6a4032217 to your computer and use it in GitHub Desktop.
Save mcrd25/1b2718054b65f4720fa582c6a4032217 to your computer and use it in GitHub Desktop.
How to untrack a file that has already been committed to repo after adding to .gitignore

How to Untrack a File That Has Been Committed

It is IMPORTANT to note that you MUST COMMIT ALL PREVIOUS CHANGES BEFOE CONTINUING

  1. Remove everything from Local Repo git rm -r --cached . or if you are untracking a specific file(s) git rm -r --cached name_of_file

  2. Add back all untracked files you need git add . or git add name_of_file_with_fix

  3. Commit Changes git commit -m "untrack name_of_file fix"

  4. Push changes to remote repo git push




If you have any questions, do not hesitate to contact me on social media.

Happy coding 😄, --Maya

GitHub linkedin

@livie99
Copy link

livie99 commented Aug 24, 2023

Thx, this is so helpful!!!<3<3<3

@Ilbashyr09
Copy link

  • same here, ### ### we appreciate

@agabaandre
Copy link

Follow this example: Add the file to .gitignore to be untracked
Then run
git rm -r --cached C:/xampp/htdocs/staff/application/config/database.php

@ArthurV7
Copy link

ArthurV7 commented Sep 16, 2024

How can git understand that the specified file is untracked if all we did was remove it from the local cache? I'm starting to study git recently 😭

@mcrd25
Copy link
Author

mcrd25 commented Sep 16, 2024

hi @ArthurV7 Well, you are not technically telling git the specified file is untracked. What you are doing is removing it from staging tree without removing the file from the system. If you use the command like so: git rm -r --cached . it would remove all files from the staging area that have not pushed to the repository and if you specify the file it'd only remove that file. That could then allow you re-add and add a new commit message or add it to your .gitignore after removing it from the staging tree in git.

If anyone knows a better way to do this, please do let me know! Feel free to add me on github, you can also email me at [email protected]

@ArthurV7
Copy link

@mcrd25 oohhh I understand, thx for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment