{"id":2798,"date":"2023-08-02T20:14:38","date_gmt":"2023-08-02T18:14:38","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=2798"},"modified":"2023-08-04T13:16:14","modified_gmt":"2023-08-04T11:16:14","slug":"how-to-untrack-files-already-added-to-git-repository","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2023\/08\/02\/how-to-untrack-files-already-added-to-git-repository\/","title":{"rendered":"How to untrack files already added to git repository"},"content":{"rendered":"\n<p>To untrack files already added to a Git repository, you can follow these steps:<\/p>\n\n\n\n<ol><li><strong>Step 1: Commit any outstanding changes:<\/strong> Before proceeding, make sure you have committed any changes you want to keep. This is to avoid losing any work.<\/li><li><strong>Step 2: Create a .gitignore file:<\/strong> If you haven&#8217;t already, create a <code>.gitignore<\/code> file in the root directory of your repository. This file will specify which files and directories should be ignored by Git.<\/li><li><strong>Step 3: Add the files to .gitignore:<\/strong> Open the <code>.gitignore<\/code> file and add the filenames or patterns of the files you want to untrack. For example, if you want to untrack a file named &#8220;example.txt,&#8221; add the following line to the <code>.gitignore<\/code> file:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>example.txt\n<\/code><\/pre>\n\n\n\n<p>We can also use wildcards to match multiple files, for instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*.txt          # Ignore all files with a .txt extension\ndirectory\/     # Ignore the entire 'directory' and its contents<\/code><\/pre>\n\n\n\n<p>4.<strong>Step 4: Remove the files from Git cache:<\/strong> After adding the files to the <code>.gitignore<\/code> file, you need to remove them from the Git cache. You can do this using the <code>git rm<\/code> command with the <code>--cached<\/code> option. For example, to remove the &#8220;example.txt&#8221; file from the cache, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git rm --cached example.txt\n<\/code><\/pre>\n\n\n\n<p>If you want to remove multiple files, you can specify them in a single command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git rm --cached file1.txt file2.txt directory\/file3.txt\n<\/code><\/pre>\n\n\n\n<p>5.<strong>Step 5: Commit changes:<\/strong> After removing the files from the cache, commit the changes to the repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git commit -m \"Untrack files specified in .gitignore\"\n<\/code><\/pre>\n\n\n\n<p>That&#8217;s it! The files specified in the <code>.gitignore<\/code> file will no longer be tracked by Git, and they won&#8217;t appear in future commits. Be careful when using the <code>git rm --cached<\/code> command, as it will remove the files only from the index (staging area) and not from our working directory. In others words, the file should no longer be tracked by Git. However, it remains in our working directory. Note that this doesn&#8217;t make any changes to existing commits, it just prevents the file from being included in future commits.<\/p>\n\n\n\n<p>We can now push the changes to our remote to see the changes effective there as well.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">PS:<\/span><\/strong> <em><span class=\"has-inline-color has-vivid-red-color\"> Untracked files can make our git status output messy and confusing, and they can also waste disk space.<\/span><\/em><\/p>\n\n\n\n<p>To remove untracked files in our working directories, we can use the git clean command. This command will delete any untracked files and directories in our working directory.<\/p>\n\n\n\n<ul><li>We can use the -n option to perform a dry run and see what files will be deleted without actually deleting them.\u00a0<\/li><li>We can use the -f option to force the deletion of the files.<\/li><li>We can use the -d option to also delete untracked directories.<\/li><li>We can use the -x option to also delete ignored files (files that match the patterns in your .gitignore file).<\/li><\/ul>\n\n\n\n<p>For example, to delete all untracked files and directories, including ignored ones, we can run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clean -d -x -f<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To untrack files already added to a Git repository, you can follow these steps: Step 1: Commit any outstanding changes: Before proceeding, make sure you have committed any changes you want to keep. This is to avoid losing any work. Step 2: Create a .gitignore file: If you haven&#8217;t already, create a .gitignore file in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60],"tags":[710,711,707,708,705,706,709],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2798"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2798"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2798\/revisions"}],"predecessor-version":[{"id":2808,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2798\/revisions\/2808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/807"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}