git remove file from repo
https://stackoverflow.com/questions/2047465/how-do-i-delete-a-file-from-a-git-repository
How do I delete a file from a Git repository?
2022-08-26
Webminal Org Webminal Org
<div><div>
 
 
<div>
 
 
<div>
 
 <div>
<p>Use <a href="https://git-scm.com/docs/git-rm"><code>git rm</code></a>.</p>
<p>If you want to remove the file from the Git repository <strong>and the filesystem</strong>, use:</p>
<pre><code>git rm file1.txt
git commit -m "remove file1.txt"
</code></pre>
<p>But if you want to remove the file only from the Git repository and not remove it from the filesystem, use: </p>
<pre><code>git rm --cached file1.txt
git commit -m "remove file1.txt"
</code></pre>
<p>And to push changes to remote repo</p>
<pre><code>git push origin branch_name
</code></pre>
 </div>
 <div>
 
 <div>
 
 <div>
 <a href="/users/8583669/johannchopin"><p></p></a>
 </div>
 <div>
 <p><a href="/users/8583669/johannchopin">johannchopin</a></p><p>13.1k9 gold badges51 silver badges99 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 answered Jan 12, 2010 at 7:52
 </p>
 <div>
 <a href="/users/893/greg-hewgill"><p></p></a>
 </div>
 <div>
 <p><a href="/users/893/greg-hewgill">Greg Hewgill</a>Greg Hewgill</p><p>927k180 gold badges1136 silver badges1275 bronze badges
 </p>
 </div>
</div>
 </div>
 
</div>
 <p>20</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p><code>git rm file.txt</code> removes the file from the repo but <strong>also deletes it from the local file system</strong>.</p>
<p>To remove the file from the repo and <strong>not</strong> delete it from the local file system use:<br />
<code>git rm --cached file.txt</code> </p>
<p>The below exact situation is where I use git to maintain version control for my business's website, but the "mickey" directory was a tmp folder to share private content with a CAD developer. When he needed HUGE files, I made a private, unlinked directory and ftpd the files there for him to fetch via browser. Forgetting I did this, I later performed a <code>git add -A</code> from the website's base directory. Subsequently, <code>git status</code> showed the new files needing committing. Now I needed to delete them from git's tracking and version control...</p>
<p>Sample output below is from what just happened to me, where I unintentionally deleted the <code>.003</code> file. Thankfully, I don't care what happened to the local copy to <code>.003</code>, but some of the other currently changed files were updates I just made to the website and would be epic to have been deleted on the local file system! "Local file system" = the live website <em>(not a great practice, but is reality)</em>.</p>
<pre><code>[~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003
error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications
(use --cached to keep the file, or -f to force removal)
[~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003
rm 'shop/mickey/mtt_flange_SCN.7z.003'
[~/www]$ 
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD &lt;file&gt;..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# (use "git add &lt;file&gt;..." to update what will be committed)
# (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)
#
# modified: shop/mickey/mtt_flange_SCN.7z.001
# modified: shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ 
[~/www]$ 
[~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002
rm 'shop/mickey/mtt_flange_SCN.7z.002'
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ 
[~/www]$ 
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD &lt;file&gt;..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.002
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# modified: shop/mickey/mtt_flange_SCN.7z.001
[~/www]$
</code></pre>
<p><strong>Update:</strong> This answer is getting some traffic, so I thought I'd mention my <a href="https://stackoverflow.com/questions/9069061">other Git answer</a> shares a couple of great resources: <a href="http://nvie.com/posts/a-successful-git-branching-model/">This page</a> has a graphic that help demystify Git for me. The <a href="http://git-scm.com/book">"Pro Git" book is online</a> and helps me a lot.</p>
 </div>
 <div>
 
 <div>
 <a href="/users/-1/community"><p></p></a>
 </div>
 <div>
 <p>
 answered May 25, 2013 at 20:26
 </p>
 <div>
 <a href="/users/946112/krista-k"><p></p></a>
 </div>
 
</div>
 </div>
 
</div>
 <p>1</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p>First, if you are using <a href="https://git-scm.com/docs/git-rm"><code>git rm</code></a>, especially for multiple files, consider any wildcard will be resolved by the shell, not by the <code>git</code> command.</p>
<pre><code>git rm -- *.anExtension
git commit -m "remove multiple files"
</code></pre>
<p>But, if your file is already on GitHub, <a href="https://github.com/blog/1545-deleting-files-on-github">you can (since July 2013)</a> directly delete it from the web GUI!</p>
<blockquote>
<p>Simply view any file in your repository, click the trash can icon at the top, and commit the removal just like any other web-based edit.</p>
</blockquote>
<p>Then "<code>git pull</code>" on your local repo, and that will delete the file locally too.<br />
Which makes this answer a (roundabout) way to delete a file from git repo?<br />
(Not to mention that a file on GitHub <em>is</em> in a "git repo")</p>
<hr />
<p><a href="https://i.stack.imgur.com/259au.jpg"></a></p>
<p>(the commit will reflect the deletion of that file):</p>
<p></p>
<blockquote>
<p>And just like that, it’s gone.</p>
<p>For help with these features, be sure to read our help articles on <a href="https://help.github.com/articles/creating-new-files">creating</a>, <a href="https://help.github.com/articles/moving-a-file-to-a-new-location">moving</a>, <a href="https://help.github.com/articles/renaming-a-file">renaming</a>, and <a href="https://help.github.com/articles/deleting-files">deleting</a> files.</p>
<p>Note: Since it’s a version control system, Git always has your back if you need to recover the file later.</p>
</blockquote>
<p>The last sentence means that the deleted file is still part of the history, and you can restore it easily enough (but not yet through the GitHub web interface):</p>
<p>See "<a href="https://stackoverflow.com/q/953481/6309">Restore a deleted file in a Git repo</a>".</p>
 </div>
 <div>
 
 <div>
 <a href="/users/-1/community"><p></p></a>
 </div>
 <div>
 <p>
 answered Jul 4, 2013 at 20:53
 </p>
 <div>
 <a href="/users/6309/vonc"><p></p></a>
 </div>
 <div>
 <p><a href="/users/6309/vonc">VonC</a>VonC</p><p>1.2m508 gold badges4244 silver badges5066 bronze badges
 </p>
 </div>
</div>
 </div>
 
</div>
 <p>7</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p>This is the only option that worked for me.</p>
<pre><code>git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'
</code></pre>
<p>Note: Replace *.sql with your file name or file type. Be very careful because this will go through every commit and rip this file type out. </p>
<p>EDIT:
pay attention - after this command you will not be able to push or pull - you will see the reject of 'unrelated history' you can use 'git push --force -u origin master' to push or pull</p>
 </div>
 <div>
 
 <div>
 <a href="/users/2613863/yehonatan-yehezkel"><p></p></a>
 </div>
 <div>
 <p>
 answered Feb 27, 2015 at 19:29
 </p>
 <div>
 <a href="/users/1701138/jason-glisson"><p></p></a>
 </div>
 
</div>
 </div>
 
</div>
 <p>9</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p>Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:</p>
<pre><code>git rm -r foldername
</code></pre>
 </div>
 <div>
 
 <div>
 
 <div>
 <a href="/users/912130/jazi"><p></p></a>
 </div>
 <div>
 <p><a href="/users/912130/jazi">Jazi</a></p><p>6,55413 gold badges58 silver badges91 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 answered Apr 17, 2015 at 15:41
 </p>
 <div>
 <a href="/users/3999718/raja"><p></p></a>
 </div>
 <div>
 <p><a href="/users/3999718/raja">Raja</a>Raja</p><p>6857 silver badges7 bronze badges
 </p>
 </div>
</div>
 </div>
 
</div>
 <p>1</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p>More generally, <code>git help</code> will help with at least simple questions like this:</p>
<pre><code>zhasper@berens:/media/Kindle/documents$ git help
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]
The most commonly used git commands are:
 add Add file contents to the index
 :
 rm Remove files from the working tree and from the index
</code></pre>
 </div>
 <div>
 <p>
 answered Jan 12, 2010 at 8:21
 </p>
 <div>
 <a href="/users/128625/james-polley"><p></p></a>
 </div>
 <div>
 <p><a href="/users/128625/james-polley">James Polley</a>James Polley</p><p>7,7892 gold badges28 silver badges32 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>3</p>
 </div>
 
<div>
 
 <div>
<p>If you want to delete the file from the repo, but leave it in the the file system (will be untracked):</p>
<pre><code>bykov@gitserver:~/temp&gt; git rm --cached file1.txt
bykov@gitserver:~/temp&gt; git commit -m "remove file1.txt from the repo"
</code></pre>
<p>If you want to delete the file from the repo and from the file system then there are two options:</p>
<ol>
<li><p>If the file has no changes staged in the index: </p>
<pre><code>bykov@gitserver:~/temp&gt; git rm file1.txt
bykov@gitserver:~/temp&gt; git commit -m "remove file1.txt"
</code></pre></li>
<li><p>If the file has changes staged in the index: </p>
<pre><code>bykov@gitserver:~/temp&gt; git rm -f file1.txt
bykov@gitserver:~/temp&gt; git commit -m "remove file1.txt"
</code></pre></li>
</ol>
 </div>
 <div>
 <p>
 answered Mar 20, 2015 at 12:13
 </p>
 
 
</div>
 
</div>
 
<div>
 
 
<div>
 
 <div>
<p><code>git rm</code> will only remove the file on this branch from now on, but it remains in history and git will remember it.</p>
<p>The right way to do it is with <code>git filter-branch</code>, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.</p>
<p>But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.</p>
<p>If you want to completely obliterate it in one step, I recommend you to use <code>git forget-blob</code></p>
<p><a href="https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/">https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/</a></p>
<p>It is easy, just do <code>git forget-blob file1.txt</code>.</p>
<p>This will remove every reference, do <code>git filter-branch</code>, and finally run the git garbage collector <code>git gc</code> to completely get rid of this file in your repo.</p>
 </div>
 <div>
 <p>
 answered Jan 23, 2017 at 12:56
 </p>
 <div>
 <a href="/users/7445849/nachoparker"><p></p></a>
 </div>
 <div>
 <p><a href="/users/7445849/nachoparker">nachoparker</a>nachoparker</p><p>1,63218 silver badges14 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>1</p>
 </div>
 
<div>
 
 <div>
<p>Note: if you want to delete file only from git use below:</p>
<pre><code>git rm --cached file1.txt
</code></pre>
<p>If you want to delete also from hard disk:</p>
<pre><code>git rm file1.txt
</code></pre>
<p>If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:</p>
<pre><code>git rm -r foldername
</code></pre>
<p>If you want to remove a folder inside another folder</p>
<pre><code>git rm -r parentFolder/childFolder
</code></pre>
<p>Then, you can <code>commit</code> and <code>push</code> as usual. However, if you want to recover deleted folder, you can follow this: <a href="https://stackoverflow.com/questions/36162686/recover-directory-removed-with-git-rm-rf">recover deleted files from git is possible.</a></p>
<blockquote>
 <p><strong>From doc:</strong></p>
<pre><code>git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] &lt;file&gt;…​
</code></pre>
 
 <p><strong>OPTIONS</strong></p>
 
 <p><strong><code>&lt;file&gt;…​</code></strong></p>
<pre><code>Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want Git to expand file glob characters, you
</code></pre>
 
 <p>may need to shell-escape them. A leading directory name (e.g. dir to
 remove dir/file1 and dir/file2) can be given to remove all files in
 the directory, and recursively all sub-directories, but this requires
 the -r option to be explicitly given.
 <strong><code>-f</code></strong>
 <strong><code>--force</code></strong></p>
<pre><code>Override the up-to-date check.
</code></pre>
 
 <p><strong><code>-n</code></strong>
 <strong><code>--dry-run</code></strong></p>
<pre><code>Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.
</code></pre>
 
 <p><strong><code>-r</code></strong></p>
<pre><code>Allow recursive removal when a leading directory name is given.
</code></pre>
 
 <p><strong><code>--</code></strong> </p>
<pre><code>This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for
</code></pre>
 
 <p>command-line options).
 <strong><code>--cached</code></strong></p>
<pre><code>Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
</code></pre>
 
 <p>--ignore-unmatch</p>
<pre><code>Exit with a zero status even if no files matched.
</code></pre>
 
 <p><strong><code>-q</code></strong>
 <strong><code>--quiet</code></strong></p>
<pre><code>git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.
</code></pre>
</blockquote>
<p><a href="https://git-scm.com/docs/git-rm">Read more on official doc.</a></p>
 </div>
 <div>
 <p>
 answered Oct 7, 2018 at 2:57
 </p>
 <div>
 <a href="/users/3675035/blasanka"><p></p></a>
 </div>
 <div>
 <p><a href="/users/3675035/blasanka">Blasanka</a>Blasanka</p><p>19.9k11 gold badges97 silver badges103 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <div>
<p>The answer by <a href="https://stackoverflow.com/users/893/greg-hewgill">Greg Hewgill</a>, that was edited by <a href="https://stackoverflow.com/users/8583669/johannchopin">Johannchopin</a> helped me, as I did not care about removing the file from the history completely.
In my case, it was a directory, so the only change I did was using:</p>
<pre><code>git rm -r --cached myDirectoryName
</code></pre>
<p>instead of "git rm --cached file1.txt"
..followed by:</p>
<pre><code>git commit -m "deleted myDirectoryName from git"
git push origin branch_name
</code></pre>
<p>Thanks <a href="https://stackoverflow.com/users/893/greg-hewgill">Greg Hewgill</a> and <a href="https://stackoverflow.com/users/8583669/johannchopin">Johannchopin</a>!</p>
 </div>
 <div>
 <p>
 answered Oct 3, 2020 at 9:14
 </p>
 <div>
 <a href="/users/13506060/wat-er"><p></p></a>
 </div>
 <div>
 <p><a href="/users/13506060/wat-er">wat er</a>wat er</p><p>1911 silver badge5 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <div>
<p>Another way if you want to delete the file from your local folder using rm command and then push the changes to the remote server.</p>
<pre><code>rm file1.txt
git commit -a -m "Deleting files"
git push origin master
</code></pre>
 </div>
 <div>
 <p>
 answered Oct 6, 2015 at 3:07
 </p>
 <div>
 <a href="/users/1778674/suhaib"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1778674/suhaib">Suhaib</a>Suhaib</p><p>2,0313 gold badges24 silver badges35 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <div>
<p>According to the <a href="https://help.github.com/en/github/managing-large-files/removing-files-from-a-repositorys-history">documentation</a>.</p>
<p><code> git rm --cached file1.txt</code></p>
<p>When it comes to sensitive data—better not say that you removed the file but rather just include it in the last known commit:</p>
<p><strong>0. Amend last commit</strong></p>
<pre><code>git commit --amend -CHEAD
</code></pre>
<p>If you want to delete the file from all git history, according to the <a href="https://help.github.com/en/github/authenticating-to-github/removing-sensitive-data-from-a-repository">documentation</a> you should do the following:</p>
<p><strong>1. Remove it from your local history</strong></p>
<pre><code>git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \ --prune-empty --tag-name-filter cat -- --all
# Replace PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename
</code></pre>
<ol>
<li><strong>Don't forget to include this file in .gitignore</strong> (If it's a file you never want to share (such as passwords...):</li>
</ol>
<pre><code>echo "YOUR-FILE-WITH-SENSITIVE-DATA" &gt;&gt; .gitignore
git add .gitignore
git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
</code></pre>
<p><strong>3. If you need to remove from the remote</strong></p>
<pre><code>git push origin --force --all
</code></pre>
<p><strong>4. If you also need to remove it from tag releases:</strong></p>
<pre><code>git push origin --force --tags
</code></pre>
 </div>
 <div>
 <p>
 answered May 16, 2020 at 21:08
 </p>
 <div>
 <a href="/users/7200875/tomas-baran"><p></p></a>
 </div>
 <div>
 <p><a href="/users/7200875/tomas-baran">Tomas Baran</a>Tomas Baran</p><p>1,70318 silver badges33 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <div>
<p>In my case I tried to remove file on github after few commits but save on computer</p>
<pre><code>git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master
</code></pre>
<p>and later this file was ignored</p>
 </div>
 <div>
 <p>
 answered Aug 23, 2016 at 12:03
 </p>
 <div>
 <a href="/users/6747787/olegv"><p></p></a>
 </div>
 <div>
 <p><a href="/users/6747787/olegv">OlegV</a>OlegV</p><p>711 silver badge1 bronze badge
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 
<div>
 
 <div>
<p>To delete a specific file</p>
<blockquote>
 <p>git rm filename</p>
</blockquote>
<p>To clean all the untracked files from a directory recursively in single shot</p>
<blockquote>
 <p>git clean -fdx</p>
</blockquote>
 </div>
 <div>
 <p>
 answered Aug 23, 2016 at 13:56
 </p>
 <div>
 <a href="/users/1382284/sitesh"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1382284/sitesh">Sitesh</a>Sitesh</p><p>1,7161 gold badge17 silver badges24 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>3</p>
 </div>
 
<div>
 
 <div>
<p>If you have the GitHub for Windows application, you can delete a file in 5 easy steps:</p>
<ul>
<li>Click Sync.</li>
<li>Click on the directory where the file is located and select your latest version of the file.</li>
<li>Click on tools and select "Open a shell here."</li>
<li>In the shell, type: "rm {filename}" and hit enter.</li>
<li>Commit the change and resync.</li>
</ul>
 </div>
 <div>
 <p>
 answered Mar 14, 2013 at 15:15
 </p>
 <div>
 <a href="/users/2044764/jushusted"><p></p></a>
 </div>
 <div>
 <p><a href="/users/2044764/jushusted">jushusted</a>jushusted</p><p>671 silver badge4 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 
<div>
 
 <div>
<ol>
<li><p>First,Remove files from local repository.</p>
<blockquote>
 <p>git rm -r File-Name</p>
</blockquote>
<p>or, remove files only from local repository but from filesystem</p>
<blockquote>
 <p>git rm --cached File-Name</p>
</blockquote></li>
<li><p>Secondly, Commit changes into local repository.</p>
<pre><code>git commit -m "unwanted files or some inline comments" 
</code></pre></li>
<li><p>Finally, update/push local changes into remote repository.</p>
<pre><code>git push 
</code></pre></li>
</ol>
 </div>
 <div>
 <p>
 answered Oct 18, 2016 at 4:00
 </p>
 <div>
 <a href="/users/2685581/ajay-kumar"><p></p></a>
 </div>
 <div>
 <p><a href="/users/2685581/ajay-kumar">Ajay Kumar</a>Ajay Kumar</p><p>4,54238 silver badges42 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>2</p>
 </div>
 
<div>
 
 <div>
<p>go to your project dir and type:</p>
<pre><code>git filter-branch --tree-filter 'rm -f &lt;deleted-file&gt;' HEAD
</code></pre>
<p>after that push --force for delete file from all commits.</p>
<pre><code>git push origin --force --all
</code></pre>
 </div>
 <div>
 <p>
 answered Sep 30, 2020 at 0:52
 </p>
 <div>
 <a href="/users/13849027/rojen"><p></p></a>
 </div>
 <div>
 <p><a href="/users/13849027/rojen">rojen</a>rojen</p><p>3493 silver badges4 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 
<div>
 
 <div>
<p><a href="https://stackoverflow.com/a/2047477/14508423">https://stackoverflow.com/a/2047477/14508423</a>
<a href="https://stackoverflow.com/a/19666677/14508423">https://stackoverflow.com/a/19666677/14508423</a>
Quite similar, tried both. Nothing. Git keeps tracking the file. (I use vscode so the file got instantly marked as U(untracked) or A(added))</p>
<p>Doing this <a href="https://stackoverflow.com/a/53431148/14508423">https://stackoverflow.com/a/53431148/14508423</a> to whole category solves the problem.</p>
<p>Do this to delete a file from git and make git forget about it:</p>
<pre><code>**manually add the file name to .gitignore**
git rm --cached settings.py 
git commit -m "remove settings.py"
git push origin master
git rm -r --cached .
git add .
git commit -m "Untrack files in .gitignore"
</code></pre>
<p>ps: every time it failed I tried to delete file with sensitive info like tokens (.env and settings.py files). But it works fine with random files oO</p>
 </div>
 <div>
 <p>
 answered Aug 5, 2022 at 13:46
 </p>
 <div>
 <a href="/users/14508423/void-eater"><p></p></a>
 </div>
 <div>
 <p><a href="/users/14508423/void-eater">void_eater</a>void_eater</p><p>831 silver badge8 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>1</p>
 </div>
 
<div>
 
 <div>
<p>For the case where <code>git rm</code> doesn't suffice and the file needs to be removed from history: As the <code>git filter-branch</code> manual page now itself suggests using <a href="https://github.com/newren/git-filter-repo">git-filter-repo</a>, and I had to do this today, here's an example using that tool. It uses the example repo <code>https://example/eguser/eg.git</code></p>
<ol>
<li><p>Clone the repository into a new directory <code>git clone https://example/eguser/eg.git</code></p>
</li>
<li><p>Keep everything except the unwanted file. <code>git-filter-repo --path file1.txt --invert-paths</code></p>
</li>
<li><p>Add the remote repository origin back :
<code>git remote add origin https://example/eguser/eg.git</code>.
The <code>git-filter-repo</code> tool removes remote remote info by design and suggests <a href="https://htmlpreview.github.io/?https://github.com/newren/git-filter-repo/blob/docs/html/git-filter-repo.html#DISCUSSION">a new remote repo</a> (see point 4). This makes sense for big shared repos but might be overkill for getting rid a single newly added file as in this example.</p>
</li>
<li><p>When happy with the contents of local, replace remote with it.</p>
<p><code>git push --force -u origin master</code>. Forcing is required due to the changed history.</p>
</li>
</ol>
<p>Also note the useful <code>--dry-run</code> option and a good discussion in the linked manual on team and project dynamics before charging in and changing repository history.</p>
 </div>
 <div>
 <p>
 answered Sep 7, 2020 at 1:14
 </p>
 <div>
 <a href="/users/5729872/adam-burke"><p></p></a>
 </div>
 <div>
 <p><a href="/users/5729872/adam-burke">Adam Burke</a>Adam Burke</p><p>6021 gold badge6 silver badges20 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 
<div>
 
 <div>
<p>I tried a lot of the suggested options and none appeared to work (I won't list the various problems). 
What I ended up doing, which worked, was simple and intuitive (to me) was:</p>
<ol>
<li>move the whole local repo elsewhere</li>
<li>clone the repo again from master to your local drive</li>
<li>copy back the files/folder from your original copy in #1 back into the new clone from #2</li>
<li>make sure that the problem large file is either not there or excluded in the .gitignore file</li>
<li>do the usual git add/git commit/git push</li>
</ol>
 </div>
 <div>
 <p>
 answered Mar 21, 2018 at 18:22
 </p>
 <div>
 <a href="/users/765827/jacanterbury"><p></p></a>
 </div>
 <div>
 <p><a href="/users/765827/jacanterbury">jacanterbury</a>jacanterbury</p><p>1,3651 gold badge24 silver badges34 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>0</p>
 </div>
 
<div>
 
 <p>After you have removed the file from the repo with <code>git rm</code> you can use <a href="https://rtyley.github.io/bfg-repo-cleaner/">BFG Repo-Cleaner</a> to completely and easily obliterate the file from the repo history.</p>
 <div>
 <p>
 answered Sep 30, 2018 at 17:26
 </p>
 <div>
 <a href="/users/8572174/glisu"><p></p></a>
 </div>
 <div>
 <p><a href="/users/8572174/glisu">glisu</a>glisu</p><p>1,0069 silver badges20 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <p>Just by going on the file in your github repository you can see the delete icon beside Raw|Blame and don't forget to click on commit changes button. And you can see that your file has been deleted.</p>
 <div>
 <p>
 answered Sep 3, 2020 at 18:45
 </p>
 <div>
 <a href="/users/14170885/nitesh-mishra"><p></p></a>
 </div>
 
</div>
 
</div>
 
<div>
 
 <div>
<p>New answer that works in 2022.</p>
<p>Do not use:</p>
<pre><code>git filter-branch
</code></pre>
<p>this command might not change the remote repo after pushing. If you clone after using it, you will see that nothing has changed and the repo still has a large size. This command is old now. For example, if you use the steps in <a href="https://github.com/18F/C2/issues/439">https://github.com/18F/C2/issues/439</a>, this won't work.</p>
<p>You need to use</p>
<pre><code>git filter-repo
</code></pre>
<p>Steps:</p>
<p>(1) Find the largest files in .git:</p>
<pre><code>git rev-list --objects --all | grep -f &lt;(git verify-pack -v .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)
</code></pre>
<p>(2) Strat filtering these large files:</p>
<pre><code> git filter-repo --path-glob '../../src/../..' --invert-paths --force
</code></pre>
<p>or</p>
<pre><code> git filter-repo --path-glob '*.zip' --invert-paths --force
</code></pre>
<p>or</p>
<pre><code> git filter-repo --path-glob '*.a' --invert-paths --force
</code></pre>
<p>or
whatever you find in step 1.</p>
<p>(3)</p>
<pre><code> git remote add origin git@github.com:.../...git
</code></pre>
<p>(4)</p>
<pre><code>git push --all --force
git push --tags --force
</code></pre>
<p>DONE!</p>
 </div>
 <div>
 <p>
 answered Nov 27, 2022 at 17:09
 </p>
 <div>
 <a href="/users/6937857/joe"><p></p></a>
 </div>
 <div>
 <p><a href="/users/6937857/joe">Joe</a>Joe</p><p>1892 silver badges9 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <div>
<p>I have obj and bin files that accidentally made it into the repo that I don't want polluting my 'changed files' list</p>
<p><em>After</em> I noticed they went to the remote, I ignored them by adding this to <code>.gitignore</code></p>
<pre><code>/*/obj
/*/bin
</code></pre>
<p>Problem is they are already in the remote, and when they get changed, they pop up as changed and pollute the changed file list. </p>
<p>To stop seeing them, you need to delete the whole folder from the remote repository.</p>
<p>In a command prompt:</p>
<ol>
<li>CD to the repo folder (i.e. <code>C:\repos\MyRepo</code>)</li>
<li>I want to delete SSIS\obj. It seems you can only delete at the top level, so you now need to CD into SSIS: (i.e. <code>C:\repos\MyRepo\SSIS</code>) </li>
<li>Now type the magic incantation <code>git rm -r -f obj</code>
<ul>
<li>rm=remove</li>
<li>-r = recursively remove</li>
<li>-f = means force, cause you really mean it</li>
<li>obj is the folder</li>
</ul></li>
<li>Now run <code>git commit -m "remove obj folder"</code></li>
</ol>
<p>I got an alarming message saying <em>13 files changed 315222 deletions</em></p>
<p>Then because I didn't want to have to look up the CMD line, I went into Visual Sstudio and did a Sync to apply it to the remote</p>
 </div>
 <div>
 <p>
 answered Jul 27, 2018 at 1:59
 </p>
 <div>
 <a href="/users/1690193/nick-mcdermaid"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1690193/nick-mcdermaid">Nick.McDermaid</a>Nick.McDermaid</p><p>17.6k6 gold badges55 silver badges89 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <div>
<p>if your file is sensitive (for example, settings or keys you accidently added and committed) then you can remove it from <strong>all</strong> versions.</p>
<p>To remove from <strong>all</strong> versions use the command below (<strong>warning:</strong> careful because you won't be able to restore the removed file in the repo if do not have a copy):</p>
<ol>
<li>Using <code>Git</code></li>
</ol>
<pre><code>git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch file.ext' HEAD
</code></pre>
<ol>
<li>Using <code>git-filter-repo</code> (get it <a href="https://github.com/newren/git-filter-repo">here</a>, new recommended way by Git)</li>
</ol>
<pre><code>git filter-repo --path file.ext
</code></pre>
 </div>
 <div>
 <p>
 answered Jul 9, 2022 at 16:00
 </p>
 <div>
 <a href="/users/712093/eugene"><p></p></a>
 </div>
 <div>
 <p><a href="/users/712093/eugene">Eugene</a>Eugene</p><p>2,92018 silver badges22 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <div>
<p>If you need to remove files from a determined extension (for example, compiled files) you could do the following to remove them all at once:</p>
<pre><code>git remove -f *.pyc
</code></pre>
 </div>
 <div>
 <p>
 answered Oct 1, 2019 at 5:21
 </p>
 <div>
 <a href="/users/5384551/sergi-ram%c3%b3n"><p></p></a>
 </div>
 
</div>
 
</div>
 
<div>
 
 <p>Incase if you don't file in your local repo but in git repo, then simply open file in git repo through web interface and find Delete button at right corner in interface. 
<a href="https://i.stack.imgur.com/HbEzs.png">Click Here, To view interface Delete Option</a></p>
 <div>
 <p>
 answered Nov 30, 2016 at 15:47
 </p>
 <div>
 <a href="/users/7150225/poseidon"><p></p></a>
 </div>
 
</div>
 
</div>
 
 <h2>
 
 </h2>
 </div>||||I|||| Stack Overflow
 1. About
 2. Products
 3. For Teams
 1. Stack Overflow Public questions & answers
 2. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
 3. Talent Build your employer brand
 4. Advertising Reach developers & technologists worldwide
 5. About the company
 Loading…

 1. current community

 + Stack Overflow
 help chat
 + Meta Stack Overflow

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 2. 
 3. Log in
 4. Sign up
 1. Home
 2. 
 1. Public
 2. Questions
 3. Tags
 4. Users
 5. Companies
 6. Collectives
 7. Explore Collectives
 3. 
 1. Teams
 Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
 2. Teams
 3. Create free Team

 Collectives™ on Stack Overflow

 Find centralized, trusted content and collaborate around the technologies you use most.

 Learn more about Collectives

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 How do I delete a file from a Git repository?

 Ask Question
 Asked 13 years ago
 Modified 2 months ago
 Viewed 3.1m times
 2435

 How can I delete "file1.txt" from my repository?

 * git
 * git-rm
 Share
 Improve this question
 Follow
 edited Aug 26, 2022 at 20:36
 John Smith
 7,143 6 6 gold badges 48 48 silver badges 61 61 bronze badges
 asked Jan 12, 2010 at 7:48
 webminal.org webminal.org
 43.7k 37 37 gold badges 92 92 silver badges 125 125 bronze badges
 5
 * 115
 git rm is the right answer, but remember that the file will still be there in history. If you want to remove a file because it had sensitive information, you'll need to do something more drastic. (Changing history, especially for content you've already pushed, is a drastic action, and should be avoided if possible.)
 – Keith Thompson
 May 16, 2013 at 21:06
 * 11
 Note: on GitHub, you now can directly delete a file from the web interface (without having to even clone the repo). See my answer below .
 – VonC
 Jul 4, 2013 at 20:53
 * 6
 @KeithThompson what steps might that be if I desperately want to do that?
 – lessthanl0l
 Feb 21, 2014 at 19:09
 * 7
 @lessthanl0l: stackoverflow.com/q/872565/827263
 – Keith Thompson
 Feb 21, 2014 at 19:19
 * related help.github.com/articles/…
 – Trevor Boyd Smith
 Jun 21, 2018 at 18:59
 Add a comment |

 27 Answers

 Sorted by: Reset to default
 Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
 4050

 Use git rm.

 If you want to remove the file from the Git repository and the filesystem, use:

 git rm file1.txt
 git commit -m "remove file1.txt"
 

 But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:

 git rm --cached file1.txt
 git commit -m "remove file1.txt"
 

 And to push changes to remote repo

 git push origin branch_name
 
 Share
 Improve this answer
 Follow
 edited Jun 19, 2020 at 10:02
 johannchopin
 13.1k 9 9 gold badges 51 51 silver badges 99 99 bronze badges
 answered Jan 12, 2010 at 7:52
 Greg Hewgill Greg Hewgill
 927k 180 180 gold badges 1136 1136 silver badges 1275 1275 bronze badges
 20
 * 146
 Also handy: git rm -r directory // To remove directory and content
 – Reg
 Nov 15, 2014 at 15:02
 * 17
 That's not going to get rid of that file in other commits though.
 – VaTo
 Jun 16, 2015 at 23:32
 * 11
 @SaulOrtega: That's correct. To remove a file from previous commits (changing past history), see GitHub's help page on Remove sensitive data .
 – Greg Hewgill
 Jun 16, 2015 at 23:43
 * 21
 Take note this will delete the file locally too. If you only want to delete it from the repo do: git rm --cached file1.txt
 – Weston Ganger
 Aug 19, 2015 at 22:44
 * 8
 It's worth noting that if that file contained sensitive information (e.g. credentials) you should change those credentials immediately . To quote GitHub "Once you have pushed a commit to GitHub, you should consider any data it contains to be compromised. If you committed a password, change it! If you committed a key, generate a new one."
 – c1moore
 Sep 14, 2016 at 20:51
 | Show 15 more comments
 648

 git rm file.txt removes the file from the repo but also deletes it from the local file system.

 To remove the file from the repo and not delete it from the local file system use:
 git rm --cached file.txt

 The below exact situation is where I use git to maintain version control for my business's website, but the "mickey" directory was a tmp folder to share private content with a CAD developer. When he needed HUGE files, I made a private, unlinked directory and ftpd the files there for him to fetch via browser. Forgetting I did this, I later performed a git add -A from the website's base directory. Subsequently, git status showed the new files needing committing. Now I needed to delete them from git's tracking and version control...

 Sample output below is from what just happened to me, where I unintentionally deleted the .003 file. Thankfully, I don't care what happened to the local copy to .003, but some of the other currently changed files were updates I just made to the website and would be epic to have been deleted on the local file system! "Local file system" = the live website (not a great practice, but is reality).

 [~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003
 error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications
 (use --cached to keep the file, or -f to force removal)
 [~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003
 rm 'shop/mickey/mtt_flange_SCN.7z.003'
 [~/www]$ 
 [~/www]$ git status
 # On branch master
 # Changes to be committed:
 # (use "git reset HEAD <file>..." to unstage)
 #
 # deleted: shop/mickey/mtt_flange_SCN.7z.003
 #
 # Changed but not updated:
 # (use "git add <file>..." to update what will be committed)
 # (use "git checkout -- <file>..." to discard changes in working directory)
 #
 # modified: shop/mickey/mtt_flange_SCN.7z.001
 # modified: shop/mickey/mtt_flange_SCN.7z.002
 [~/www]$ ls shop/mickey/mtt_flange_S*
 shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
 [~/www]$ 
 [~/www]$ 
 [~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002
 rm 'shop/mickey/mtt_flange_SCN.7z.002'
 [~/www]$ ls shop/mickey/mtt_flange_S*
 shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
 [~/www]$ 
 [~/www]$ 
 [~/www]$ git status
 # On branch master
 # Changes to be committed:
 # (use "git reset HEAD <file>..." to unstage)
 #
 # deleted: shop/mickey/mtt_flange_SCN.7z.002
 # deleted: shop/mickey/mtt_flange_SCN.7z.003
 #
 # Changed but not updated:
 # modified: shop/mickey/mtt_flange_SCN.7z.001
 [~/www]$
 

 Update: This answer is getting some traffic, so I thought I'd mention my other Git answer shares a couple of great resources: This page has a graphic that help demystify Git for me. The "Pro Git" book is online and helps me a lot.

 Share
 Improve this answer
 Follow
 edited May 23, 2017 at 12:02
 Community Bot
 1 1 1 silver badge
 answered May 25, 2013 at 20:26
 Krista K Krista K
 21.2k 3 3 gold badges 31 31 silver badges 42 42 bronze badges
 1
 * 6
 You'd then need to commit this with something like git commit -m "Just removing file1.txt" and then, if you have a remote repository, push the commit with something like git push origin master .
 – ban-geoengineering
 Jun 10, 2014 at 18:13
 Add a comment |
 103

 First, if you are using git rm, especially for multiple files, consider any wildcard will be resolved by the shell, not by the git command.

 git rm -- *.anExtension
 git commit -m "remove multiple files"
 

 But, if your file is already on GitHub, you can (since July 2013) directly delete it from the web GUI!

 Simply view any file in your repository, click the trash can icon at the top, and commit the removal just like any other web-based edit.

 Then "git pull" on your local repo, and that will delete the file locally too.
 Which makes this answer a (roundabout) way to delete a file from git repo?
 (Not to mention that a file on GitHub is in a "git repo")

 (the commit will reflect the deletion of that file):

 And just like that, it’s gone.

 For help with these features, be sure to read our help articles on creating, moving, renaming, and deleting files.

 Note: Since it’s a version control system, Git always has your back if you need to recover the file later.

 The last sentence means that the deleted file is still part of the history, and you can restore it easily enough (but not yet through the GitHub web interface):

 See "Restore a deleted file in a Git repo".

 Share
 Improve this answer
 Follow
 edited Jun 20, 2020 at 9:12
 Community Bot
 1 1 1 silver badge
 answered Jul 4, 2013 at 20:53
 VonC VonC
 1.2m 508 508 gold badges 4244 4244 silver badges 5066 5066 bronze badges
 7
 * 4
 You have to scroll to the bottom and hit "Commit changes" to commit the delete!
 – LevinsonTechnologies
 Aug 18, 2015 at 19:16
 * 1
 This does not work for large text files. The server tries to render the file as in the screenshot and then fails with a 500 error.
 – CapeCoder
 Aug 31, 2016 at 19:37
 * 12
 This answer is refering to github, the question is about a git repo. Not how to use the github interface.
 – Peter Marshall
 Apr 12, 2017 at 9:46
 * 1
 @Sergo That will not delete the files from past commits though. For that, use git-filter-repo, as in here . And a git push --force (so make sure other collaborators know about your new repository history).
 – VonC
 Aug 5, 2022 at 20:03
 * 1
 @Sergo OK, no filter-repo then.
 – VonC
 Aug 6, 2022 at 20:38
 | Show 2 more comments
 86

 This is the only option that worked for me.

 git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'
 

 Note: Replace *.sql with your file name or file type. Be very careful because this will go through every commit and rip this file type out.

 EDIT: pay attention - after this command you will not be able to push or pull - you will see the reject of 'unrelated history' you can use 'git push --force -u origin master' to push or pull

 Share
 Improve this answer
 Follow
 edited Feb 8, 2019 at 16:59
 yehonatan yehezkel
 1,026 16 16 silver badges 26 26 bronze badges
 answered Feb 27, 2015 at 19:29
 Jason Glisson Jason Glisson
 1,021 7 7 silver badges 10 10 bronze badges
 9
 * 15
 This is a great answer, but note that in contrast to the other answers, this command rewrites the commit history and removes all trace of the files matching the pattern. This is particularly useful if you found you accidentally committed some sensitive information (and, if needed can successfully use --force to rewrite history of any remote repos this has been pushed to).
 – yoniLavi
 Apr 28, 2015 at 0:51
 * 3
 That was exactly the problem that I had and it took some trial and error to get it right. A guy that I was working with had committed several large database dumps into a repo going back through many many commits. Which is why I used .sql in my example. My repo got so bloated and large that github wouldn't accept pushes anymore.
 – Jason Glisson
 May 19, 2015 at 18:52
 * 3
 If you are using a remote master you need to force a commit and push it up with git push --force -u origin master
 – AlbertMarkovski
 Aug 31, 2016 at 22:56
 * 2
 @AlbertMarkovski Sorry I wasn't specific. Github actually wouldn't accept the push because the repo was beyond their size limit. At least several GBs. They cut me off and emailed me about it. So, unfortunately, no amount of forcing a push would help at that point.
 – Jason Glisson
 Sep 8, 2016 at 18:12
 * 2
 @JasonGlisson I've also faced that issue where a video got added to a repository by accident.
 – AlbertMarkovski
 Sep 20, 2016 at 19:53
 | Show 4 more comments
 42

 Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:

 git rm -r foldername
 
 Share
 Improve this answer
 Follow
 edited Apr 28, 2016 at 13:12
 Jazi
 6,554 13 13 gold badges 58 58 silver badges 91 91 bronze badges
 answered Apr 17, 2015 at 15:41
 Raja Raja
 685 7 7 silver badges 7 7 bronze badges
 1
 * 6
 This remove the folder from the local as well! Use git rm --cached -r <foldername>
 – Axis
 Jun 8, 2019 at 5:12
 Add a comment |
 24

 More generally, git help will help with at least simple questions like this:

 zhasper@berens:/media/Kindle/documents$ git help
 usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]
 
 The most commonly used git commands are:
 add Add file contents to the index
 :
 rm Remove files from the working tree and from the index
 
 Share
 Improve this answer
 Follow
 answered Jan 12, 2010 at 8:21
 James Polley James Polley
 7,789 2 2 gold badges 28 28 silver badges 32 32 bronze badges
 3
 * 6
 For more complex things, I find git help confusing, but it's good for the simple things :)
 – James Polley
 Jan 12, 2010 at 10:43
 * 11
 It's not actually that great for git noobs because "index" is not a concept git noobs are familiar with. I speak from personal experience of being a git noob :) Also, I feel it's much less confusing to say that rm stages a file for deletion rather than removes from the index (though it's still meaningless for noobs).
 – Roman Starkov
 Apr 4, 2013 at 11:51
 * Agreeing with romkyns: The description "Remove files from the working tree and from the index" doesn't sound to me like it's a way to remove the file from the repo. Maybe if I understood git better it would. Instead it sounds like you're just unstaging files (and optionally removing them from the working tree -- would that necessarily affect the repo?)
 – LarsH
 Jul 5, 2016 at 21:33
 Add a comment |
 16

 If you want to delete the file from the repo, but leave it in the the file system (will be untracked):

 bykov@gitserver:~/temp> git rm --cached file1.txt
 bykov@gitserver:~/temp> git commit -m "remove file1.txt from the repo"
 

 If you want to delete the file from the repo and from the file system then there are two options:

 1. If the file has no changes staged in the index:

 bykov@gitserver:~/temp> git rm file1.txt
 bykov@gitserver:~/temp> git commit -m "remove file1.txt"
 

 2. If the file has changes staged in the index:

 bykov@gitserver:~/temp> git rm -f file1.txt
 bykov@gitserver:~/temp> git commit -m "remove file1.txt"
 
 Share
 Improve this answer
 Follow
 edited Mar 20, 2015 at 12:31
 answered Mar 20, 2015 at 12:13
 user1134181 user1134181
 Add a comment |
 14

 git rm will only remove the file on this branch from now on, but it remains in history and git will remember it.

 The right way to do it is with git filter-branch, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.

 But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.

 If you want to completely obliterate it in one step, I recommend you to use git forget-blob

 https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/

 It is easy, just do git forget-blob file1.txt.

 This will remove every reference, do git filter-branch, and finally run the git garbage collector git gc to completely get rid of this file in your repo.

 Share
 Improve this answer
 Follow
 answered Jan 23, 2017 at 12:56
 nachoparker nachoparker
 1,632 18 18 silver badges 14 14 bronze badges
 1
 * In 2021, I tried this and it didn't seem to work. I could still see the file history. Instead I ended up renaming the file with git mv , cleared all contents from the file committed it.
 – Harsha N Hegde
 Sep 21, 2021 at 10:56
 Add a comment |
 11

 Note: if you want to delete file only from git use below:

 git rm --cached file1.txt
 

 If you want to delete also from hard disk:

 git rm file1.txt
 

 If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:

 git rm -r foldername
 

 If you want to remove a folder inside another folder

 git rm -r parentFolder/childFolder
 

 Then, you can commit and push as usual. However, if you want to recover deleted folder, you can follow this: recover deleted files from git is possible.

 From doc:

 git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​
 

 OPTIONS

 <file>…​

 Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want Git to expand file glob characters, you
 

 may need to shell-escape them. A leading directory name (e.g. dir to remove dir/file1 and dir/file2) can be given to remove all files in the directory, and recursively all sub-directories, but this requires the -r option to be explicitly given. -f --force

 Override the up-to-date check.
 

 -n --dry-run

 Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.
 

 -r

 Allow recursive removal when a leading directory name is given.
 

 --

 This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for
 

 command-line options). --cached

 Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
 

 --ignore-unmatch

 Exit with a zero status even if no files matched.
 

 -q --quiet

 git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.
 

 Read more on official doc.

 Share
 Improve this answer
 Follow
 edited Oct 7, 2018 at 3:02
 answered Oct 7, 2018 at 2:57
 Blasanka Blasanka
 19.9k 11 11 gold badges 97 97 silver badges 103 103 bronze badges
 Add a comment |
 11

 The answer by Greg Hewgill, that was edited by Johannchopin helped me, as I did not care about removing the file from the history completely. In my case, it was a directory, so the only change I did was using:

 git rm -r --cached myDirectoryName
 

 instead of "git rm --cached file1.txt" ..followed by:

 git commit -m "deleted myDirectoryName from git"
 git push origin branch_name
 

 Thanks Greg Hewgill and Johannchopin!

 Share
 Improve this answer
 Follow
 answered Oct 3, 2020 at 9:14
 wat er wat er
 191 1 1 silver badge 5 5 bronze badges
 Add a comment |
 10

 Another way if you want to delete the file from your local folder using rm command and then push the changes to the remote server.

 rm file1.txt
 
 git commit -a -m "Deleting files"
 
 git push origin master
 
 Share
 Improve this answer
 Follow
 answered Oct 6, 2015 at 3:07
 Suhaib Suhaib
 2,031 3 3 gold badges 24 24 silver badges 35 35 bronze badges
 Add a comment |
 10

 According to the documentation.

 git rm --cached file1.txt

 When it comes to sensitive data—better not say that you removed the file but rather just include it in the last known commit:

 0. Amend last commit

 git commit --amend -CHEAD
 

 If you want to delete the file from all git history, according to the documentation you should do the following:

 1. Remove it from your local history

 git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \ --prune-empty --tag-name-filter cat -- --all
 # Replace PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename
 
 1. Don't forget to include this file in .gitignore (If it's a file you never want to share (such as passwords...):
 echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
 git add .gitignore
 git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
 

 3. If you need to remove from the remote

 git push origin --force --all
 

 4. If you also need to remove it from tag releases:

 git push origin --force --tags
 
 Share
 Improve this answer
 Follow
 edited Jul 1, 2020 at 10:10
 answered May 16, 2020 at 21:08
 Tomas Baran Tomas Baran
 1,703 18 18 silver badges 33 33 bronze badges
 Add a comment |
 7

 In my case I tried to remove file on github after few commits but save on computer

 git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
 git push --force -u origin master
 

 and later this file was ignored

 Share
 Improve this answer
 Follow
 answered Aug 23, 2016 at 12:03
 OlegV OlegV
 71 1 1 silver badge 1 1 bronze badge
 Add a comment |
 7

 To delete a specific file

 git rm filename

 To clean all the untracked files from a directory recursively in single shot

 git clean -fdx

 Share
 Improve this answer
 Follow
 answered Aug 23, 2016 at 13:56
 Sitesh Sitesh
 1,716 1 1 gold badge 17 17 silver badges 24 24 bronze badges
 3
 * git clean -fdx <-- Beware as it will remove everything that is not tracked.
 – Philip
 Mar 22, 2022 at 12:58
 * @Philip By To clean all the untracked files from a directory recursively I meant exactly same.
 – Sitesh
 Mar 23, 2022 at 16:13
 * Can be clarified in a better way imho.
 – Philip
 Mar 25, 2022 at 12:28
 Add a comment |
 5

 If you have the GitHub for Windows application, you can delete a file in 5 easy steps:

 * Click Sync.
 * Click on the directory where the file is located and select your latest version of the file.
 * Click on tools and select "Open a shell here."
 * In the shell, type: "rm {filename}" and hit enter.
 * Commit the change and resync.
 Share
 Improve this answer
 Follow
 edited Mar 15, 2013 at 18:52
 answered Mar 14, 2013 at 15:15
 jushusted jushusted
 67 1 1 silver badge 4 4 bronze badges
 Add a comment |
 3

 1. First,Remove files from local repository.

 git rm -r File-Name

 or, remove files only from local repository but from filesystem

 git rm --cached File-Name

 2. Secondly, Commit changes into local repository.

 git commit -m "unwanted files or some inline comments" 
 

 3. Finally, update/push local changes into remote repository.

 git push 
 
 Share
 Improve this answer
 Follow
 answered Oct 18, 2016 at 4:00
 Ajay Kumar Ajay Kumar
 4,542 38 38 silver badges 42 42 bronze badges
 2
 * would this remove everything related with the accidentally added and pushed file?
 – bapors
 Feb 14, 2018 at 22:56
 * @bapors no it will remain in git history
 – Alaa Eddine Cherbib
 Apr 19, 2021 at 9:01
 Add a comment |
 3

 go to your project dir and type:

 git filter-branch --tree-filter 'rm -f <deleted-file>' HEAD
 

 after that push --force for delete file from all commits.

 git push origin --force --all
 
 Share
 Improve this answer
 Follow
 answered Sep 30, 2020 at 0:52
 rojen rojen
 349 3 3 silver badges 4 4 bronze badges
 Add a comment |
 3

 https://stackoverflow.com/a/2047477/14508423 https://stackoverflow.com/a/19666677/14508423 Quite similar, tried both. Nothing. Git keeps tracking the file. (I use vscode so the file got instantly marked as U(untracked) or A(added))

 Doing this https://stackoverflow.com/a/53431148/14508423 to whole category solves the problem.

 Do this to delete a file from git and make git forget about it:

 **manually add the file name to .gitignore**
 
 git rm --cached settings.py 
 git commit -m "remove settings.py"
 git push origin master
 git rm -r --cached .
 git add .
 git commit -m "Untrack files in .gitignore"
 

 ps: every time it failed I tried to delete file with sensitive info like tokens (.env and settings.py files). But it works fine with random files oO

 Share
 Improve this answer
 Follow
 edited Aug 5, 2022 at 15:42
 answered Aug 5, 2022 at 13:46
 void_eater void_eater
 83 1 1 silver badge 8 8 bronze badges
 1
 * 1
 If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
 – OznOg
 Aug 12, 2022 at 9:47
 Add a comment |
 2

 For the case where git rm doesn't suffice and the file needs to be removed from history: As the git filter-branch manual page now itself suggests using git-filter-repo, and I had to do this today, here's an example using that tool. It uses the example repo https://example/eguser/eg.git

 1. Clone the repository into a new directory git clone https://example/eguser/eg.git

 2. Keep everything except the unwanted file. git-filter-repo --path file1.txt --invert-paths

 3. Add the remote repository origin back : git remote add origin https://example/eguser/eg.git. The git-filter-repo tool removes remote remote info by design and suggests a new remote repo (see point 4). This makes sense for big shared repos but might be overkill for getting rid a single newly added file as in this example.

 4. When happy with the contents of local, replace remote with it.

 git push --force -u origin master. Forcing is required due to the changed history.

 Also note the useful --dry-run option and a good discussion in the linked manual on team and project dynamics before charging in and changing repository history.

 Share
 Improve this answer
 Follow
 edited Sep 7, 2020 at 4:34
 answered Sep 7, 2020 at 1:14
 Adam Burke Adam Burke
 602 1 1 gold badge 6 6 silver badges 20 20 bronze badges
 Add a comment |
 1

 I tried a lot of the suggested options and none appeared to work (I won't list the various problems). What I ended up doing, which worked, was simple and intuitive (to me) was:

 1. move the whole local repo elsewhere
 2. clone the repo again from master to your local drive
 3. copy back the files/folder from your original copy in #1 back into the new clone from #2
 4. make sure that the problem large file is either not there or excluded in the .gitignore file
 5. do the usual git add/git commit/git push
 Share
 Improve this answer
 Follow
 answered Mar 21, 2018 at 18:22
 jacanterbury jacanterbury
 1,365 1 1 gold badge 24 24 silver badges 34 34 bronze badges
 0
 Add a comment |
 1

 After you have removed the file from the repo with git rm you can use BFG Repo-Cleaner to completely and easily obliterate the file from the repo history.

 Share
 Improve this answer
 Follow
 answered Sep 30, 2018 at 17:26
 glisu glisu
 1,006 9 9 silver badges 20 20 bronze badges
 Add a comment |
 1

 Just by going on the file in your github repository you can see the delete icon beside Raw|Blame and don't forget to click on commit changes button. And you can see that your file has been deleted.

 Share
 Improve this answer
 Follow
 answered Sep 3, 2020 at 18:45
 nitesh mishra nitesh mishra
 39 2 2 bronze badges
 Add a comment |
 1

 New answer that works in 2022.

 Do not use:

 git filter-branch
 

 this command might not change the remote repo after pushing. If you clone after using it, you will see that nothing has changed and the repo still has a large size. This command is old now. For example, if you use the steps in https://github.com/18F/C2/issues/439, this won't work.

 You need to use

 git filter-repo
 

 Steps:

 (1) Find the largest files in .git:

 git rev-list --objects --all | grep -f <(git verify-pack -v .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)
 

 (2) Strat filtering these large files:

 git filter-repo --path-glob '../../src/../..' --invert-paths --force
 

 or

 git filter-repo --path-glob '*.zip' --invert-paths --force
 

 or

 git filter-repo --path-glob '*.a' --invert-paths --force
 

 or whatever you find in step 1.

 (3)

 git remote add origin git@github.com:.../...git
 

 (4)

 git push --all --force
 
 git push --tags --force
 

 DONE!

 Share
 Improve this answer
 Follow
 edited Nov 27, 2022 at 17:49
 answered Nov 27, 2022 at 17:09
 Joe Joe
 189 2 2 silver badges 9 9 bronze badges
 Add a comment |
 0

 I have obj and bin files that accidentally made it into the repo that I don't want polluting my 'changed files' list

 After I noticed they went to the remote, I ignored them by adding this to .gitignore

 /*/obj
 /*/bin
 

 Problem is they are already in the remote, and when they get changed, they pop up as changed and pollute the changed file list.

 To stop seeing them, you need to delete the whole folder from the remote repository.

 In a command prompt:

 1. CD to the repo folder (i.e. C:\repos\MyRepo)
 2. I want to delete SSIS\obj. It seems you can only delete at the top level, so you now need to CD into SSIS: (i.e. C:\repos\MyRepo\SSIS)
 3. Now type the magic incantation git rm -r -f obj
 + rm=remove
 + -r = recursively remove
 + -f = means force, cause you really mean it
 + obj is the folder
 4. Now run git commit -m "remove obj folder"

 I got an alarming message saying 13 files changed 315222 deletions

 Then because I didn't want to have to look up the CMD line, I went into Visual Sstudio and did a Sync to apply it to the remote

 Share
 Improve this answer
 Follow
 edited Jul 28, 2018 at 10:07
 answered Jul 27, 2018 at 1:59
 Nick.McDermaid Nick.McDermaid
 17.6k 6 6 gold badges 55 55 silver badges 89 89 bronze badges
 Add a comment |
 0

 if your file is sensitive (for example, settings or keys you accidently added and committed) then you can remove it from all versions.

 To remove from all versions use the command below (warning: careful because you won't be able to restore the removed file in the repo if do not have a copy):

 1. Using Git
 git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch file.ext' HEAD
 
 1. Using git-filter-repo (get it here, new recommended way by Git)
 git filter-repo --path file.ext
 
 Share
 Improve this answer
 Follow
 edited Jul 9, 2022 at 17:31
 answered Jul 9, 2022 at 16:00
 Eugene Eugene
 2,920 18 18 silver badges 22 22 bronze badges
 Add a comment |
 -1

 If you need to remove files from a determined extension (for example, compiled files) you could do the following to remove them all at once:

 git remove -f *.pyc
 
 Share
 Improve this answer
 Follow
 answered Oct 1, 2019 at 5:21
 Sergi Ramón Sergi Ramón
 175 1 1 silver badge 4 4 bronze badges
 Add a comment |
 -8

 Incase if you don't file in your local repo but in git repo, then simply open file in git repo through web interface and find Delete button at right corner in interface. Click Here, To view interface Delete Option

 Share
 Improve this answer
 Follow
 answered Nov 30, 2016 at 15:47
 Poseidon Poseidon
 1
 Add a comment |
 Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

 Not the answer you're looking for? Browse other questions tagged
 * git
 * git-rm
 or ask your own question.

 * The Overflow Blog
 * Announcing more ways to learn and grow your skills
 * Why developer experience is the key to better software, straight from the...
 * Featured on Meta
 * Accessibility Update: Colors
 * 2022: a year in moderation
 * Collectives: The next iteration
 * We’re bringing advertisements for technology courses to Stack Overflow
 * Temporary policy: ChatGPT is banned

 Linked

 2
 I still get unwanted files uploaded on Github after I added them in .gitignore
 2
 Is there a preferred method of deleting a file with git?
 0
 How does one .gitignore pickle files in python?
 0
 How to delete a file from github repository after deleting the same file on local machine?
 3133
 How do I find and restore a deleted file in a Git repository?
 1278
 What effect does the `--no-ff` flag have for `git merge`?
 401
 Why is .gitignore not ignoring my files?
 488
 Remove sensitive files and their commits from Git history
 212
 Git: Remove committed file after push
 104
 How can I remove a file from Git within IntelliJ VCS?
 See more linked questions

 Related

 7884
 How do I remove local (untracked) files from the current Git working tree?
 13516
 What is the difference between 'git pull' and 'git fetch'?
 25326
 How do I undo the most recent local commits in Git?
 9162
 How do I force "git pull" to overwrite local files?
 3667
 Remove a file from a Git repository without deleting it from the local filesystem
 7654
 How do I make Git forget about a file that was tracked, but is now in .gitignore?
 8258
 How do I check out a remote Git branch?
 19889
 How do I delete a Git branch locally and remotely?
 7613
 How do I revert a Git repository to a previous commit?
 11078
 How do I rename a local Git branch?

 Hot Network Questions

 * Can "né" or "née" be used to refer to a religious's secular name?
 * Ada and its candidate languages
 * A standard way of marking alternate tunings for classical guitar?
 * Are there any Oxford Colleges which will accommodate a family including children
 * Should I meet my boss when I travel to his/her country?
 * When did Kievan Rus' rulers become culturally Slavic?
 * Why are none of these chips identifiable?
 * Can a helicopter's main rotor mince a human?
 * Addition and multiplication are commutative but exponentiation and tetration are not. Do we know why?
 * What did Paul McCartney mean by "Oh, yesterday came suddenly"?
 * How to clean bath spa nozzles?
 * how to modify a wooden fence to keep nubian goats from extending their heads over and eating dwarf tree on other side
 * EAS Build iOS Failure: Authentication with Apple Developer Portal failed
 * Will a spark gap transmit a signal an FM receiver can "hear?"
 * Is there a cutoff for Newtonian gravity?
 * Meaning of "nude" in Saxo Grammaticus
 * 2 tabulars with supposed same size aren't actually the same size
 * Computers lose power, lights dim. I suspect the mains power supply; is there anything I can do?
 * Two questions about the 4th verse of "The Star-Spangled Banner"
 * Which Deep Learning Method to use for the classification of precious stones (diamonds, sapphire, ruby etc) based on digital photo images and data set?
 * Smallest Bit Rotation
 * What is the use of alumium profile with LED strip lighting?
 * A very difficult functional equation
 * Can a vampire rest in a coffin in a bag of holding?
 more hot questions
 Question feed

 Subscribe to RSS

 Question feed

 To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

 Stack Overflow

 * Questions
 * Help

 Products

 * Teams
 * Advertising
 * Collectives
 * Talent

 Company

 * About
 * Press
 * Work Here
 * Legal
 * Privacy Policy
 * Terms of Service
 * Contact Us
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.2.3.43216

 Your privacy

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

 Accept all cookies Customize settings
Use  git rm .
If you want to remove the file from the Git repository and the filesystem, use:
 git rm file1.txt
git commit -m "remove file1.txt"
 
But if you want to remove the file only from the Git repository and not remove it from the filesystem, use: 
 git rm --cached file1.txt
git commit -m "remove file1.txt"
 
And to push changes to remote repo
 git push origin branch_name
 
 
 
 
 
 
 
    
 
 
  johannchopin  13.1k9 gold badges51 silver badges99 bronze badges
 
 
 
 
 answered Jan 12, 2010 at 7:52
 
 
    
 
 
  Greg HewgillGreg Hewgill 927k180 gold badges1136 silver badges1275 bronze badges
 
 
 
 
 20
 
 
 
 
 
 
 git rm file.txt removes the file from the repo but also deletes it from the local file system.
To remove the file from the repo and not delete it from the local file system use:
git rm --cached file.txt 
The below exact situation is where I use git to maintain version control for my business's website, but the "mickey" directory was a tmp folder to share private content with a CAD developer. When he needed HUGE files, I made a private, unlinked directory and ftpd the files there for him to fetch via browser. Forgetting I did this, I later performed a git add -A from the website's base directory. Subsequently, git status showed the new files needing committing. Now I needed to delete them from git's tracking and version control...
Sample output below is from what just happened to me, where I unintentionally deleted the .003 file. Thankfully, I don't care what happened to the local copy to .003, but some of the other currently changed files were updates I just made to the website and would be epic to have been deleted on the local file system! "Local file system" = the live website (not a great practice, but is reality).
 [~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003
error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications
(use --cached to keep the file, or -f to force removal)
[~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003
rm 'shop/mickey/mtt_flange_SCN.7z.003'
[~/www]$ 
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD &lt;file&gt;..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# (use "git add &lt;file&gt;..." to update what will be committed)
# (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)
#
# modified: shop/mickey/mtt_flange_SCN.7z.001
# modified: shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ 
[~/www]$ 
[~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002
rm 'shop/mickey/mtt_flange_SCN.7z.002'
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ 
[~/www]$ 
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD &lt;file&gt;..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.002
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# modified: shop/mickey/mtt_flange_SCN.7z.001
[~/www]$
 
 Update: This answer is getting some traffic, so I thought I'd mention my other Git answer shares a couple of great resources: This page has a graphic that help demystify Git for me. The "Pro Git" book is online and helps me a lot.
 
 
 
 
    
 
 
 
 answered May 25, 2013 at 20:26
 
 
    
 
 
 
 
 1
 
 
 
 
 
 
First, if you are using  git rm , especially for multiple files, consider any wildcard will be resolved by the shell, not by the git command.
 git rm -- *.anExtension
git commit -m "remove multiple files"
 
But, if your file is already on GitHub, you can (since July 2013) directly delete it from the web GUI!
Simply view any file in your repository, click the trash can icon at the top, and commit the removal just like any other web-based edit.
Then "git pull" on your local repo, and that will delete the file locally too.
Which makes this answer a (roundabout) way to delete a file from git repo?
(Not to mention that a file on GitHub is in a "git repo")
   
(the commit will reflect the deletion of that file):
 
And just like that, it’s gone.
For help with these features, be sure to read our help articles on creating, moving, renaming, and deleting files.
Note: Since it’s a version control system, Git always has your back if you need to recover the file later.
The last sentence means that the deleted file is still part of the history, and you can restore it easily enough (but not yet through the GitHub web interface):
See "Restore a deleted file in a Git repo".
 
 
 
 
    
 
 
 
 answered Jul 4, 2013 at 20:53
 
 
    
 
 
  VonCVonC 1.2m508 gold badges4244 silver badges5066 bronze badges
 
 
 
 
 7
 
 
 
 
 
 
This is the only option that worked for me.
 git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'
 
Note: Replace *.sql with your file name or file type. Be very careful because this will go through every commit and rip this file type out. 
EDIT:
pay attention - after this command you will not be able to push or pull - you will see the reject of 'unrelated history' you can use 'git push --force -u origin master' to push or pull
 
 
 
 
    
 
 
 
 answered Feb 27, 2015 at 19:29
 
 
    
 
 
 
 
 9
 
 
 
 
 
 
Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:
 git rm -r foldername
 
 
 
 
 
 
 
    
 
 
  Jazi  6,55413 gold badges58 silver badges91 bronze badges
 
 
 
 
 answered Apr 17, 2015 at 15:41
 
 
    
 
 
  RajaRaja 6857 silver badges7 bronze badges
 
 
 
 
 1
 
 
 
 
 
 
More generally, git help will help with at least simple questions like this:
 zhasper@berens:/media/Kindle/documents$ git help
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]
The most commonly used git commands are:
 add Add file contents to the index
 :
 rm Remove files from the working tree and from the index
 
 
 
 
 answered Jan 12, 2010 at 8:21
 
 
    
 
 
  James PolleyJames Polley 7,7892 gold badges28 silver badges32 bronze badges
 
 
 
 3
 
 
 
 
If you want to delete the file from the repo, but leave it in the the file system (will be untracked):
 bykov@gitserver:~/temp&gt; git rm --cached file1.txt
bykov@gitserver:~/temp&gt; git commit -m "remove file1.txt from the repo"
 
If you want to delete the file from the repo and from the file system then there are two options:
 If the file has no changes staged in the index: 
 bykov@gitserver:~/temp&gt; git rm file1.txt
bykov@gitserver:~/temp&gt; git commit -m "remove file1.txt"
  
 If the file has changes staged in the index: 
 bykov@gitserver:~/temp&gt; git rm -f file1.txt
bykov@gitserver:~/temp&gt; git commit -m "remove file1.txt"
  
 
 
 
 answered Mar 20, 2015 at 12:13
 
 
 
 
 
 
 
 
 
 git rm will only remove the file on this branch from now on, but it remains in history and git will remember it.
The right way to do it is with git filter-branch, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.
But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.
If you want to completely obliterate it in one step, I recommend you to use git forget-blob 
 https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/ 
It is easy, just do git forget-blob file1.txt.
This will remove every reference, do git filter-branch, and finally run the git garbage collector git gc to completely get rid of this file in your repo.
 
 
 
 answered Jan 23, 2017 at 12:56
 
 
    
 
 
  nachoparkernachoparker 1,63218 silver badges14 bronze badges
 
 
 
 1
 
 
 
 
Note: if you want to delete file only from git use below:
 git rm --cached file1.txt
 
If you want to delete also from hard disk:
 git rm file1.txt
 
If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:
 git rm -r foldername
 
If you want to remove a folder inside another folder
 git rm -r parentFolder/childFolder
 
Then, you can commit and push as usual. However, if you want to recover deleted folder, you can follow this: recover deleted files from git is possible. 
  From doc: 
 git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] &lt;file&gt;…​
 
 
  OPTIONS 
 
   &lt;file&gt;…​  
 Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want Git to expand file glob characters, you
 
 
 may need to shell-escape them. A leading directory name (e.g. dir to
 remove dir/file1 and dir/file2) can be given to remove all files in
 the directory, and recursively all sub-directories, but this requires
 the -r option to be explicitly given.
  -f 
  --force  
 Override the up-to-date check.
 
 
   -n 
  --dry-run  
 Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.
 
 
   -r  
 Allow recursive removal when a leading directory name is given.
 
 
   --  
 This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for
 
 
 command-line options).
  --cached  
 Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
 
 
 --ignore-unmatch
 Exit with a zero status even if no files matched.
 
 
   -q 
  --quiet  
 git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.
 
 Read more on official doc. 
 
 
 
 answered Oct 7, 2018 at 2:57
 
 
    
 
 
  BlasankaBlasanka 19.9k11 gold badges97 silver badges103 bronze badges
 
 
 
 
 
 
The answer by Greg Hewgill, that was edited by Johannchopin helped me, as I did not care about removing the file from the history completely.
In my case, it was a directory, so the only change I did was using:
 git rm -r --cached myDirectoryName
 
instead of "git rm --cached file1.txt"
..followed by:
 git commit -m "deleted myDirectoryName from git"
git push origin branch_name
 
Thanks Greg Hewgill and Johannchopin!
 
 
 
 answered Oct 3, 2020 at 9:14
 
 
    
 
 
  wat erwat er 1911 silver badge5 bronze badges
 
 
 
 
 
 
Another way if you want to delete the file from your local folder using rm command and then push the changes to the remote server.
 rm file1.txt
git commit -a -m "Deleting files"
git push origin master
 
 
 
 
 answered Oct 6, 2015 at 3:07
 
 
    
 
 
  SuhaibSuhaib 2,0313 gold badges24 silver badges35 bronze badges
 
 
 
 
 
 
According to the documentation.
  git rm --cached file1.txt 
When it comes to sensitive data—better not say that you removed the file but rather just include it in the last known commit:
 0. Amend last commit 
 git commit --amend -CHEAD
 
If you want to delete the file from all git history, according to the documentation you should do the following:
 1. Remove it from your local history 
 git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \ --prune-empty --tag-name-filter cat -- --all
# Replace PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename
 
 Don't forget to include this file in .gitignore (If it's a file you never want to share (such as passwords...):
 echo "YOUR-FILE-WITH-SENSITIVE-DATA" &gt;&gt; .gitignore
git add .gitignore
git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
 
 3. If you need to remove from the remote 
 git push origin --force --all
 
 4. If you also need to remove it from tag releases: 
 git push origin --force --tags
 
 
 
 
 answered May 16, 2020 at 21:08
 
 
    
 
 
  Tomas BaranTomas Baran 1,70318 silver badges33 bronze badges
 
 
 
 
 
 
In my case I tried to remove file on github after few commits but save on computer
 git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master
 
and later this file was ignored
 
 
 
 answered Aug 23, 2016 at 12:03
 
 
    
 
 
  OlegVOlegV 711 silver badge1 bronze badge
 
 
 
 
 
 
 
 
To delete a specific file
 git rm filename
To clean all the untracked files from a directory recursively in single shot
 git clean -fdx
 
 
 
 answered Aug 23, 2016 at 13:56
 
 
    
 
 
  SiteshSitesh 1,7161 gold badge17 silver badges24 bronze badges
 
 
 
 3
 
 
 
 
If you have the GitHub for Windows application, you can delete a file in 5 easy steps:
Click Sync.
Click on the directory where the file is located and select your latest version of the file.
Click on tools and select "Open a shell here."
In the shell, type: "rm {filename}" and hit enter.
Commit the change and resync.
 
 
 
 answered Mar 14, 2013 at 15:15
 
 
    
 
 
  jushustedjushusted 671 silver badge4 bronze badges
 
 
 
 
 
 
 
 
 First,Remove files from local repository.
 git rm -r File-Name
or, remove files only from local repository but from filesystem
 git rm --cached File-Name
 
 Secondly, Commit changes into local repository.
 git commit -m "unwanted files or some inline comments" 
  
 Finally, update/push local changes into remote repository.
 git push 
  
 
 
 
 answered Oct 18, 2016 at 4:00
 
 
    
 
 
  Ajay KumarAjay Kumar 4,54238 silver badges42 bronze badges
 
 
 
 2
 
 
 
 
go to your project dir and type:
 git filter-branch --tree-filter 'rm -f &lt;deleted-file&gt;' HEAD
 
after that push --force for delete file from all commits.
 git push origin --force --all
 
 
 
 
 answered Sep 30, 2020 at 0:52
 
 
    
 
 
  rojenrojen 3493 silver badges4 bronze badges
 
 
 
 
 
 
 
 
 https://stackoverflow.com/a/2047477/14508423
https://stackoverflow.com/a/19666677/14508423
Quite similar, tried both. Nothing. Git keeps tracking the file. (I use vscode so the file got instantly marked as U(untracked) or A(added))
Doing this https://stackoverflow.com/a/53431148/14508423 to whole category solves the problem.
Do this to delete a file from git and make git forget about it:
 **manually add the file name to .gitignore**
git rm --cached settings.py 
git commit -m "remove settings.py"
git push origin master
git rm -r --cached .
git add .
git commit -m "Untrack files in .gitignore"
 
ps: every time it failed I tried to delete file with sensitive info like tokens (.env and settings.py files). But it works fine with random files oO
 
 
 
 answered Aug 5, 2022 at 13:46
 
 
    
 
 
  void_eatervoid_eater 831 silver badge8 bronze badges
 
 
 
 1
 
 
 
 
For the case where git rm doesn't suffice and the file needs to be removed from history: As the git filter-branch manual page now itself suggests using git-filter-repo, and I had to do this today, here's an example using that tool. It uses the example repo https://example/eguser/eg.git 
 Clone the repository into a new directory git clone https://example/eguser/eg.git 
 Keep everything except the unwanted file. git-filter-repo --path file1.txt --invert-paths 
 Add the remote repository origin back :
git remote add origin https://example/eguser/eg.git.
The git-filter-repo tool removes remote remote info by design and suggests a new remote repo (see point 4). This makes sense for big shared repos but might be overkill for getting rid a single newly added file as in this example.
 When happy with the contents of local, replace remote with it.
 git push --force -u origin master. Forcing is required due to the changed history.
Also note the useful --dry-run option and a good discussion in the linked manual on team and project dynamics before charging in and changing repository history.
 
 
 
 answered Sep 7, 2020 at 1:14
 
 
    
 
 
  Adam BurkeAdam Burke 6021 gold badge6 silver badges20 bronze badges
 
 
 
 
 
 
 
 
I tried a lot of the suggested options and none appeared to work (I won't list the various problems). 
What I ended up doing, which worked, was simple and intuitive (to me) was:
move the whole local repo elsewhere
clone the repo again from master to your local drive
copy back the files/folder from your original copy in #1 back into the new clone from #2
make sure that the problem large file is either not there or excluded in the .gitignore file
do the usual git add/git commit/git push
 
 
 
 answered Mar 21, 2018 at 18:22
 
 
    
 
 
  jacanterburyjacanterbury 1,3651 gold badge24 silver badges34 bronze badges
 
 
 
 0
 
 
 
 After you have removed the file from the repo with git rm you can use BFG Repo-Cleaner to completely and easily obliterate the file from the repo history.
 
 
 answered Sep 30, 2018 at 17:26
 
 
    
 
 
  glisuglisu 1,0069 silver badges20 bronze badges
 
 
 
 
 
 Just by going on the file in your github repository you can see the delete icon beside Raw|Blame and don't forget to click on commit changes button. And you can see that your file has been deleted.
 
 
 answered Sep 3, 2020 at 18:45
 
 
    
 
 
 
 
 
 
New answer that works in 2022.
Do not use:
 git filter-branch
 
this command might not change the remote repo after pushing. If you clone after using it, you will see that nothing has changed and the repo still has a large size. This command is old now. For example, if you use the steps in https://github.com/18F/C2/issues/439, this won't work.
You need to use
 git filter-repo
 
Steps:
(1) Find the largest files in .git:
 git rev-list --objects --all | grep -f &lt;(git verify-pack -v .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)
 
(2) Strat filtering these large files:
  git filter-repo --path-glob '../../src/../..' --invert-paths --force
 
or
  git filter-repo --path-glob '*.zip' --invert-paths --force
 
or
  git filter-repo --path-glob '*.a' --invert-paths --force
 
or
whatever you find in step 1.
(3)
  git remote add origin git@github.com:.../...git
 
(4)
 git push --all --force
git push --tags --force
 
DONE!
 
 
 
 answered Nov 27, 2022 at 17:09
 
 
    
 
 
  JoeJoe 1892 silver badges9 bronze badges
 
 
 
 
 
 
I have obj and bin files that accidentally made it into the repo that I don't want polluting my 'changed files' list
 After I noticed they went to the remote, I ignored them by adding this to .gitignore 
 /*/obj
/*/bin
 
Problem is they are already in the remote, and when they get changed, they pop up as changed and pollute the changed file list. 
To stop seeing them, you need to delete the whole folder from the remote repository.
In a command prompt:
CD to the repo folder (i.e. C:\repos\MyRepo)
I want to delete SSIS\obj. It seems you can only delete at the top level, so you now need to CD into SSIS: (i.e. C:\repos\MyRepo\SSIS) 
Now type the magic incantation git rm -r -f obj
rm=remove
-r = recursively remove
-f = means force, cause you really mean it
obj is the folder
 
Now run git commit -m "remove obj folder" 
I got an alarming message saying 13 files changed 315222 deletions 
Then because I didn't want to have to look up the CMD line, I went into Visual Sstudio and did a Sync to apply it to the remote
 
 
 
 answered Jul 27, 2018 at 1:59
 
 
    
 
 
  Nick.McDermaidNick.McDermaid 17.6k6 gold badges55 silver badges89 bronze badges
 
 
 
 
 
 
if your file is sensitive (for example, settings or keys you accidently added and committed) then you can remove it from all versions.
To remove from all versions use the command below (warning: careful because you won't be able to restore the removed file in the repo if do not have a copy):
Using Git 
 git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch file.ext' HEAD
 
Using git-filter-repo (get it here, new recommended way by Git)
 git filter-repo --path file.ext
 
 
 
 
 answered Jul 9, 2022 at 16:00
 
 
    
 
 
  EugeneEugene 2,92018 silver badges22 bronze badges
 
 
 
 
 
 
If you need to remove files from a determined extension (for example, compiled files) you could do the following to remove them all at once:
 git remove -f *.pyc
 
 
 
 
 answered Oct 1, 2019 at 5:21
 
 
    
 
 
 
 
 
 Incase if you don't file in your local repo but in git repo, then simply open file in git repo through web interface and find Delete button at right corner in interface. 
Click Here, To view interface Delete Option 
 
 
 answered Nov 30, 2016 at 15:47
How do I delete a file from a Git repository?
https://stackoverflow.com/questions/2047465/how-do-i-delete-a-file-from-a-git-repository
https://stackoverflow.com/questions/1143796/remove-a-file-from-a-git-repository-without-deleting-it-from-the-local-filesyste
Remove a file from a Git repository without deleting it from the local filesystem
2022-07-25
Mveerman Mveerman
<div><div>
<p>I would like to add to the accepted answer of <a href="https://stackoverflow.com/a/1143800/6073148">@bdonlan</a>.</p>
<hr />
<h4> DON'T USE THIS ANSWER TO REMOVE FILE(S) THAT EXISTS ON REMOTE.</h4>
<pre><code>git rm --cached filename
</code></pre>
<hr />
<h3>What the answer is supposed to do?</h3>
<p>It is supposed to remove some files from the local staged area that you have mistakenly committed in some previous commit(s). 
</p><ol>
<li>And have not pushed to the remote.</li>
<li>And if pushed on remote, others don't care about those changes.</li>
</ol>
<p>It moves files from <strong>Tracked</strong> 𝐭𝐨 <strong>Untracked</strong> state by that what I mean is, it <em>deletes</em> the files and <em>adds</em> them again.</p>
<p>So, git doesn't know about them anymore.</p>
<hr />
<h2> What could go wrong? </h2><p>
On remote, there is 𝐧𝐨 such thing as an 𝐮𝐧𝐭𝐫𝐚𝐜𝐤𝐞𝐝 𝐬𝐭𝐚𝐭𝐞, there is 𝐨𝐧𝐥𝐲 𝐭𝐫𝐚𝐜𝐤𝐞𝐝 𝐬𝐭𝐚𝐭𝐞 and that results in havoc.
</p><hr />
<h2> Why? </h2><p>
When collaborating with the team, if you 𝐩𝐮𝐬𝐡 such changes up to remote it will delete those changes on remote and all the team who takes a 𝐩𝐮𝐥𝐥 from 𝐫𝐞𝐦𝐨𝐭𝐞 𝐰𝐢𝐥𝐥 𝐜𝐚𝐮𝐬𝐞 𝐝𝐞𝐥𝐞𝐭𝐢𝐨𝐧 𝐨𝐟 𝐭𝐡𝐨𝐬𝐞 𝐟𝐢𝐥𝐞𝐬 𝐰𝐡𝐢𝐜𝐡 𝐲𝐨𝐮 𝐡𝐚𝐯𝐞 𝐣𝐮𝐬𝐭 𝐫𝐞𝐦𝐨𝐯𝐞𝐝 𝐣𝐮𝐬𝐭 𝐟𝐫𝐨𝐦 𝐬𝐭𝐚𝐠𝐞𝐝.
</p><p><strong>Summary</strong>: You removed files from staged and then pushed them will result in the deletion of files on the collaborating team's local repository as well (𝘸𝘩𝘪𝘭𝘦 𝘺𝘰𝘶 𝘩𝘢𝘷𝘦 𝘵𝘩𝘰𝘴𝘦 𝘧𝘪𝘭𝘦𝘴 𝘢𝘷𝘢𝘪𝘭𝘢𝘣𝘭𝘦 𝘪𝘯 𝘵𝘩𝘦 𝘶𝘯𝘵𝘳𝘢𝘤𝘬𝘦𝘥 𝘴𝘵𝘢𝘨𝘦, 𝘧𝘰𝘳 𝘢𝘭𝘭 𝘵𝘦𝘢𝘮𝘴 𝘪𝘵 will be 𝘨𝘰𝘯𝘦. )</p>
 </div>||||I|||| Stack Overflow
 1. About
 2. Products
 3. For Teams
 1. Stack Overflow Public questions & answers
 2. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
 3. Talent Build your employer brand
 4. Advertising Reach developers & technologists worldwide
 5. About the company
 Loading…

 1. current community

 + Stack Overflow
 help chat
 + Meta Stack Overflow

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 2. 
 3. Log in
 4. Sign up
 1. Home
 2. 
 1. Public
 2. Questions
 3. Tags
 4. Users
 5. Companies
 6. Collectives
 7. Explore Collectives
 3. 
 1. Teams
 Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
 2. Teams
 3. Create free Team

 Collectives™ on Stack Overflow

 Find centralized, trusted content and collaborate around the technologies you use most.

 Learn more about Collectives

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 Remove a file from a Git repository without deleting it from the local filesystem

 Ask Question
 Asked 13 years, 6 months ago
 Modified 7 days ago
 Viewed 1.1m times
 3667

 I want to remove a file from my repository.

 git rm file_to_remove.txt
 

 will remove the file from the repository, but it will also remove the file from the local file system. How do I remove this file from the repo without deleting my local copy of the file?

 * git
 * repository
 * remote-server
 * delete-file
 * git-rm
 Share
 Improve this question
 Follow
 edited Jul 25, 2022 at 16:58
 Mateen Ulhaq
 23k 16 16 gold badges 89 89 silver badges 130 130 bronze badges
 asked Jul 17, 2009 at 14:56
 mveerman mveerman
 37.6k 3 3 gold badges 21 21 silver badges 14 14 bronze badges
 6
 * 4
 Duplicate of Stop tracking and ignore changes to a file in Git
 – user456814
 May 24, 2014 at 23:21
 * 43
 It's worth noting that the most upvoted answer is dangerous for some. If you are using a remote repo than when you push your local then pull elsewhere those files you removed from git only WILL BE DELETED . This is mentioned in one of the replies but not commented upon.
 – RichieHH
 Aug 24, 2018 at 4:17
 * 1
 to do it the right way: stackoverflow.com/questions/57418769/…
 – goofology
 Aug 11, 2020 at 19:35
 * 1
 Critical warning: Please read this answer before pasting accepted answer. stackoverflow.com/a/67793572/6073148
 – Zahid Khan
 May 30, 2022 at 8:21
 * @goofology As far as I can see that linked question has no accepted answer. So what is the right way to do this??
 – becko
 Sep 3, 2022 at 11:12
 | Show 1 more comment

 13 Answers

 Sorted by: Reset to default
 Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
 5174

 The git rm documentation states:

 When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

 So, for a single file:

 git rm --cached file_to_remove.txt
 

 and for a single directory:

 git rm --cached -r directory_to_remove
 
 Share
 Improve this answer
 Follow
 edited Jul 25, 2022 at 5:07
 Mateen Ulhaq
 23k 16 16 gold badges 89 89 silver badges 130 130 bronze badges
 answered Jul 17, 2009 at 14:57
 bdonlan bdonlan
 220k 29 29 gold badges 264 264 silver badges 321 321 bronze badges
 11
 * 195
 Easily missed because it is not as self explanatory as svn rm --keep-local .
 – Martin
 Jun 24, 2011 at 11:44
 * 138
 But how do I preserve the files on remote servers? This keeps my local one, but if I push and pull from another server, the file gets deleted. I also added a .gitignore for the file, but it still get's removed
 – spankmaster79
 Feb 22, 2013 at 15:44
 * 14
 This still removes the files on git pull if you are behind the commit after git rm
 – Petr Peller
 May 22, 2014 at 17:02
 * 15
 Worth noting that, after running the command in the answer, you need to use git commit -m "Commit message" and git push . If you have any other staged changes (check with git status ), they will also be committed at this time.
 – Sinjai
 Sep 11, 2017 at 20:29
 * 24
 Since this is the most accepted answer and doesn't do, what is asked for, i'll tell what I do. I use the command git rm --cached mylogfile.log and delete the file from the repository. To avoid losing the file on productive system i do a backup of the file and pull after this. The file get's deleted as mentioned before and need to be copied back from your backup. This is quite a pain, but i found no better solution for this problem.
 – Marcel Grolms
 Dec 14, 2017 at 18:37
 | Show 6 more comments
 311

 To remove an entire folder from the repo (like Resharper files), do this:

 git rm -r --cached folderName
 

 I had committed some resharper files, and did not want those to persist for other project users.

 Share
 Improve this answer
 Follow
 edited Oct 2, 2012 at 9:25
 konsolenfreddy
 9,550 1 1 gold badge 25 25 silver badges 36 36 bronze badges
 answered Apr 4, 2012 at 18:14
 Sam Tyson Sam Tyson
 4,406 4 4 gold badges 26 26 silver badges 34 34 bronze badges
 1
 * 13
 Just an added note for future visitors: Don't use a GUI to "Sync" the commit back to the repo. That will pull the files back down into your local repo. You have to do a Git Push repo branch to actually remove the files from the remote.
 – RubberDuck
 Dec 4, 2014 at 4:24
 Add a comment |
 264

 To remove files from the repository based on .gitignore, without deleting them from the local file system:

 git rm --cached `git ls-files -i -c -X .gitignore`
 

 For Windows Powershell:

 git rm --cached $(git ls-files -i -c -X .gitignore)
 
 Share
 Improve this answer
 Follow
 edited Jan 29 at 19:48
 Joel Harkes
 10.6k 3 3 gold badges 45 45 silver badges 64 64 bronze badges
 answered Jan 31, 2014 at 10:05
 null null
 3,699 1 1 gold badge 20 20 silver badges 27 27 bronze badges
 9
 * 8
 Does not work on Windows. git ls-files -i -X .gitignore works, but I dont know how to send the files to 'git rm'. Do you know how to do that?
 – Erik Z
 May 9, 2014 at 6:04
 * 18
 Works on Windows if you use Git Bash instead of cmd-console
 – Andreas Zita
 Dec 16, 2014 at 18:12
 * 17
 Love this. Worked except that I had some files that ended up having spaces in filename. I modified solution here to this: git ls-files -i -X .gitignore | xargs -I{} git rm --cached "{}" . Please consider modifying or adding this solution to the answer here, because it is a great tool to have...
 – mpettis
 Mar 12, 2016 at 17:48
 * 1
 I didn't try but it should be tested will it remove also files like .gitkeep which preserves an empty folder in repository. Eg. .gitignore contain folder uploads and repo is forced to keep track of .gitkeep . By removing all from repo under uploads it will remove also .gitkeep .
 – Vladimir Vukanac
 Jul 7, 2016 at 9:11
 * 2
 This suggestion worked perfectly in powershell: git rm --cached $(git ls-files -i -X .gitignore)
 – geekandglitter
 Apr 21, 2019 at 11:30
 | Show 4 more comments
 124

 As per my Answer here: https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in-my-github-repository

 To remove folder/directory or file only from git repository and not from the local try 3 simple steps.

 Steps to remove directory

 git rm -r --cached File-or-FolderName
 git commit -m "Removed folder from repository"
 git push origin master
 

 Steps to ignore that folder in next commits

 To ignore that folder from next commits make one file in root named .gitignore and put that folders name into it. You can put as many as you want

 .gitignore file will be look like this

 /FolderName
 

 Share
 Improve this answer
 Follow
 edited Jun 20, 2020 at 9:12
 Community Bot
 1 1 1 silver badge
 answered Aug 24, 2015 at 12:16
 Suresh Karia Suresh Karia
 17k 18 18 gold badges 66 66 silver badges 84 84 bronze badges
 1
 * Will this delete other people's local files when they pull down your changes though?
 – Brad Mace
 Mar 30, 2022 at 16:44
 Add a comment |
 81

 A more generic solution:

 1. Edit .gitignore file.

 echo mylogfile.log >> .gitignore

 2. Remove all items from index.

 git rm -r -f --cached .

 3. Rebuild index.

 git add .

 4. Make new commit

 git commit -m "Removed mylogfile.log"

 Share
 Improve this answer
 Follow
 edited Jul 2, 2020 at 12:49
 Math Bob
 113 1 1 silver badge 5 5 bronze badges
 answered Dec 13, 2013 at 11:42
 mAsT3RpEE mAsT3RpEE
 1,810 1 1 gold badge 17 17 silver badges 14 14 bronze badges
 7
 * 3
 Will this actually delete the file ?
 – Mr_and_Mrs_D
 Dec 13, 2013 at 15:39
 * 6
 From GitHub? NO. If you have already pushed to github it will not remove it from the site. But it will update your local git repository.
 – mAsT3RpEE
 Dec 14, 2013 at 12:37
 * 7
 The comment you deleted was more eliminated actually :) The problem with the solution rm --cashed is that it will eventually delete the file when one pulls - right ? And this is not what people want when they say "Remove a file from the repository without deleting it from the local filesystem ". Now why was the solution above accepted is beyond me - probably the OP was working alone and never pulled ? Dunno. I understand the github "once pushed always there" issue ofc
 – Mr_and_Mrs_D
 Dec 14, 2013 at 12:42
 * I don't think there is a 100% solution unless you ask github itself. For now stick to this. Copy file, Add to gitignore, do actual git rm -r, commit, push, restore file. Did you manage to find another solution?
 – mAsT3RpEE
 Dec 14, 2013 at 12:58
 * 12
 My problem is not github - it is that the file will actually be deleted from coworkers when they pull . I don't want the file to be deleted. This has caused me huge issues in the past. So I was wondering if there is really a solution that really does not delete the file. See also the comment inn the accepted answer : stackoverflow.com/questions/1143796/…
 – Mr_and_Mrs_D
 Dec 14, 2013 at 13:07
 | Show 2 more comments
 76

 Also, if you have commited sensitive data (e.g. a file containing passwords), you should completely delete it from the history of the repository. Here's a guide explaining how to do that: http://help.github.com/remove-sensitive-data/

 Share
 Improve this answer
 Follow
 answered Aug 21, 2011 at 12:16
 BoD BoD
 10.7k 6 6 gold badges 63 63 silver badges 58 58 bronze badges
 2
 * 22
 This answer should include the required commands to complete this task instead of linking to another website.
 – Florian Lemaitre
 Feb 2, 2018 at 13:08
 * 1
 I would also note that I found using the git bfg repo cleaner tool easier and faster.
 – Lubed Up Slug
 Feb 14, 2019 at 22:52
 Add a comment |
 28

 Git lets you ignore those files by assuming they are unchanged. This is done by running the git update-index --assume-unchanged path/to/file.txt command. Once marking a file as such, git will completely ignore any changes on that file; they will not show up when running git status or git diff, nor will they ever be committed.

 (From https://help.github.com/articles/ignoring-files)

 Hence, not deleting it, but ignoring changes to it forever. I think this only works locally, so co-workers can still see changes to it unless they run the same command as above. (Still need to verify this though.)

 Note: This isn't answering the question directly, but is based on follow up questions in the comments of the other answers.

 Share
 Improve this answer
 Follow
 answered Jan 28, 2014 at 11:46
 Rystraum Rystraum
 1,965 1 1 gold badge 20 20 silver badges 32 32 bronze badges
 1
 * 4
 Hm, I do this but I see that the file can be overwritten if someone else makes changes to it on the repo.
 – AlxVallejo
 Sep 15, 2014 at 16:50
 Add a comment |
 26

 If you want to just untrack a file and not delete from local and remote repo then use this command:

 git update-index --assume-unchanged file_name_with_path
 
 Share
 Improve this answer
 Follow
 edited Jan 21, 2021 at 6:58
 answered Aug 15, 2018 at 7:33
 Afraz Ahmad Afraz Ahmad
 4,973 28 28 silver badges 37 37 bronze badges
 1
 * 8
 While this is a good answer, it's important to note this doesn't "untrack" a file in the sense that people usually use that word with Git, where an untracked file is one that isn't in the repository history and never has been. What this answer does is keep the file in the repository but prevent Git from noticing that changes have been made to it. That has some significant differences -- most importantly, the file is still present for others, and if someone else makes changes to it and you pull, your local copy can be overwritten without confirmation.
 – Soren Bjornstad
 Oct 22, 2019 at 14:02
 Add a comment |
 15

 Ignore the files, remove the files from git, update git (for the removal).

 Note : this does not deal with history for sensitive information.

 This process definitely takes some undertanding of what is going on with git. Over time, having gained that, I've learned to do processes such as:

 1) Ignore the files

 * Add or update the project .gitignore to ignore them - in many cases such as yours, the parent directory, e.g. log/ will be the regex to use.
 * commit and push that .gitignore file change (not sure if push needed mind you, no harm if done).

 2) Remove the files from git (only).

 * Now remove the files from git (only) with git rm --cached some_dir/
 * Check that they still remain locally (they should!).

 3) Add and commit that change (essentially this is a change to "add" deleting stuff, despite the otherwise confusing "add" command!)

 * git add .
 * git commit -m"removal"
 Share
 Improve this answer
 Follow
 edited Aug 5, 2021 at 10:09
 Arthur Bowers
 534 4 4 silver badges 9 9 bronze badges
 answered Apr 14, 2020 at 15:37
 Michael Durrant Michael Durrant
 92k 94 94 gold badges 319 319 silver badges 483 483 bronze badges
 1
 * see this questions for details of why this is a bad answer: stackoverflow.com/questions/57418769/…
 – Ilya Kolesnikov
 Mar 12, 2021 at 7:36
 Add a comment |
 8

 Above answers didn't work for me. I used filter-branch to remove all committed files.

 Remove a file from a git repository with:

 git filter-branch --tree-filter 'rm file'
 

 Remove a folder from a git repository with:

 git filter-branch --tree-filter 'rm -rf directory'
 

 This removes the directory or file from all the commits.

 You can specify a commit by using:

 git filter-branch --tree-filter 'rm -rf directory' HEAD
 

 Or an range:

 git filter-branch --tree-filter 'rm -rf vendor/gems' t49dse..HEAD
 

 To push everything to remote, you can do:

 git push origin master --force
 
 Share
 Improve this answer
 Follow
 edited Feb 20, 2019 at 3:48
 Samuel Philipp
 10.4k 12 12 gold badges 34 34 silver badges 55 55 bronze badges
 answered Jan 12, 2016 at 13:59
 Martijn Mellens Martijn Mellens
 520 7 7 silver badges 25 25 bronze badges
 3
 * 2
 This combined with git rm -r --cached NAME is the trick to remove it from your local git repo and prevent it from affecting anyone who pulls later (by deleting history of the file or directory from git.)
 – notbad.jpeg
 Mar 17, 2016 at 20:15
 * 1
 This rewrites git history and you would need to push --force after, this is a bit out of scope with the question I guess. On a public famous repo you can't just change the history line like that as everyone having already cloned the repo would get issues when pulling.
 – Guillaume Perrot
 Apr 1, 2017 at 1:37
 * From the man page "git filter-branch has a plethora of pitfalls .... Please use an alternative history filtering tool such as git filter-repo". Also, if you are going to rewrite history, I guess you could modify the ignore file at an early age so that the removed item(s) are ignored throughout the new history?
 – Rodney
 Feb 25, 2021 at 5:20
 Add a comment |
 6

 I would like to add to the accepted answer of @bdonlan.

 DON'T USE THIS ANSWER TO REMOVE FILE(S) THAT EXISTS ON REMOTE.

 git rm --cached filename
 

 What the answer is supposed to do?

 It is supposed to remove some files from the local staged area that you have mistakenly committed in some previous commit(s).

 1. And have not pushed to the remote.
 2. And if pushed on remote, others don't care about those changes.

 It moves files from Tracked 𝐭𝐨 Untracked state by that what I mean is, it deletes the files and adds them again.

 So, git doesn't know about them anymore.

 What could go wrong?

 On remote, there is 𝐧𝐨 such thing as an 𝐮𝐧𝐭𝐫𝐚𝐜𝐤𝐞𝐝 𝐬𝐭𝐚𝐭𝐞, there is 𝐨𝐧𝐥𝐲 𝐭𝐫𝐚𝐜𝐤𝐞𝐝 𝐬𝐭𝐚𝐭𝐞 and that results in havoc.


 Why?

 When collaborating with the team, if you 𝐩𝐮𝐬𝐡 such changes up to remote it will delete those changes on remote and all the team who takes a 𝐩𝐮𝐥𝐥 from 𝐫𝐞𝐦𝐨𝐭𝐞 𝐰𝐢𝐥𝐥 𝐜𝐚𝐮𝐬𝐞 𝐝𝐞𝐥𝐞𝐭𝐢𝐨𝐧 𝐨𝐟 𝐭𝐡𝐨𝐬𝐞 𝐟𝐢𝐥𝐞𝐬 𝐰𝐡𝐢𝐜𝐡 𝐲𝐨𝐮 𝐡𝐚𝐯𝐞 𝐣𝐮𝐬𝐭 𝐫𝐞𝐦𝐨𝐯𝐞𝐝 𝐣𝐮𝐬𝐭 𝐟𝐫𝐨𝐦 𝐬𝐭𝐚𝐠𝐞𝐝.


 Summary: You removed files from staged and then pushed them will result in the deletion of files on the collaborating team's local repository as well (𝘸𝘩𝘪𝘭𝘦 𝘺𝘰𝘶 𝘩𝘢𝘷𝘦 𝘵𝘩𝘰𝘴𝘦 𝘧𝘪𝘭𝘦𝘴 𝘢𝘷𝘢𝘪𝘭𝘢𝘣𝘭𝘦 𝘪𝘯 𝘵𝘩𝘦 𝘶𝘯𝘵𝘳𝘢𝘤𝘬𝘦𝘥 𝘴𝘵𝘢𝘨𝘦, 𝘧𝘰𝘳 𝘢𝘭𝘭 𝘵𝘦𝘢𝘮𝘴 𝘪𝘵 will be 𝘨𝘰𝘯𝘦. )

 Share
 Improve this answer
 Follow
 edited Jun 2, 2021 at 4:30
 answered Jun 1, 2021 at 17:35
 Zahid Khan Zahid Khan
 1,729 2 2 gold badges 16 16 silver badges 26 26 bronze badges
 4
 * 2
 Without an alternate solution, this is a (very important) comment but not an answer.
 – Brian Z
 May 29, 2022 at 14:38
 * But, doesn't git keep a history to this file? I mean, we could always revert back?
 – Raynal Gobel
 Jun 10, 2022 at 5:59
 * @RaynalGobel yes git has all the history available and you can revert back anytime. But the thing is should you do it? Like you delete your collaborator's file while you have that file available locally? In my case, the lead trusts me and merges my PR blindly. The application wasn't working on his machine while it was working on mine. Think about this scenario!
 – Zahid Khan
 Jun 10, 2022 at 7:29
 * 1
 Agree with @BrianZ, this is a very important comment . It would be nice to complete it with a suggestion of the right way to do it!
 – becko
 Sep 3, 2022 at 11:10
 Add a comment |
 1

 This depends on what you mean by 'remove' from git. :)

 You can unstage a file using git rm --cached see for more details. When you unstage something, it means that it is no longer tracked, but this does not remove the file from previous commits.

 If you want to do more than unstage the file, for example to remove sensitive data from all previous commits you will want to look into filtering the branch using tools like the BFG Repo-Cleaner.

 Share
 Improve this answer
 Follow
 edited Nov 15, 2020 at 18:20
 Dharman ♦
 29.3k 21 21 gold badges 79 79 silver badges 131 131 bronze badges
 answered Nov 15, 2020 at 18:14
 Zach Zach
 882 5 5 silver badges 17 17 bronze badges
 Add a comment |
 0

 since rm --cache will delete files in the remote repository, you could use update-index instead
 see: https://learn.microsoft.com/en-us/azure/devops/repos/git/ignore-files?view=azure-devops&tabs=visual-studio-2019

 Share
 Improve this answer
 Follow
 answered Jun 13, 2022 at 10:32
 hj yan hj yan
 21 4 4 bronze badges
 Add a comment |
 Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

 Not the answer you're looking for? Browse other questions tagged
 * git
 * repository
 * remote-server
 * delete-file
 * git-rm
 or ask your own question.

 * The Overflow Blog
 * Announcing more ways to learn and grow your skills
 * Why developer experience is the key to better software, straight from the...
 * Featured on Meta
 * Accessibility Update: Colors
 * 2022: a year in moderation
 * Collectives: The next iteration
 * We’re bringing advertisements for technology courses to Stack Overflow
 * Temporary policy: ChatGPT is banned

 Linked

 407
 How do I git rm a file without deleting it from disk?
 26
 How to delete a file in remote repository ONLY?
 3
 How to remove a file from git repository without deleting the file actually
 2
 Remove an ignored directory from Github
 1
 Remove folder in git repo?
 1577
 How do I remove a directory from a Git repository?
 935
 How to ignore certain files in Git
 580
 Applying .gitignore to committed files
 371
 Accidentally committed .idea directory files into git
 404
 How do I add Git submodule to a sub-directory?
 See more linked questions

 Related

 7884
 How do I remove local (untracked) files from the current Git working tree?
 10875
 How do I undo 'git add' before commit?
 25328
 How do I undo the most recent local commits in Git?
 3133
 How do I find and restore a deleted file in a Git repository?
 9163
 How do I force "git pull" to overwrite local files?
 5312
 Reset local repository branch to be just like remote repository HEAD
 5766
 How do I change the URI (URL) for a remote Git repository?
 5420
 How do I push a new local branch to a remote Git repository and track it too?
 5196
 How to determine the URL that a local Git repository was originally cloned from

 Hot Network Questions

 * Don't repeat yourself
 * Why does Ontario have a quota on milk production?
 * Understanding how to terminate recurrence relations in quantum SHO
 * Which Deep Learning Method to use for the classification of precious stones (diamonds, sapphire, ruby etc) based on digital photo images and data set?
 * Why is b.pop(0) over 200 times slower than del b[0] for bytearray?
 * Other than looking cool as hell, what is the reason behind the TSR-2's anhedral wingtips?
 * Do famous people really have the power to ruin the career of unrelated researchers?
 * Slings for giant
 * Can poor grades in the bachelor's affect working in the academia?
 * How to decide the sequence of cheeses for a cheese tasting event?
 * Some conjectures about prime gaps
 * Why are the trailing edges of wings not always made as 'sharp' as possible?
 * How big of an explosion can you make with a fusion reactor?
 * How to download only certain columns of a binary file using wget?
 * How can an Indian homemaker spouse be guaranteed a Schengen tourist visa?
 * Isekai manga where the main character is a healer but gets his limbs chopped off
 * What does it mean for two properties of objects to be of the same class (or even identical)?
 * How many spells require expensive components for each casting?
 * Is this Stanford certificate real?
 * Addition and multiplication are commutative but exponentiation and tetration are not. Do we know why?
 * How to find the safest place to cross a frozen river from a map?
 * What did Paul McCartney mean by "Oh, yesterday came suddenly"?
 * Stable words in the Proto-Indo-European language family
 * Polynomial approximation of max function
 more hot questions
 Question feed

 Subscribe to RSS

 Question feed

 To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

 Stack Overflow

 * Questions
 * Help

 Products

 * Teams
 * Advertising
 * Collectives
 * Talent

 Company

 * About
 * Press
 * Work Here
 * Legal
 * Privacy Policy
 * Terms of Service
 * Contact Us
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.2.6.43220

 Your privacy

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

 Accept all cookies Customize settings
I would like to add to the accepted answer of @bdonlan.
 DON'T USE THIS ANSWER TO REMOVE FILE(S) THAT EXISTS ON REMOTE.
 git rm --cached filename
 
What the answer is supposed to do?
It is supposed to remove some files from the local staged area that you have mistakenly committed in some previous commit(s). 
 
And have not pushed to the remote.
And if pushed on remote, others don't care about those changes.
It moves files from Tracked 𝐭𝐨 Untracked state by that what I mean is, it deletes the files and adds them again.
So, git doesn't know about them anymore.
 What could go wrong?  
On remote, there is 𝐧𝐨 such thing as an 𝐮𝐧𝐭𝐫𝐚𝐜𝐤𝐞𝐝 𝐬𝐭𝐚𝐭𝐞, there is 𝐨𝐧𝐥𝐲 𝐭𝐫𝐚𝐜𝐤𝐞𝐝 𝐬𝐭𝐚𝐭𝐞 and that results in havoc.
 
 Why?  
When collaborating with the team, if you 𝐩𝐮𝐬𝐡 such changes up to remote it will delete those changes on remote and all the team who takes a 𝐩𝐮𝐥𝐥 from 𝐫𝐞𝐦𝐨𝐭𝐞 𝐰𝐢𝐥𝐥 𝐜𝐚𝐮𝐬𝐞 𝐝𝐞𝐥𝐞𝐭𝐢𝐨𝐧 𝐨𝐟 𝐭𝐡𝐨𝐬𝐞 𝐟𝐢𝐥𝐞𝐬 𝐰𝐡𝐢𝐜𝐡 𝐲𝐨𝐮 𝐡𝐚𝐯𝐞 𝐣𝐮𝐬𝐭 𝐫𝐞𝐦𝐨𝐯𝐞𝐝 𝐣𝐮𝐬𝐭 𝐟𝐫𝐨𝐦 𝐬𝐭𝐚𝐠𝐞𝐝.
  Summary: You removed files from staged and then pushed them will result in the deletion of files on the collaborating team's local repository as well (𝘸𝘩𝘪𝘭𝘦 𝘺𝘰𝘶 𝘩𝘢𝘷𝘦 𝘵𝘩𝘰𝘴𝘦 𝘧𝘪𝘭𝘦𝘴 𝘢𝘷𝘢𝘪𝘭𝘢𝘣𝘭𝘦 𝘪𝘯 𝘵𝘩𝘦 𝘶𝘯𝘵𝘳𝘢𝘤𝘬𝘦𝘥 𝘴𝘵𝘢𝘨𝘦, 𝘧𝘰𝘳 𝘢𝘭𝘭 𝘵𝘦𝘢𝘮𝘴 𝘪𝘵 will be 𝘨𝘰𝘯𝘦. )
Remove a file from a Git repository without deleting it from the local filesystem
https://stackoverflow.com/questions/1143796/remove-a-file-from-a-git-repository-without-deleting-it-from-the-local-filesyste
https://stackoverflow.com/questions/6313126/how-do-i-remove-a-directory-from-a-git-repository
How do I remove a directory from a Git repository?
2022-09-06
Sahat Yalkabov Sahat Yalkabov
<div><div>
<p>I already had committed the folder before and want to remove the directory in the history as well.</p>
<p>I did the following:</p>
<p>Add folder to <code>.gitignore</code>:</p>
<pre><code>echo Folder_Name/ &gt;&gt; .gitignore
</code></pre>
<p>Remove from all commits:</p>
<pre><code>git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch Folder_Name/' --prune-empty --tag-name-filter cat -- --all
</code></pre>
<p>remove the refs from the old commits:</p>
<pre><code>git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
</code></pre>
<p>Ensure all old refs are fully removed</p>
<pre><code>rm -Rf .git/logs .git/refs/original
</code></pre>
<p>Perform a garbage collection</p>
<pre><code>git gc --prune=all --aggressive
</code></pre>
<p>push you changes to the online repository:</p>
<pre><code>git push
</code></pre>
<p>You are done here.</p>
<p>But you can to the following to push all the changes to all branches with:
But be careful with this command!</p>
<pre><code>git push origin --all --force
git push origin --tags --force
</code></pre>
<p>After that the folder was removed from git, but was not deleted from local disk.</p>
 </div>||||I|||| Stack Overflow
 1. About
 2. Products
 3. For Teams
 1. Stack Overflow Public questions & answers
 2. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
 3. Talent Build your employer brand
 4. Advertising Reach developers & technologists worldwide
 5. About the company
 Loading…

 1. current community

 + Stack Overflow
 help chat
 + Meta Stack Overflow

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 2. 
 3. Log in
 4. Sign up
 1. Home
 2. 
 1. Public
 2. Questions
 3. Tags
 4. Users
 5. Companies
 6. Collectives
 7. Explore Collectives
 3. 
 1. Teams
 Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
 2. Teams
 3. Create free Team

 Collectives™ on Stack Overflow

 Find centralized, trusted content and collaborate around the technologies you use most.

 Learn more about Collectives

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 How do I remove a directory from a Git repository?

 Ask Question
 Asked 11 years, 8 months ago
 Modified 2 months ago
 Viewed 1.8m times
 1577

 How can I delete a single directory containing files from a Git repository?

 * git
 * delete-file
 Share
 Follow
 edited Sep 6, 2022 at 16:58
 John Smith
 7,143 6 6 gold badges 48 48 silver badges 61 61 bronze badges
 asked Jun 10, 2011 at 23:54
 Sahat Yalkabov Sahat Yalkabov
 31.9k 43 43 gold badges 109 109 silver badges 174 174 bronze badges
 1
 * 13
 You need only: git rm -r --cached folder_name
 – Vadim
 Aug 30, 2020 at 21:41
 Add a comment |

 16 Answers

 Sorted by: Reset to default
 Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
 2942

 Remove directory from Git and local

 Checkout 'master' with both directories:

 git rm -r one-of-the-directories // This deletes from filesystem
 git commit . -m "Remove duplicated directory"
 git push origin <your-git-branch> (typically 'master', but not always)
 

 Remove directory from Git but NOT local

 To remove this directory from Git, but not delete it entirely from the filesystem (local):

 git rm -r --cached myFolder
 
 Share
 Follow
 edited Sep 6, 2022 at 16:59
 John Smith
 7,143 6 6 gold badges 48 48 silver badges 61 61 bronze badges
 answered Jun 11, 2011 at 0:37
 karmakaze karmakaze
 33.7k 1 1 gold badge 29 29 silver badges 32 32 bronze badges
 17
 * 11
 I had same exact situation. When I tried git rm -r myFolder it worked, but also deleted everything from "MyFolder" directory. Had to revert everything in the "MyFolder" directory and then commit.
 – Justin
 Oct 25, 2012 at 13:12
 * 284
 @Justin To only remove from git and leave filesystem as-is, use git rm -r --cached myFolder
 – karmakaze
 Nov 16, 2012 at 6:28
 * 52
 Error message I get is: fatal: pathspec 'directory' did not match any files.
 – user2441441
 Sep 8, 2016 at 21:14
 * 5
 @user2441441 I had this error poping up when I was trying to remove a folder/file that was not added to the repo at all
 – luke-at-work
 Oct 13, 2016 at 13:36
 * 7
 git rm -r --cached directory/ only removes from git tracked, but the repository structure remains.
 – Néstor
 Aug 29, 2018 at 16:57
 | Show 12 more comments
 443

 To remove folder/directory only from git repository and not from the local try 3 simple commands.

 Steps to remove directory

 git rm -r --cached FolderName
 git commit -m "Removed folder from repository"
 git push origin master
 

 Steps to ignore that folder in next commits

 To ignore that folder from next commits make one file in root folder (main project directory where the git is initialized) named .gitignore and put that folder name into it. You can ignore as many files/folders as you want

 .gitignore file will look like this

 /FolderName
 

 Share
 Follow
 edited Jul 30, 2020 at 7:16
 answered Dec 3, 2014 at 12:34
 Suresh Karia Suresh Karia
 17k 18 18 gold badges 66 66 silver badges 84 84 bronze badges
 6
 * 1
 You will also need to do the git rm -r --cached FolderName git commit -m "Removed folder from repository" on your other computer and then do a pull to keep it in synch.
 – Simon
 Oct 6, 2015 at 17:07
 * 1
 Will this delete the directory from the point it was added to repository till now? Or is it just to remove the directory and recommit it to the .git repo?
 – alpha_989
 Jan 21, 2018 at 18:15
 * Why not just do a rm -rf FolderName; git add -A; git commit -m 'removed FolderName ?
 – alpha_989
 Jan 21, 2018 at 18:18
 * 1
 well done. On my machine, the third step doesnt work. But it works if it is changed to $git push
 – Hong
 Aug 1, 2019 at 1:57
 * Unlucky for me: robdavisprojects@Robs-MacBook-Air default % ls default.services.yml drushrc.php settings.ddev.php default.settings.php files settings.php robdavisprojects@Robs-MacBook-Air default % git rm -r --cached files fatal: pathspec 'files' did not match any files robdavisprojects@Robs-MacBook-Air default %
 – therobyouknow
 Dec 15, 2019 at 13:54
 | Show 1 more comment
 88

 If, for some reason, what karmakaze said doesn't work, you could try deleting the directory you want using or with your file system browser (ex. In Windows File Explorer). After deleting the directory, issuing the command:
 git add -A
 and then
 git commit -m 'deleting directory'
 and then
 git push origin master.

 Share
 Follow
 edited Feb 20, 2019 at 7:07
 sophros
 13.7k 9 9 gold badges 44 44 silver badges 69 69 bronze badges
 answered Jun 11, 2011 at 2:14
 cmcculloh cmcculloh
 46.9k 39 39 gold badges 100 100 silver badges 129 129 bronze badges
 3
 * 4
 This worked for me. Could you explain the -A argument ?
 – Arun
 Oct 15, 2018 at 7:10
 * 2
 @Arun -A is short for --all and add changes from all tracked and untracked files
 – Joachim Rohde
 Apr 3, 2019 at 10:58
 * 'origin' does not appear to be a git repository
 – Jens Mander
 Sep 15, 2021 at 3:44
 Add a comment |
 29

 I already had committed the folder before and want to remove the directory in the history as well.

 I did the following:

 Add folder to .gitignore:

 echo Folder_Name/ >> .gitignore
 

 Remove from all commits:

 git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch Folder_Name/' --prune-empty --tag-name-filter cat -- --all
 

 remove the refs from the old commits:

 git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
 

 Ensure all old refs are fully removed

 rm -Rf .git/logs .git/refs/original
 

 Perform a garbage collection

 git gc --prune=all --aggressive
 

 push you changes to the online repository:

 git push
 

 You are done here.

 But you can to the following to push all the changes to all branches with: But be careful with this command!

 git push origin --all --force
 git push origin --tags --force
 

 After that the folder was removed from git, but was not deleted from local disk.

 Share
 Follow
 edited May 21, 2019 at 4:04
 marc_s
 722k 173 173 gold badges 1320 1320 silver badges 1442 1442 bronze badges
 answered May 14, 2019 at 23:59
 Skyborg Skyborg
 814 13 13 silver badges 13 13 bronze badges
 Add a comment |
 28

 You can try this: git rm -rf <directory_name>

 It will force delete the directory.

 Share
 Follow
 answered Oct 23, 2013 at 10:21
 Breen ho Breen ho
 1,571 13 13 silver badges 23 23 bronze badges
 1
 * 13
 this will also delete from local directory
 – Abdul Rehman Kaim Khani
 Mar 24, 2019 at 14:10
 Add a comment |
 19

 If you remove the files in the directory (with git rm as the other answers explain), then the directory no longer exists as far as git is concerned. You cannot commit an empty directory, nor can you remove one.

 This is unlike subversion where you have to explicitly svn rm emptyfolder/, and is incidentally why the man page for git describes itself as "the stupid content tracker"

 An answer on "How do I add an empty directory to a git repository" links to the FAQ on this subject:

 Currently the design of the git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.

 Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.

 You can say "git add <dir>" and it will add files in there.

 If you really need a directory to exist in checkouts you should create a file in it. .gitignore works well for this purpose; you can leave it empty, or fill in the names of files you expect to show up in the directory.

 Share
 Follow
 edited May 23, 2017 at 12:26
 Community Bot
 1 1 1 silver badge
 answered Aug 6, 2013 at 9:01
 dbr dbr
 164k 66 66 gold badges 274 274 silver badges 341 341 bronze badges
 Add a comment |
 8

 for deleting Empty folders

 i wanted to delete an empty directory(folder) i created, git can not delete it, after some research i learned Git doesn't track empty directories. If you have an empty directory in your working tree you should simply removed it with

 rm -r folderName
 

 There is no need to involve Git.

 Share
 Follow
 edited Jan 10, 2019 at 4:37
 answered Dec 19, 2018 at 1:59
 Ridha Rezzag Ridha Rezzag
 3,517 29 29 silver badges 38 38 bronze badges
 0
 Add a comment |
 7

 Go to your git Directory then type the following command: rm -rf <Directory Name>

 After Deleting the directory commit the changes by: git commit -m "Your Commit Message"

 Then Simply push the changes on remote GIT directory: git push origin <Branch name>

 Share
 Follow
 answered Jul 13, 2016 at 11:53
 anky anky
 71 1 1 silver badge 5 5 bronze badges
 Add a comment |
 5

 You can delete the folder locally and then push, as follow:

 git rm -r folder_name
 git commit -m "your commit"
 git push origin master
 
 Share
 Follow
 answered Feb 26, 2016 at 18:59
 Shrikant Shrikant
 383 6 6 silver badges 7 7 bronze badges
 Add a comment |
 4

 I usually use git add --all to remove files / folders from remote repositories

 rm -r folder_name
 git add --all
 git commit -m "some comment"
 git push origin master
 

 master can be replaced by any other branch of the repository.

 Share
 Follow
 answered Sep 5, 2016 at 15:41
 ettanany ettanany
 18.5k 8 8 gold badges 43 43 silver badges 61 61 bronze badges
 Add a comment |
 2

 You can use Attlasian Source Tree (Windows) (https://www.atlassian.com/software/sourcetree/overview). Just select files from tree and push button "Remove" at the top. Files will be deleted from local repository and local git database. Then Commit, then push.

 Share
 Follow
 answered Sep 1, 2015 at 18:04
 Sergey Groshev Sergey Groshev
 21 3 3 bronze badges
 0
 Add a comment |
 2

 A combination of the 2 answers worked for me

 git rm -r one-of-the-directories
 git commit . -m "Remove duplicated directory"
 git push
 

 if it still shows some warning, remove the files manually

 git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD
 git push
 
 Share
 Follow
 answered Dec 31, 2019 at 13:01
 user3821178 user3821178
 323 5 5 silver badges 14 14 bronze badges
 Add a comment |
 2

 If you are deleting directory from GitHub web application, you will find a delete option. However, after that it will give you list of files being deleted, with commit option at the bottom. Do remember to commit it. Otherwise, directory will not be deleted.

 Reference - https://docs.github.com/en/repositories/working-with-files/managing-files/deleting-files-in-a-repository

 Share
 Follow
 answered Nov 22, 2022 at 7:46
 Mihir Kagrana Mihir Kagrana
 471 4 4 silver badges 4 4 bronze badges
 Add a comment |
 1

 One of my colleague suggested BFG Repo-Cleaner which I think powerful. It is not only delete unwanted data but also clean your repository from any related commit information.

 Share
 Follow
 answered Nov 16, 2016 at 14:28
 thanh.h.le thanh.h.le
 49 3 3 bronze badges
 Add a comment |
 1

 First git command need to know who you are before deleting anything

 1. git init
 2. git config user.name "someone"
 3. git config user.email "someone@someplace.com"
 4. git rm -r
 5. git commit -m "deleting dir"
 6. git push origin master
 Share
 Follow
 answered Jul 6, 2017 at 21:27
 Zeus Zeus
 29 3 3 bronze badges
 Add a comment |
 1

 To add new directory:

 mkdir <YOUR-DIRECTORY>
 

 But now Git is not aware by this new directory, because Git keep tracks of file not directories DIRECTORY

 git status
 

 Git won't be aware with the change we've made, so we add hidden .keep file to make Git aware by this new change.

 touch /YOUR-directory/.keep
 

 Now, if you hit git status Git will be aware with the changes.

 And If you want to delete the directory, you should use this command.

 rm -r <YOUR-DIRECTORY>
 

 And If you checked by using git status, you will see the directory has been removed.

 Share
 Follow
 edited Sep 27, 2018 at 3:37
 Martin Zeitler
 1
 answered Oct 22, 2017 at 15:22
 Atef Atef
 2,832 1 1 gold badge 34 34 silver badges 32 32 bronze badges
 Add a comment |
 Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

 Not the answer you're looking for? Browse other questions tagged
 * git
 * delete-file
 or ask your own question.

 * The Overflow Blog
 * Announcing more ways to learn and grow your skills
 * Why developer experience is the key to better software, straight from the...
 * Featured on Meta
 * Accessibility Update: Colors
 * 2022: a year in moderation
 * Collectives: The next iteration
 * We’re bringing advertisements for technology courses to Stack Overflow
 * Temporary policy: ChatGPT is banned

 Linked

 6
 How to delete one folder from git repo?
 0
 Gitlab folder structure not updating after push
 -3
 How to delete folders from GIT?
 3667
 Remove a file from a Git repository without deleting it from the local filesystem
 808
 Remove directory from remote repository after adding them to .gitignore
 651
 Remove a folder from git tracking
 13
 Why is applicationhost.config still being added to source control even thought it's in gitignore
 1
 Delete deleted files/folder when updating a git repo
 5
 Error: Cannot find module './drivers' with mongoose only on the server
 5
 How do you delete a folder with files in it in Git Bash?
 See more linked questions

 Related

 7884
 How do I remove local (untracked) files from the current Git working tree?
 5142
 How do I add an empty directory to a Git repository?
 5288
 How do I resolve merge conflicts in a Git repository?
 13516
 What is the difference between 'git pull' and 'git fetch'?
 10875
 How do I undo 'git add' before commit?
 25327
 How do I undo the most recent local commits in Git?
 9163
 How do I force "git pull" to overwrite local files?
 8258
 How do I check out a remote Git branch?
 19891
 How do I delete a Git branch locally and remotely?
 11078
 How do I rename a local Git branch?

 Hot Network Questions

 * What would be the next logical step after crossbows?
 * how to modify a wooden fence to keep nubian goats from extending their heads over and eating dwarf tree on other side
 * Why does the Heisenberg uncertainty principle apply to particles?
 * How do we use lowercase `\mathcal` or `\mathscr` in latex overleaf?
 * What is the use of alumium profile with LED strip lighting?
 * Why is b.pop(0) over 200 times slower than del b[0] for bytearray?
 * Inconsistent interline spacing in enumitem
 * Intel 80188 & 8087 clock frequency differences
 * Why did Harry and the others think they could rescue Sirius?
 * Is this Stanford certificate real?
 * How can an Indian homemaker spouse be guaranteed a Schengen tourist visa?
 * Why are ordinary spheres not strictly invertible?
 * GFCI Light Is On, Test Button Doesn't Work
 * In Matthew 26:29 could Christ be referring to the book of Enoch?
 * How many spells require expensive components for each casting?
 * How are local varibales stored on Atmega
 * At what point does combining integrated circuits yield something patentable?
 * Ada and its candidate languages
 * Is there a circuit that takes two single-qubit states and reflects the second around the first?
 * Sum of Consecutive Squares
 * Meaning of "nude" in Saxo Grammaticus
 * How has the resolution of astronomical spectrographs improved over time?
 * Why are atoms electrically neutral?
 * The word "decant" for clone generation
 more hot questions
 Question feed

 Subscribe to RSS

 Question feed

 To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

 Stack Overflow

 * Questions
 * Help

 Products

 * Teams
 * Advertising
 * Collectives
 * Talent

 Company

 * About
 * Press
 * Work Here
 * Legal
 * Privacy Policy
 * Terms of Service
 * Contact Us
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.2.3.43216

 Your privacy

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

 Accept all cookies Customize settings
I already had committed the folder before and want to remove the directory in the history as well.
I did the following:
Add folder to .gitignore:
 echo Folder_Name/ &gt;&gt; .gitignore
 
Remove from all commits:
 git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch Folder_Name/' --prune-empty --tag-name-filter cat -- --all
 
remove the refs from the old commits:
 git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
 
Ensure all old refs are fully removed
 rm -Rf .git/logs .git/refs/original
 
Perform a garbage collection
 git gc --prune=all --aggressive
 
push you changes to the online repository:
 git push
 
You are done here.
But you can to the following to push all the changes to all branches with:
But be careful with this command!
 git push origin --all --force
git push origin --tags --force
 
After that the folder was removed from git, but was not deleted from local disk.
How do I remove a directory from a Git repository?
https://stackoverflow.com/questions/6313126/how-do-i-remove-a-directory-from-a-git-repository
https://superuser.com/questions/1093727/is-there-a-way-to-remove-files-from-a-folder-that-are-in-another-folder
Is there a way to remove files from a folder that are in another folder?
2016-06-26
DarkFire
<div><div>
<p><a href="https://superuser.com/a/1093739/32245">LPChip's answer</a> is the better one.</p>
<p>But because I've started to learn Python, I thought, "Heck, why not write a Python script as answer to this question?"</p>
<h3>Install Python and Send2Trash</h3>
<p>You'd need to install Python before you can run the script from the command line.</p>
<p>Then install <a href="https://pypi.python.org/pypi/Send2Trash">Send2Trash</a> so the deleted files aren't gone irretrievably but end up in the OS's trash:</p>
<pre><code>pip install Send2Trash
</code></pre>
<h3>Create script</h3>
<p>Create a new file with for instance the name <code>DeleteDuplicateInFolderA.py</code></p>
<p>Copy the following script into the file.</p>
<pre><code>#!/usr/bin/python
import sys
import os
from send2trash import send2trash
class DeleteDuplicateInFolderA(object):
 """Given two paths A and B, the application determines which files are in
 path A which are also in path B and then deletes the duplicates from
 path A.
 If the "dry run" flag is set to 'true', files are deleted. Otherwise
 they are only displayed but not deleted.
 """
 def __init__(self, path_A, path_B, is_dry_run=True):
 self._path_A = path_A
 self._path_B = path_B
 self._is_dry_run = is_dry_run
 def get_filenames_in_folder(self, folder_path):
 only_files = []
 for (dirpath, dirnames, filenames) in os.walk(folder_path):
 only_files.extend(filenames)
 return only_files
 def print_files(sel, heading, files):
 print(heading)
 if len(files) == 0:
 print(" none")
 else:
 for file in files:
 print(" {}".format(file))
 def delete_duplicates_in_folder_A(self):
 only_files_A = self.get_filenames_in_folder(self._path_A)
 only_files_B = self.get_filenames_in_folder(self._path_B)
 files_of_A_that_are_in_B = [file for file in only_files_A if file in only_files_B]
 self.print_files("Files in {}".format(self._path_A), only_files_A)
 self.print_files("Files in {}".format(self._path_B), only_files_B)
 if self._is_dry_run:
 self.print_files("These files would be deleted: ", [os.path.join(self._path_A, file) for file in files_of_A_that_are_in_B])
 else:
 print("Deleting files:")
 for filepath in [os.path.join(self._path_A, file) for file in files_of_A_that_are_in_B]:
 print(" {}".format(filepath))
 # os.remove(filepath) # Use this line instead of the next if Send2Trash is not installed
 send2trash(filepath)
if __name__ == "__main__":
 if len(sys.argv) == 4:
 is_dry_run_argument = sys.argv[3]
 if not is_dry_run_argument == "--dryrun":
 println("The 3rd argument must be '--dryrun' or nothing.")
 else:
 app = DeleteDuplicateInFolderA(sys.argv[1], sys.argv[2], is_dry_run=True)
 else:
 app = DeleteDuplicateInFolderA(sys.argv[1], sys.argv[2], is_dry_run=False)
 app.delete_duplicates_in_folder_A()
</code></pre>
<h3>Usage</h3>
<p>Dry run mode, which shows you which files would be delete without actually deleting any files:</p>
<pre><code>c:\temp&gt; python .\DeleteDuplicateInFolderA.py c:\temp\test\A c:\temp\test\B --dryrun
</code></pre>
<p>File deleting mode, which indeed does delete files, so be careful:</p>
<pre><code>c:\temp&gt; python .\DeleteDuplicateInFolderA.py c:\temp\test\A c:\temp\test\B
</code></pre>
<h3>Output of dry run mode</h3>
<pre><code>Files in C:\temp\A
 1.txt
 2.txt
Files in C:\temp\B
 2.txt
 3.txt
These files would be deleted:
 C:\temp\A\2.txt
</code></pre>
<h3>Output of file deleting mode</h3>
<pre><code>Files in C:\temp\A
 1.txt
 2.txt
Files in C:\temp\B
 2.txt
 3.txt
Deleting files:
 C:\temp\A\2.txt
</code></pre>
<h3>Unit test</h3>
<p>If you want to test the application above, create a file named <code>DeleteDuplicateInFolderATest.py</code> and paste these unittests into it:</p>
<pre><code>import unittest
import os
import shutil
from DeleteDuplicateInFolderA import DeleteDuplicateInFolderA
class DeleteDuplicateInFolderATest(unittest.TestCase):
 def __init__(self, *args, **kwargs):
 super(DeleteDuplicateInFolderATest, self).__init__(*args, **kwargs)
 self._base_directory = r"c:\temp\test"
 self._path_A = self._base_directory + r"\A"
 self._path_B = self._base_directory + r"\B"
 def create_folder_and_create_some_files(self, path, filename_list):
 if os.path.exists(path):
 shutil.rmtree(path)
 os.makedirs(path)
 for filename in filename_list:
 open(os.path.join(path, filename), "w+").close()
 def setUp(self):
 # Create folders and files for testing
 self.create_folder_and_create_some_files(self._path_A, ["1.txt", "2.txt"])
 self.create_folder_and_create_some_files(self._path_B, ["2.txt", "3.txt"])
 def tearDown(self):
 for path in [self._path_A, self._path_B, self._base_directory]:
 if os.path.exists(path):
 shutil.rmtree(path)
 def test_duplicate_file_gets_deleted(self):
 # Arrange
 app = DeleteDuplicateInFolderA(self._path_A, self._path_B, is_dry_run=False)
 # Act
 app.delete_duplicates_in_folder_A()
 # Assert
 self.assertFalse(os.path.isfile(self._path_A + r"\2.txt"), "File 2.txt has not been deleted.")
 def test_duplicate_file_gets_not_deleted_in_mode_dryrun(self):
 # Arrange
 app = DeleteDuplicateInFolderA(self._path_A, self._path_B, is_dry_run=True)
 # Act
 app.delete_duplicates_in_folder_A()
 # Assert
 self.assertTrue(os.path.isfile(self._path_A + r"\2.txt"), "File 2.txt should not have been deleted in mode '--dryrun'")
def main():
 unittest.main()
if __name__ == '__main__':
 main()
</code></pre>
 </div>||||I|||| Stack Exchange Network

 Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

 Visit Stack Exchange
 Loading…
 1. 
 + Tour Start here for a quick overview of the site
 + Help Center Detailed answers to any questions you might have
 + Meta Discuss the workings and policies of this site
 + About Us Learn more about Stack Overflow the company
 + Business Learn more about our products
 2. 

 3. current community

 + Super User
 help chat
 + Meta Super User

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 4. 
 5. Log in
 6. Sign up

 Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.

 Sign up to join this community
 Anybody can ask a question
 Anybody can answer
 The best answers are voted up and rise to the top
 1. Home
 2. 
 1. Public
 2. Questions
 3. Tags
 4. Users
 5. Companies
 6. Unanswered
 3. 
 1. Teams
 Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
 2. Teams
 3. Create free Team

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 Is there a way to remove files from a folder that are in another folder?

 Ask Question
 Asked 6 years, 7 months ago
 Modified 8 months ago
 Viewed 28k times
 25

 Let's say I copy and paste files from folder A, which includes:

 Folder A:

 file1.cfg 
 file2.txt 
 file3.esp 
 file4.bsa 
 

 into folder B, which after updating, has:

 Folder B:

 apples.mp3 
 file1.cfg *
 file2.txt *
 file3.esp *
 file4.bsa *
 turtles.jpg
 

 Is there a way to delete all files from folder A that are in folder B (marked with a *)? Besides manually selecting each one and deleting it, or ctrl-Z'ing right after the copy-paste

 I would prefer either a windows method or some software that could do this

 thanks!

 * windows
 * windows-explorer
 Share
 Improve this question
 Follow
 edited Jun 26, 2016 at 18:12
 LPChip
 57.5k 9 9 gold badges 92 92 silver badges 135 135 bronze badges
 asked Jun 26, 2016 at 17:13
 DarkFire13 DarkFire13
 269 1 1 gold badge 3 3 silver badges 5 5 bronze badges
 4
 * 4
 How do you know they're the same files content-wise? I can't imagine a scenario where you would want to blindly consider a file to be a duplicate just based on file name alone.
 – rory.ap
 Jun 26, 2016 at 22:10
 * @roryap I think this question was arised because OP copied the files from folder 1 to folder 2, replaced all and now thinks, hmm, this was a mistake, but realizes that the next day, so undo is not possible. But you're right, contentwize you can't know.
 – LPChip
 Jun 27, 2016 at 7:36
 * 13
 Just a dumb question... Why not use "cut" and "paste"?
 – DaMachk
 Jun 27, 2016 at 10:13
 * 1
 @DaMachk if you're working with network drives or removable media, copy->verify->clean-up is a reasonable route. If the files are used by some process, it might be a good idea to test it on a copy (I do this with files for python data analysis in case of bugs in my own code clobbering the input file (for example). It may not be as necessary as it used to be, but old habits and all that. Alternatively the OP could have mis-clicked copy instead of cut,
 – Chris H
 Jun 28, 2016 at 12:27
 Add a comment |

 11 Answers

 Sorted by: Reset to default
 Highest score (default) Date modified (newest first) Date created (oldest first)
 37

 There's free software out there called WinMerge. You can use this software to match up duplicates. First, use File → Open, and choose both directories, with the folder with files you want to keep on the left, and those you do not on the right. Then, go to View, and deselect Show Different Items, Show Left Unique Items, and Show Right Unique Items. This will leave just the identical files left in the list. After that, choose Edit → Select All, right-click on any file, and click on Delete → Right. This will delete the duplicates from the right-hand folder.

 Share
 Improve this answer
 Follow
 edited Jun 27, 2016 at 20:05
 Lernkurve
 1,845 7 7 gold badges 26 26 silver badges 39 39 bronze badges
 answered Jun 26, 2016 at 23:22
 phyrfox phyrfox
 2,963 18 18 silver badges 16 16 bronze badges
 1
 * The benefit of this method is that it can detect if the files are not content-wise similar, if this is important. WinMerge can compare all factors that matters to one.
 – user477799
 Jun 27, 2016 at 19:59
 Add a comment |
 26

 This can be done through the commandline by using the command forfiles

 Lets assume you have Folder A located in c:\temp\Folder A, and Folder B located in c:\temp\Folder B

 The command would then be:

 c:\>forfiles /p "c:\temp\Folder A" /c "cmd /c del c:\temp\Folder B\@file"
 

 After this is done, Folder B will have all files removed that are present in Folder A. Keep in mind, that if folder B has files with the same name, but not the same content, they will still be deleted.

 It is possible to extend this to work with folders in subfolders too, but out of fear for this to become unnecessary complicated, I've decided against posting it. It would require the /s and @relpath options (and further testing xD)

 Share
 Improve this answer
 Follow
 answered Jun 26, 2016 at 18:18
 LPChip LPChip
 57.5k 9 9 gold badges 92 92 silver badges 135 135 bronze badges
 0
 Add a comment |
 12

 You can use this PowerShell script:

 $folderA = 'C:\Users\Ben\test\a\' # Folder to remove cross-folder duplicates from
 $folderB = 'C:\Users\Ben\test\b\' # Folder to keep the last remaining copies in
 Get-ChildItem $folderB | ForEach-Object {
 $pathInA = $folderA + $_.Name
 If (Test-Path $pathInA) {Remove-Item $pathInA}
 }
 

 Hopefully it's fairly self-explanatory. It looks at every item in Folder B, checks whether there's an item with the same name in Folder A, and if so, it removes the Folder A item. Note that the final \ in the folder paths is important.

 One-line version:

 gci 'C:\Users\Ben\test\b\' | % {del ('C:\Users\Ben\test\a\' + $_.Name) -EA 'SilentlyContinue'}
 

 If you don't care whether you get a deluge of red errors in the console, you can remove the -EA 'SilentlyContinue'.

 Save it as a .ps1 file, e.g. dedupe.ps1. Before you can run PowerShell scripts, you'll need to enable their execution:

 Set-ExecutionPolicy Unrestricted -Scope CurrentUser
 

 Then you'll be able to invoke it with .\dedupe.ps1 when you're in the folder that contains it.

 Share
 Improve this answer
 Follow
 edited Jun 26, 2016 at 23:55
 answered Jun 26, 2016 at 22:04
 Ben N Ben N
 39.4k 17 17 gold badges 137 137 silver badges 174 174 bronze badges
 Add a comment |
 6

 rsync

 rsync is a program used to synchronize directory. From the many (really many) options you have there are the self explaining --ignore-non-existing, --remove-source-files and --recursive.

 You can do

 rsync -avr --ignore-non-existing --recursive --remove-source-files B/ A -v
 

 if we suppose you have the files in the directory A (4) and B (4+2).

 A B
 ├── a ├── a
 ├── b ├── b
 ├── c ├── c
 └── d ├── d
 ├── e
 └── f # Before
 
 
 A B
 ├── a ├── e
 ├── b └── f
 ├── c 
 └── d # After
 
 Share
 Improve this answer
 Follow
 edited Jun 12, 2020 at 13:48
 Community Bot
 1
 answered Jun 27, 2016 at 12:17
 Hastur Hastur
 18.4k 8 8 gold badges 51 51 silver badges 94 94 bronze badges
 Add a comment |
 4

 LPChip's answer is the better one.

 But because I've started to learn Python, I thought, "Heck, why not write a Python script as answer to this question?"

 Install Python and Send2Trash

 You'd need to install Python before you can run the script from the command line.

 Then install Send2Trash so the deleted files aren't gone irretrievably but end up in the OS's trash:

 pip install Send2Trash
 

 Create script

 Create a new file with for instance the name DeleteDuplicateInFolderA.py

 Copy the following script into the file.

 #!/usr/bin/python
 
 import sys
 import os
 from send2trash import send2trash
 
 
 class DeleteDuplicateInFolderA(object):
 """Given two paths A and B, the application determines which files are in
 path A which are also in path B and then deletes the duplicates from
 path A.
 
 If the "dry run" flag is set to 'true', files are deleted. Otherwise
 they are only displayed but not deleted.
 """
 
 def __init__(self, path_A, path_B, is_dry_run=True):
 self._path_A = path_A
 self._path_B = path_B
 self._is_dry_run = is_dry_run
 
 def get_filenames_in_folder(self, folder_path):
 only_files = []
 for (dirpath, dirnames, filenames) in os.walk(folder_path):
 only_files.extend(filenames)
 return only_files
 
 def print_files(sel, heading, files):
 print(heading)
 if len(files) == 0:
 print(" none")
 else:
 for file in files:
 print(" {}".format(file))
 
 def delete_duplicates_in_folder_A(self):
 only_files_A = self.get_filenames_in_folder(self._path_A)
 only_files_B = self.get_filenames_in_folder(self._path_B)
 
 files_of_A_that_are_in_B = [file for file in only_files_A if file in only_files_B]
 
 self.print_files("Files in {}".format(self._path_A), only_files_A)
 self.print_files("Files in {}".format(self._path_B), only_files_B)
 
 if self._is_dry_run:
 self.print_files("These files would be deleted: ", [os.path.join(self._path_A, file) for file in files_of_A_that_are_in_B])
 else:
 print("Deleting files:")
 for filepath in [os.path.join(self._path_A, file) for file in files_of_A_that_are_in_B]:
 print(" {}".format(filepath))
 # os.remove(filepath) # Use this line instead of the next if Send2Trash is not installed
 send2trash(filepath)
 
 if __name__ == "__main__":
 if len(sys.argv) == 4:
 is_dry_run_argument = sys.argv[3]
 if not is_dry_run_argument == "--dryrun":
 println("The 3rd argument must be '--dryrun' or nothing.")
 else:
 app = DeleteDuplicateInFolderA(sys.argv[1], sys.argv[2], is_dry_run=True)
 else:
 app = DeleteDuplicateInFolderA(sys.argv[1], sys.argv[2], is_dry_run=False)
 app.delete_duplicates_in_folder_A()
 

 Usage

 Dry run mode, which shows you which files would be delete without actually deleting any files:

 c:\temp> python .\DeleteDuplicateInFolderA.py c:\temp\test\A c:\temp\test\B --dryrun
 

 File deleting mode, which indeed does delete files, so be careful:

 c:\temp> python .\DeleteDuplicateInFolderA.py c:\temp\test\A c:\temp\test\B
 

 Output of dry run mode

 Files in C:\temp\A
 1.txt
 2.txt
 Files in C:\temp\B
 2.txt
 3.txt
 These files would be deleted:
 C:\temp\A\2.txt
 

 Output of file deleting mode

 Files in C:\temp\A
 1.txt
 2.txt
 Files in C:\temp\B
 2.txt
 3.txt
 Deleting files:
 C:\temp\A\2.txt
 

 Unit test

 If you want to test the application above, create a file named DeleteDuplicateInFolderATest.py and paste these unittests into it:

 import unittest
 import os
 import shutil
 from DeleteDuplicateInFolderA import DeleteDuplicateInFolderA
 
 
 class DeleteDuplicateInFolderATest(unittest.TestCase):
 
 def __init__(self, *args, **kwargs):
 super(DeleteDuplicateInFolderATest, self).__init__(*args, **kwargs)
 self._base_directory = r"c:\temp\test"
 self._path_A = self._base_directory + r"\A"
 self._path_B = self._base_directory + r"\B"
 
 def create_folder_and_create_some_files(self, path, filename_list):
 if os.path.exists(path):
 shutil.rmtree(path)
 os.makedirs(path)
 for filename in filename_list:
 open(os.path.join(path, filename), "w+").close()
 
 def setUp(self):
 # Create folders and files for testing
 self.create_folder_and_create_some_files(self._path_A, ["1.txt", "2.txt"])
 self.create_folder_and_create_some_files(self._path_B, ["2.txt", "3.txt"])
 
 def tearDown(self):
 for path in [self._path_A, self._path_B, self._base_directory]:
 if os.path.exists(path):
 shutil.rmtree(path)
 
 def test_duplicate_file_gets_deleted(self):
 # Arrange
 app = DeleteDuplicateInFolderA(self._path_A, self._path_B, is_dry_run=False)
 
 # Act
 app.delete_duplicates_in_folder_A()
 
 # Assert
 self.assertFalse(os.path.isfile(self._path_A + r"\2.txt"), "File 2.txt has not been deleted.")
 
 def test_duplicate_file_gets_not_deleted_in_mode_dryrun(self):
 # Arrange
 app = DeleteDuplicateInFolderA(self._path_A, self._path_B, is_dry_run=True)
 
 # Act
 app.delete_duplicates_in_folder_A()
 
 # Assert
 self.assertTrue(os.path.isfile(self._path_A + r"\2.txt"), "File 2.txt should not have been deleted in mode '--dryrun'")
 
 def main():
 unittest.main()
 
 if __name__ == '__main__':
 main()
 
 Share
 Improve this answer
 Follow
 edited Mar 20, 2017 at 10:17
 Community Bot
 1
 answered Jun 26, 2016 at 18:56
 Lernkurve Lernkurve
 1,845 7 7 gold badges 26 26 silver badges 39 39 bronze badges
 8
 * Can you tell me why this script is "ugly as hell"? I just read through it and what you are doing is crystal clear. I'm almost tempted to paste it on CodeReview.SE to learn about what isn't preferred about it.
 – user1717828
 Jun 27, 2016 at 15:45
 * Adding a md5sum to check if the files contents are the same would be a nice option. Also using the OS trash mechanism instead of removing.
 – lolesque
 Jun 27, 2016 at 16:01
 * @user1717828: I've restructured the code, deleted that comment and took your suggestion to post the code on CodeReview.SE .
 – Lernkurve
 Jun 28, 2016 at 21:50
 * @lolesque: Send2Trash part: done. Thank you for the idea!
 – Lernkurve
 Jun 30, 2016 at 8:58
 * 1
 @barlop, I was replying to the original post, not a comment.
 – user1717828
 Jul 17, 2016 at 14:07
 | Show 3 more comments
 1

 Using bash

 for f in $(ls /path/to/folderB/); do 
 rm -rf /path/to/folderA/$f
 done
 

 Sure you could be more more safe by checking if the file is there, or checking if the filename is safe. But assuming you just want to get this done, and don't have any ridiculously named files in folderB - this is a quick and dirty way to get it done. (and you can use the bash emulator that comes with git, if you aren't running Win10 + bash)

 Share
 Improve this answer
 Follow
 answered Jun 27, 2016 at 17:22
 rm-vanda rm-vanda
 230 2 2 silver badges 13 13 bronze badges
 1
 * Maybe you need to add a check if you find directories...
 – Hastur
 Jul 12, 2019 at 13:20
 Add a comment |
 1

 Any NC-style program, like Total Commander, has a directory difference command that selects files in both tabs that are different from the other tab. Call this command, tab to the larger directory (B), invert selection using * and delete. This has the advantage of not deleting files which may have changed (somehow) and are not the same albeit they agree in name. You can use the same directory diff command to locate these after the deletion.

 I guess I'm stuck in the nineties... but I haven't really seen anything more elegant since :-) So far this is the only answer that requires as little as some 5 keystrokes and no scripting / command line whatsoever.

 Share
 Improve this answer
 Follow
 answered Jun 27, 2016 at 22:04
 The Vee The Vee
 180 9 9 bronze badges
 Add a comment |
 1

 Let's say I copy and paste files from folder A into folder B.

 Is there a way to delete all files from folder A that are in folder B? Besides manually selecting each one and deleting it, or ctrl-Z'ing right after the copy-paste

 Windows Method

 If you always need to copy files from one location into another and then afterwards ensure the files which were copied successfully are also deleted from the original source location, then below is a batch script solution that you can use to automate that entire task with just a simple click each run.

 * Be sure to set the SourceDir and the DestDir variables accordingly for your needs.

 * Additionally, in the part of the script below as ("%SourceDir%\*.*") DO you can simply change the *.* value to be more explicit for file names (File A.txt) or file extensions (*.wav) as needed.

 @ECHO ON
 SET SourceDir=C:\Users\User\Desktop\Source
 SET DestDir=C:\Users\User\Desktop\Dest
 
 FOR %%A IN ("%SourceDir%\*.*") DO XCOPY /F /Y "%%~A" "%DestDir%\" && DEL /Q /F "%%~A"
 GOTO EOF
 

 Further Resources

 * FOR LOOPING
 * XCOPY
 * DEL
 Share
 Improve this answer
 Follow
 edited Jun 28, 2016 at 0:12
 answered Jun 27, 2016 at 1:35
 Vomit IT - Chunky Mess Style Vomit IT - Chunky Mess Style
 38.8k 26 26 gold badges 77 77 silver badges 116 116 bronze badges
 Add a comment |
 0
 `

 1. Go to Origin Folder: pushd "D:\Dir_Origin"

 2. Use one simple for loop to list yours files: for %i in (*)do...

 3. Test if exist your file in Target folder: if exist "D:\Dir_Target\%~nxi"

 4. If last command return true, delete this current file in loop: del "%~fi"

 * In command-line:
 @(pushd "D:\Dir_Origin" && (for %i in (*)do if exist "D:\Dir_Target\%~nxi" del "%~fi")) & popd
 
 * In bat File:
 @(pushd "D:\Dir_Origin" && (for %%i in (*)do if exist "D:\Dir_Target\%%~nxi" del "%%~fi")) & popd
 
 * One option using dir and operator &&
 @(pushd "D:\Dir_Origin" && (for %i in (*)do 2>nul dir /b "D:\Dir_Target\%~nxi" && del "%~fi")) & popd
 

 * Some further reading:

 [√] Pushd

 [√] Popd

 [√] For Loop

 [√] For /F Loop

 [√] For /R Loop

 [√] Conditional Execution | Refer &&

 Share
 Improve this answer
 Follow
 answered Aug 2, 2020 at 2:27
 Io-oI Io-oI
 6,781 3 3 gold badges 12 12 silver badges 38 38 bronze badges
 Add a comment |
 0

 Method without any External Software and Terminal

 Just copy the files from folder B to folder A. It will ask for replacing the files which are duplicates. Replace all of them. By replacing, the last modified time of the duplicated (replaced) files changes. Now just sort them using the modified time. Delete the files which are modified currently. You will get the remaining ones.

 Share
 Improve this answer
 Follow
 answered Dec 10, 2021 at 9:42
 Akash Gupta Akash Gupta
 1
 Add a comment |
 0

 I've adapted Ben N's answer to make it dynamic with multiple folders on both sides (keep and delete).

 Keep in mind it will only check for file names, and not the difference between the actual files.

 # Pages that were helpful:
 # https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.2
 # https://superuser.com/a/1093799
 # https://stackoverflow.com/a/24315319
 
 # DON'T FORGET TO ADD A "\" AT THE END OF THE FILE PATH.
 # EXAMPLE: 'C:\Temp\'
 
 # Folders to remove cross-folder duplicates from
 $pathsToRemoveFilesFrom = @(
 'C:\Temp\Test1\'
 'C:\Temp\Test2\'
 )
 
 # Folders to keep the last remaining copies in
 $pathsToKeepThefilesFrom = @(
 'C:\ImportantFiles1\'
 'C:\ImportantFiles2\'
 'C:\ImportantFiles3\'
 'C:\ImportantFiles4\'
 )
 
 
 foreach($pathKeep in $pathsToKeepThefilesFrom) {
 Get-ChildItem $pathKeep -recurse | ForEach-Object {
 foreach($pathDelete in $pathsToRemoveFilesFrom){
 $fullPath = $pathDelete + $_.Name
 If (Test-Path $fullPath) {Remove-Item $fullPath}
 }
 }
 }
 
 Share
 Improve this answer
 Follow
 answered May 27, 2022 at 2:59
 Jean-Michel Morin Jean-Michel Morin
 1
 Add a comment |

 Your Answer

 Thanks for contributing an answer to Super User!

 * Please be sure to answer the question. Provide details and share your research!

 But avoid …

 * Asking for help, clarification, or responding to other answers.
 * Making statements based on opinion; back them up with references or personal experience.

 To learn more, see our tips on writing great answers.

 Sign up or log in

 Sign up using Google
 Sign up using Facebook
 Sign up using Email and Password
 Submit

 Post as a guest

 Name
 Email

 Required, but never shown

 Post as a guest

 Name
 Email

 Required, but never shown

 Post Your Answer Discard

 By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

 Not the answer you're looking for? Browse other questions tagged
 * windows
 * windows-explorer
 or ask your own question.

 * The Overflow Blog
 * Why developer experience is the key to better software, straight from the...
 * The frontier of computing: Q&A with IBM quantum computing research
 * Featured on Meta
 * Accessibility Update: Colors
 * Use of ChatGPT is now banned on Super User

 Linked

 2
 Remove files in Directory B that also exist in Directory A

 Related

 0
 Recover files that are still there
 2
 Windows Batch commands to copy file to multiple subdirectories
 1
 Find and remove invisible EFS*.TMP files that are created by NTFS encrypting file system
 4
 "Reverse incremental" backup for Windows?
 1
 Can't use Windows File Explorer
 2
 Execute 20,000 BAT files with one command
 0
 How to create batch file for copy command?

 Hot Network Questions

 * Both "fēmina" and "mulier" mean "woman": what's the difference?
 * Can poor grades in a bachelor's affect working in academia?
 * How to download only certain columns of a binary file using wget?
 * I need a seemingly large battery for project containing several sensors, what should I do?
 * He acts as if he owns (vs owned) the place?
 * What would be the next logical step after crossbows?
 * How to decide the sequence of cheeses for a cheese tasting event?
 * How many spells require expensive components for each casting?
 * Historic timezone offsets in newer JDK versions
 * Does any non-Catholic denomination maintain Altar Lamp, following Lev 6:12-13?
 * Can an aircraft use ram air to generate power while flying?
 * Powershell Script to write printer name
 * Will a spark gap transmit a signal an FM receiver can "hear?"
 * A standard way of marking alternate tunings for classical guitar?
 * How can an Indian homemaker spouse be guaranteed a Schengen tourist visa?
 * Did JK Rowling ever describe Harry Potter goblins as "hook-nosed"?
 * Does it matter if you don't preheat the oven for frozen French fries
 * A journal editor has pretended my referee report actually came from two separate referees
 * Psalm 7:9 and meaning of the word "reins" within the context
 * Why does faster cooling produce smaller grains?
 * Find the perimeter (seemingly unsolvable problem)
 * Other than looking cool, what is the reason behind the TSR-2's anhedral wingtips?
 * Why is the solstice the first day of summer, and not the central day?
 * Computers lose power, lights dim. I suspect the mains power supply; is there anything I can do?
 more hot questions
 Question feed

 Subscribe to RSS

 Question feed

 To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

 Super User

 * Tour
 * Help
 * Chat
 * Contact
 * Feedback

 Company

 * Stack Overflow
 * Teams
 * Advertising
 * Collectives
 * Talent
 * About
 * Press
 * Legal
 * Privacy Policy
 * Terms of Service
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.2.6.43222

 Your privacy

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

 Accept all cookies Customize settings
LPChip's answer is the better one.
But because I've started to learn Python, I thought, "Heck, why not write a Python script as answer to this question?"
Install Python and Send2Trash
You'd need to install Python before you can run the script from the command line.
Then install Send2Trash so the deleted files aren't gone irretrievably but end up in the OS's trash:
 pip install Send2Trash
 
Create script
Create a new file with for instance the name DeleteDuplicateInFolderA.py 
Copy the following script into the file.
 #!/usr/bin/python
import sys
import os
from send2trash import send2trash
class DeleteDuplicateInFolderA(object):
 """Given two paths A and B, the application determines which files are in
 path A which are also in path B and then deletes the duplicates from
 path A.
 If the "dry run" flag is set to 'true', files are deleted. Otherwise
 they are only displayed but not deleted.
 """
 def __init__(self, path_A, path_B, is_dry_run=True):
 self._path_A = path_A
 self._path_B = path_B
 self._is_dry_run = is_dry_run
 def get_filenames_in_folder(self, folder_path):
 only_files = []
 for (dirpath, dirnames, filenames) in os.walk(folder_path):
 only_files.extend(filenames)
 return only_files
 def print_files(sel, heading, files):
 print(heading)
 if len(files) == 0:
 print(" none")
 else:
 for file in files:
 print(" {}".format(file))
 def delete_duplicates_in_folder_A(self):
 only_files_A = self.get_filenames_in_folder(self._path_A)
 only_files_B = self.get_filenames_in_folder(self._path_B)
 files_of_A_that_are_in_B = [file for file in only_files_A if file in only_files_B]
 self.print_files("Files in {}".format(self._path_A), only_files_A)
 self.print_files("Files in {}".format(self._path_B), only_files_B)
 if self._is_dry_run:
 self.print_files("These files would be deleted: ", [os.path.join(self._path_A, file) for file in files_of_A_that_are_in_B])
 else:
 print("Deleting files:")
 for filepath in [os.path.join(self._path_A, file) for file in files_of_A_that_are_in_B]:
 print(" {}".format(filepath))
 # os.remove(filepath) # Use this line instead of the next if Send2Trash is not installed
 send2trash(filepath)
if __name__ == "__main__":
 if len(sys.argv) == 4:
 is_dry_run_argument = sys.argv[3]
 if not is_dry_run_argument == "--dryrun":
 println("The 3rd argument must be '--dryrun' or nothing.")
 else:
 app = DeleteDuplicateInFolderA(sys.argv[1], sys.argv[2], is_dry_run=True)
 else:
 app = DeleteDuplicateInFolderA(sys.argv[1], sys.argv[2], is_dry_run=False)
 app.delete_duplicates_in_folder_A()
 
Usage
Dry run mode, which shows you which files would be delete without actually deleting any files:
 c:\temp&gt; python .\DeleteDuplicateInFolderA.py c:\temp\test\A c:\temp\test\B --dryrun
 
File deleting mode, which indeed does delete files, so be careful:
 c:\temp&gt; python .\DeleteDuplicateInFolderA.py c:\temp\test\A c:\temp\test\B
 
Output of dry run mode
 Files in C:\temp\A
 1.txt
 2.txt
Files in C:\temp\B
 2.txt
 3.txt
These files would be deleted:
 C:\temp\A\2.txt
 
Output of file deleting mode
 Files in C:\temp\A
 1.txt
 2.txt
Files in C:\temp\B
 2.txt
 3.txt
Deleting files:
 C:\temp\A\2.txt
 
Unit test
If you want to test the application above, create a file named DeleteDuplicateInFolderATest.py and paste these unittests into it:
 import unittest
import os
import shutil
from DeleteDuplicateInFolderA import DeleteDuplicateInFolderA
class DeleteDuplicateInFolderATest(unittest.TestCase):
 def __init__(self, *args, **kwargs):
 super(DeleteDuplicateInFolderATest, self).__init__(*args, **kwargs)
 self._base_directory = r"c:\temp\test"
 self._path_A = self._base_directory + r"\A"
 self._path_B = self._base_directory + r"\B"
 def create_folder_and_create_some_files(self, path, filename_list):
 if os.path.exists(path):
 shutil.rmtree(path)
 os.makedirs(path)
 for filename in filename_list:
 open(os.path.join(path, filename), "w+").close()
 def setUp(self):
 # Create folders and files for testing
 self.create_folder_and_create_some_files(self._path_A, ["1.txt", "2.txt"])
 self.create_folder_and_create_some_files(self._path_B, ["2.txt", "3.txt"])
 def tearDown(self):
 for path in [self._path_A, self._path_B, self._base_directory]:
 if os.path.exists(path):
 shutil.rmtree(path)
 def test_duplicate_file_gets_deleted(self):
 # Arrange
 app = DeleteDuplicateInFolderA(self._path_A, self._path_B, is_dry_run=False)
 # Act
 app.delete_duplicates_in_folder_A()
 # Assert
 self.assertFalse(os.path.isfile(self._path_A + r"\2.txt"), "File 2.txt has not been deleted.")
 def test_duplicate_file_gets_not_deleted_in_mode_dryrun(self):
 # Arrange
 app = DeleteDuplicateInFolderA(self._path_A, self._path_B, is_dry_run=True)
 # Act
 app.delete_duplicates_in_folder_A()
 # Assert
 self.assertTrue(os.path.isfile(self._path_A + r"\2.txt"), "File 2.txt should not have been deleted in mode '--dryrun'")
def main():
 unittest.main()
if __name__ == '__main__':
 main()
Is there a way to remove files from a folder that are in another folder?
https://superuser.com/questions/1093727/is-there-a-way-to-remove-files-from-a-folder-that-are-in-another-folder
https://linuxize.com/post/how-to-remove-files-and-directories-using-linux-command-line/
How to Remove Files and Directories Using Linux Command Line
2018-10-01
Linuxize
<div><section><figure><div><p></p></div></figure><div><p>This tutorial, will show you how to use the <code>rm</code>, <code>unlink</code>, and <code>rmdir</code> commands to remove files and directories in Linux.</p><p>To remove (or delete) a file in Linux from the command line, use either the <code>rm</code> (remove) or <code>unlink</code> command.</p><p>The <code>unlink</code> command allows you to remove only a single file, while with <code>rm</code>, you can remove multiple files at once.</p><p>Be extra careful when removing files or directories, because once the file is deleted it cannot be easily recovered.</p><ul><li><p>To delete a single file, use the <a href="https://linuxize.com/post/rm-command-in-linux/"><code>rm</code></a>
or <a href="https://linuxize.com/post/unlink-command-in-linux/"><code>unlink</code></a>
command followed by the file name:</p><pre><code>unlink filename</code></pre><pre><code>rm filename</code></pre><p>If the file is write-protected, you will be prompted for confirmation, as shown below. To remove the file type <code>y</code>, and hit <code>Enter</code>. Otherwise, if the file is not write-protected, it will be deleted without prompting.</p><pre><code> 
rm: remove write-protected regular empty file 'filename'?</code></pre></li><li><p>To delete multiple files at once, use the <code>rm</code> command followed by the file names separated by space.</p><pre><code>rm filename1 filename2 filename3</code></pre><p>You can also use a wildcard (<code>*</code>) and regular expansions to match multiple files. For example, to remove all <code>.pdf</code> files in the current directory, use the following command:</p><pre><code>rm *.pdf</code></pre><p>When using regular expansions, first list the files with the <a href="https://linuxize.com/post/how-to-list-files-in-linux-using-the-ls-command/"><code>ls</code></a>
command so that you can see what files will be deleted before running the <code>rm</code> command.</p></li><li><p>Use the <code>rm</code> with the <code>-i</code> option to confirm each file before deleting it:</p><pre><code>rm -i filename(s)</code></pre></li><li><p>To remove files without prompting, even if the files are write-protected, pass the <code>-f</code> (force) option to the <code>rm</code> command:</p><pre><code>rm -f filename(s)</code></pre></li><li><p>You can also combine <code>rm</code> options. For example, to remove all <code>.txt</code> files in the current directory without a prompt in verbose mode, use the following command:</p><pre><code>rm -fv *.txt</code></pre></li></ul><h2>How to Remove Directories (Folders) </h2><p>In Linux, you can <a href="https://linuxize.com/post/remove-directory-linux/">remove/delete directories</a>
with the <code>rmdir</code> and <code>rm</code>.</p><p><code>rmdir</code> is a command-line utility for deleting empty directories, while with <code>rm</code> you can remove directories and their contents recursively.</p><ul><li><p>To remove an empty directory, use either <code>rmdir</code> or <code>rm -d</code> followed by the directory name:</p><pre><code>rm -d dirname</code></pre><pre><code>rmdir dirname</code></pre></li><li><p>To remove non-empty directories and all the files within them, use the <code>rm</code> command with the<code>-r</code> (recursive) option:</p><pre><code>rm -r dirname</code></pre><p>If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.</p></li><li><p>To remove non-empty directories and all the files without being prompted, use <code>rm</code> with the <code>-r</code> (recursive) and <code>-f</code> options:</p><pre><code>rm -rf dirname</code></pre></li><li><p>To remove multiple directories at once, use the <code>rm -r</code> command followed by the directory names separated by space.</p><pre><code>rm -r dirname1 dirname2 dirname3</code></pre><p>Same as with files, you can also use a wildcard (<code>*</code>) and regular expansions to match multiple directories.</p></li></ul><h2>Conclusion </h2><p>By now you should have a good understanding of how to use the Linux <code>rm</code>, <code>rmdir</code> and <code>unlink</code> commands and you should be able to safely remove files and directories from the command line.</p><p>Feel free to leave a comment if you have any questions.</p></div></section>||||I|||| Linuxize
 Ubuntu Centos Debian Commands Series Donate

 How to Remove Files and Directories Using Linux Command Line

 Updated Aug 10, 2021

 •

 3 min read

 Contents

 * How to Remove Files
 * How to Remove Directories (Folders)
 * Conclusion

 Share:

 This tutorial, will show you how to use the rm, unlink, and rmdir commands to remove files and directories in Linux.

 How to Remove Files #

 To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command.

 The unlink command allows you to remove only a single file, while with rm, you can remove multiple files at once.

 Be extra careful when removing files or directories, because once the file is deleted it cannot be easily recovered.

 * To delete a single file, use the rm or unlink command followed by the file name:

 unlink filename
 rm filename

 If the file is write-protected, you will be prompted for confirmation, as shown below. To remove the file type y, and hit Enter. Otherwise, if the file is not write-protected, it will be deleted without prompting.

 
 rm: remove write-protected regular empty file 'filename'?

 * To delete multiple files at once, use the rm command followed by the file names separated by space.

 rm filename1 filename2 filename3

 You can also use a wildcard (*) and regular expansions to match multiple files. For example, to remove all .pdf files in the current directory, use the following command:

 rm *.pdf

 When using regular expansions, first list the files with the ls command so that you can see what files will be deleted before running the rm command.

 * Use the rm with the -i option to confirm each file before deleting it:

 rm -i filename(s)

 * To remove files without prompting, even if the files are write-protected, pass the -f (force) option to the rm command:

 rm -f filename(s)

 * You can also combine rm options. For example, to remove all .txt files in the current directory without a prompt in verbose mode, use the following command:

 rm -fv *.txt

 How to Remove Directories (Folders) #

 In Linux, you can remove/delete directories with the rmdir and rm.

 rmdir is a command-line utility for deleting empty directories, while with rm you can remove directories and their contents recursively.

 * To remove an empty directory, use either rmdir or rm -d followed by the directory name:

 rm -d dirname
 rmdir dirname

 * To remove non-empty directories and all the files within them, use the rm command with the-r (recursive) option:

 rm -r dirname

 If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.

 * To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:

 rm -rf dirname

 * To remove multiple directories at once, use the rm -r command followed by the directory names separated by space.

 rm -r dirname1 dirname2 dirname3

 Same as with files, you can also use a wildcard (*) and regular expansions to match multiple directories.

 Conclusion #

 By now you should have a good understanding of how to use the Linux rm, rmdir and unlink commands and you should be able to safely remove files and directories from the command line.

 Feel free to leave a comment if you have any questions.

 rm rmdir unlink terminal
 report this ad

 Related Tutorials

 * How to Remove (Delete) Symbolic Links in Linux
 * How to Remove (Delete) Directory in Linux
 * Rm Command in Linux
 * Unlink Command in Linux (Remove File)
 * Linux File Command
 * How to Create Bash Aliases
 * How to Rename Files and Directories in Linux
 report this ad

 If you like our content, please consider buying us a coffee.
 Thank you for your support!

 Buy me a coffee

 Sign up to our newsletter and get our latest tutorials and news straight to your mailbox.

 Subscribe

 We’ll never share your email address or spam you.

 Related Articles

 Sep 5, 2019

 How to Remove (Delete) Symbolic Links in Linux

 Aug 6, 2019

 How to Remove (Delete) Directory in Linux

 Sep 1, 2019

 Rm Command in Linux

 Write a comment
 Please enable JavaScript to view the comments powered by Disqus.
 ESC
 © 2022 Linuxize.com Privacy Policy Terms Contact Advertise on Linuxize
This tutorial, will show you how to use the rm, unlink, and rmdir commands to remove files and directories in Linux. To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command. The unlink command allows you to remove only a single file, while with rm, you can remove multiple files at once. Be extra careful when removing files or directories, because once the file is deleted it cannot be easily recovered.   To delete a single file, use the  rm 
or  unlink 
command followed by the file name:  unlink filename   rm filename  If the file is write-protected, you will be prompted for confirmation, as shown below. To remove the file type y, and hit Enter. Otherwise, if the file is not write-protected, it will be deleted without prompting.   
rm: remove write-protected regular empty file 'filename'?    To delete multiple files at once, use the rm command followed by the file names separated by space.  rm filename1 filename2 filename3  You can also use a wildcard (*) and regular expansions to match multiple files. For example, to remove all .pdf files in the current directory, use the following command:  rm *.pdf  When using regular expansions, first list the files with the  ls 
command so that you can see what files will be deleted before running the rm command.   Use the rm with the -i option to confirm each file before deleting it:  rm -i filename(s)    To remove files without prompting, even if the files are write-protected, pass the -f (force) option to the rm command:  rm -f filename(s)    You can also combine rm options. For example, to remove all .txt files in the current directory without a prompt in verbose mode, use the following command:  rm -fv *.txt    How to Remove Directories (Folders)  In Linux, you can remove/delete directories
with the rmdir and rm.  rmdir is a command-line utility for deleting empty directories, while with rm you can remove directories and their contents recursively.   To remove an empty directory, use either rmdir or rm -d followed by the directory name:  rm -d dirname   rmdir dirname    To remove non-empty directories and all the files within them, use the rm command with the-r (recursive) option:  rm -r dirname  If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.   To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:  rm -rf dirname    To remove multiple directories at once, use the rm -r command followed by the directory names separated by space.  rm -r dirname1 dirname2 dirname3  Same as with files, you can also use a wildcard (*) and regular expansions to match multiple directories.   Conclusion  By now you should have a good understanding of how to use the Linux rm, rmdir and unlink commands and you should be able to safely remove files and directories from the command line. Feel free to leave a comment if you have any questions.
How to Remove Files and Directories Using Linux Command Line
https://linuxize.com/post/how-to-remove-files-and-directories-using-linux-command-line/
https://stackoverflow.com/questions/20638100/how-do-i-delete-a-file-in-terminal
How do I delete a file in terminal?
2013-12-17
User
<div><div>
 
<div>
 
 
<div>
 
 <div>
 
<p>Sorry I am a newbie in Terminal.</p>
<p>When creating a PHP file in the terminal on Ubuntu using the following code:</p>
<pre><code>sudo nano /var/www/filename.php
</code></pre>
<p>I successfully create a PHP file. However I have no idea how to delete it. Any suggestions?</p>
 </div>
 
 <div>
 
 <div>
 
 <div>
 <a href="/users/1883647/ajp15243"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1883647/ajp15243">ajp15243</a></p><p>7,5641 gold badge32 silver badges38 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 asked Dec 17, 2013 at 15:29
 </p>
 <div>
 <a href="/users/2707299/user2707299"><p></p></a>
 </div>
 
</div>
 </div>
 
</div>
 <p>1</p>
 </div>
 <div>
 
 
<div>
 
 
<div>
 
 <div>
<p>Use:</p>
<pre><code>sudo rm /var/www/filename.php
</code></pre>
<p><code>rm</code> stands for "remove files or directories".</p>
 </div>
 <div>
 <p>
 answered Dec 17, 2013 at 15:29
 </p>
 <div>
 <a href="/users/1983854/fedorqui"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1983854/fedorqui">fedorqui</a>fedorqui</p><p>266k101 gold badges536 silver badges590 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>1</p>
 </div>
 <h2>
 
 </h2>
 </div>
 </div>||||I|||| Stack Overflow
 1. About
 2. Products
 3. For Teams
 1. Stack Overflow Public questions & answers
 2. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
 3. Talent Build your employer brand
 4. Advertising Reach developers & technologists worldwide
 5. About the company
 Loading…

 1. current community

 + Stack Overflow
 help chat
 + Meta Stack Overflow

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 2. 
 3. Log in
 4. Sign up
 1. Home
 2. 
 1. Public
 2. Questions
 3. Tags
 4. Users
 5. Companies
 6. Collectives
 7. Explore Collectives
 3. 
 1. Teams
 Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
 2. Teams
 3. Create free Team

 Collectives™ on Stack Overflow

 Find centralized, trusted content and collaborate around the technologies you use most.

 Learn more about Collectives

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 How do I delete a file in terminal? [closed]

 Ask Question
 Asked 9 years, 1 month ago
 Modified 9 years, 1 month ago
 Viewed 23k times
 6
 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

 Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist

 Closed 9 years ago .

 Improve this question

 Sorry I am a newbie in Terminal.

 When creating a PHP file in the terminal on Ubuntu using the following code:

 sudo nano /var/www/filename.php
 

 I successfully create a PHP file. However I have no idea how to delete it. Any suggestions?

 * ubuntu
 * terminal
 Share
 Follow
 edited Dec 17, 2013 at 15:33
 ajp15243
 7,564 1 1 gold badge 32 32 silver badges 38 38 bronze badges
 asked Dec 17, 2013 at 15:29
 user2707299 user2707299
 241 1 1 gold badge 2 2 silver badges 9 9 bronze badges
 1
 * 1
 You really couldn't find an answer to this by searching the web?
 – Chris Seymour
 Dec 17, 2013 at 15:36
 Add a comment |

 1 Answer

 Sorted by: Reset to default
 Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
 20

 Use:

 sudo rm /var/www/filename.php
 

 rm stands for "remove files or directories".

 Share
 Follow
 answered Dec 17, 2013 at 15:29
 fedorqui fedorqui
 266k 101 101 gold badges 536 536 silver badges 590 590 bronze badges
 1
 * 1
 you can use "unlink" as well in place of "rm"
 – Akhilesh Sinha
 Jan 4, 2019 at 12:26
 Add a comment |

 Not the answer you're looking for? Browse other questions tagged
 * ubuntu
 * terminal
 or ask your own question.

 * The Overflow Blog
 * Announcing more ways to learn and grow your skills
 * Why developer experience is the key to better software, straight from the...
 * Featured on Meta
 * Accessibility Update: Colors
 * 2022: a year in moderation
 * Collectives: The next iteration
 * We’re bringing advertisements for technology courses to Stack Overflow
 * Temporary policy: ChatGPT is banned
 Visit chat

 Related

 894
 How to move the cursor word by word in the OS X Terminal
 5903
 How do I execute a program or call a system command?
 2993
 How do I print colored text to the terminal?
 2090
 How to reload .bashrc settings without logging out and back in again?
 1105
 How do I clear/delete the current line in terminal?
 0
 Php webpage can not find bash file
 701
 Run / Open VSCode from Mac Terminal

 Hot Network Questions

 * 2 tabulars with supposed same size aren't actually the same size
 * What changes to Voyager could have been made?
 * Number of representations of a semisimple Lie algebra of any given dimension
 * Smallest Bit Rotation
 * In an underdetermined linear regression setup, why does the residual sum of squares equal 0?
 * Why does the Heisenberg uncertainty principle apply to particles?
 * Is there a canonical name for a polynomial-like expression allowing for negative powers?
 * Polynomial approximation of max function
 * Why did Harry and the others think they could rescue Sirius?
 * How can one order cheeses for a cheese tasting event?
 * How to find the safest place to cross a frozen river from a map?
 * EAS Build iOS Failure: Authentication with Apple Developer Portal failed
 * Ada and its candidate languages
 * Can a vampire rest in a coffin in a bag of holding?
 * Non-inverting op-amp
 * I forgot to include $10 in interest in my tax return. My tax liability did not change. Should I file an amended return?
 * Meaning of "nude" in Saxo Grammaticus
 * Can a creature be targeted by future uses of eyebite if they are immune to the initial casting?
 * Books containing new results
 * What does it mean for two properties of objects to be of the same class (or even identical)?
 * Can an aircraft use ram air to generate power while flying?
 * Do famous people really have the power to ruin the career of unrelated researchers?
 * Two questions about the 4th verse of "The Star-Spangled Banner"
 * How are local varibales stored on Atmega
 more hot questions

 Stack Overflow

 * Questions
 * Help

 Products

 * Teams
 * Advertising
 * Collectives
 * Talent

 Company

 * About
 * Press
 * Work Here
 * Legal
 * Privacy Policy
 * Terms of Service
 * Contact Us
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.2.3.43216

 Your privacy

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

 Accept all cookies Customize settings
Sorry I am a newbie in Terminal.
When creating a PHP file in the terminal on Ubuntu using the following code:
 sudo nano /var/www/filename.php
 
I successfully create a PHP file. However I have no idea how to delete it. Any suggestions?
 
 
 
 
 
 
 
    
 
 
  ajp15243  7,5641 gold badge32 silver badges38 bronze badges
 
 
 
 
 asked Dec 17, 2013 at 15:29
 
 
    
 
 
 
 
 1
 
 
 
 
 
 
 
 
Use:
 sudo rm /var/www/filename.php
 
 rm stands for "remove files or directories".
 
 
 
 answered Dec 17, 2013 at 15:29
 
 
    
 
 
  fedorquifedorqui 266k101 gold badges536 silver badges590 bronze badges
 
 
 
 1
How do I delete a file in terminal?
https://stackoverflow.com/questions/20638100/how-do-i-delete-a-file-in-terminal
https://stackoverflow.com/questions/1125476/retrieve-a-single-file-from-a-repository
Retrieve a single file from a repository
2019-11-14
Theozaurus Theozaurus
<div><div>
<p>This is specific for git repos hosted on <strong>GitHub</strong></p>
<p>Try the <em>'api'</em> command of Github's command line app, <code>gh</code>, to make an authenticated call to Github's <em>'get repository contents'</em> endpoint.</p>
<p>The basic command is:</p>
<pre><code>$gh api /repos/{owner}/{repo}/contents/&lt;path_to_the_file&gt;
</code></pre>
<p>As an added bonus, when you do this from inside a directory that contains a clone of the repo you're trying to get the file from, the {owner} and {repo} part will be automatically filled in.</p>
<p><a href="https://docs.github.com/en/rest/reference/repos#get-repository-content">https://docs.github.com/en/rest/reference/repos#get-repository-content</a></p>
<p>The response will be a JSON object. If the &lt;path_to_the_file&gt; indeed points to a file, the JSON will include a 'size', 'name', several url fields to access the file, as well as a 'content' field, which is a base64 encoded version of the file contents.</p>
<p>To get the file contents, you can curl the value of the "download_url", or just decode the 'content' field. You can do that by piping the base64 command, like this:</p>
<pre><code>$gh api /repos/{owner}/{repo}/contents/&lt;path-to-the-file&gt; --jq '.content' | base64 -d
</code></pre>
 </div>||||I|||| Stack Overflow
 1. About
 2. Products
 3. For Teams
 1. Stack Overflow Public questions & answers
 2. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
 3. Talent Build your employer brand
 4. Advertising Reach developers & technologists worldwide
 5. About the company
 Loading…

 1. current community

 + Stack Overflow
 help chat
 + Meta Stack Overflow

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 2. 
 3. Log in
 4. Sign up
 1. Home
 2. 
 1. Public
 2. Questions
 3. Tags
 4. Users
 5. Companies
 6. Collectives
 7. Explore Collectives
 3. 
 1. Teams
 Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
 2. Teams
 3. Create free Team

 Collectives™ on Stack Overflow

 Find centralized, trusted content and collaborate around the technologies you use most.

 Learn more about Collectives

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 Retrieve a single file from a repository

 Ask Question
 Asked 13 years, 6 months ago
 Modified 7 months ago
 Viewed 310k times
 275

 What is the most efficient mechanism (in respect to data transferred and disk space used) to get the contents of a single file from a remote git repository?

 So far I've managed to come up with:

 git clone --no-checkout --depth 1 git@github.com:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
 

 This still seems overkill.

 What about getting multiple files from the repo?

 * git
 * git-checkout
 * sparse-checkout
 * git-sparse-checkout
 Share
 Improve this question
 Follow
 edited Nov 14, 2019 at 17:20
 dreftymac
 30.8k 26 26 gold badges 117 117 silver badges 180 180 bronze badges
 asked Jul 14, 2009 at 13:42
 Theozaurus Theozaurus
 3
 * 3
 Aaw. I would love it if there was a built in way to do the equivalent of "cat-remote" and "tag-remote".
 – conny
 Jul 7, 2010 at 11:27
 * 3
 I have this same problem I want to have the same license file in 2 repos; edit the file in 1 repo then have it auto update the copy in the other repo.
 – GlassGhost
 Jan 26, 2011 at 11:04
 * Possible duplicate of How to checkout only one file from git repository?
 – Ciro Santilli OurBigBook.com
 Mar 30, 2016 at 10:48
 Add a comment |

 24 Answers

 Sorted by: Reset to default
 Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
 172

 In git version 1.7.9.5 this seems to work to export a single file from a remote

 git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar xO
 

 This will cat the contents of the file README.md.

 Share
 Improve this answer
 Follow
 edited Jun 15, 2022 at 2:20
 x-yuri
 15.2k 13 13 gold badges 109 109 silver badges 153 153 bronze badges
 answered Aug 20, 2013 at 9:23
 Yisrael Dov Yisrael Dov
 2,301 1 1 gold badge 16 16 silver badges 12 12 bronze badges
 15
 * 38
 ... Except it doesn't work on GitHub. Dang. :( twitter.com/GitHubHelp/status/322818593748303873
 – Rob Howard
 Sep 26, 2013 at 14:20
 * 17
 This doesn't seem to yield the raw file but rather a tar file with just a single file.
 – Frerich Raabe
 Mar 10, 2014 at 19:58
 * 22
 @FrerichRaabe just add ` | tar -x` to the command. git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -x cat README.md
 – renier
 May 21, 2015 at 18:46
 * 14
 You can use tar -xO to output to STDOUT for piping, e.g. FILE=README.md && git archive --remote=ssh://host/pathto/repo.git HEAD "$FILE" | tar -xO "$FILE"
 – paulcm
 Aug 25, 2015 at 11:56
 * 3
 Exactly the answer I was looking for, but I get "fatal: Operation not supported by protocol." in response from Git. Argh.
 – mhvelplund
 Feb 18, 2016 at 8:06
 | Show 10 more comments
 82

 Following on from Jakub's answer. git archive produces a tar or zip archive, so you need to pipe the output through tar to get the file content:

 git archive --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -x
 

 Will save a copy of 'filename' from the HEAD of the remote repository in the current directory.

 The :path/to/directory part is optional. If excluded, the fetched file will be saved to <current working dir>/path/to/directory/filename

 In addition, if you want to enable use of git archive --remote on Git repositories hosted by git-daemon, you need to enable the daemon.uploadarch config option. See https://kernel.org/pub/software/scm/git/docs/git-daemon.html

 Share
 Improve this answer
 Follow
 edited Dec 13, 2017 at 16:26
 DavidRR
 17.6k 24 24 gold badges 106 106 silver badges 185 185 bronze badges
 answered Mar 16, 2011 at 11:20
 Robert Knight Robert Knight
 2,828 26 26 silver badges 21 21 bronze badges
 4
 * 3
 If it is a text file and we want to save it to another part it is gut to use: | tar -xO > ~/destfile.ext
 – yucer
 Jul 15, 2015 at 10:52
 * does it work with specific commit? (i.e. one specifies both specific file and commit)
 – Alleo
 Nov 11, 2021 at 23:26
 * 1
 Yes. Replace HEAD with the commit ID that you want to use. HEAD is an alias that refers to either the currently checked out commit (if applicable) or the tip of the default branch. I wrote the above answer years ago and learned this morning that GitHub doesn't support git archive , so that makes it a lot less useful.
 – Robert Knight
 Nov 13, 2021 at 6:50
 * Looks like the best answer to me. Add a v as another option to tar -x doesn't hut. Also it may be good to note that it works also for a specific folder , not only a single file : git archive --remote=git://git.foo.com/project.git HEAD path/to/folder/ | tar -xv
 – M-Jack
 May 11, 2022 at 14:15
 Add a comment |
 47

 If there is web interface deployed (like gitweb, cgit, Gitorious, ginatra), you can use it to download single file ('raw' or 'plain' view).

 If other side enabled it, you can use git archive's '--remote=<URL>' option (and possibly limit it to a directory given file resides in), for example:

 $ git archive --remote=git@github.com:foo/bar.git --prefix=path/to/ HEAD:path/to/ | tar xvf -
 
 Share
 Improve this answer
 Follow
 answered Jul 14, 2009 at 15:50
 Jakub Narębski Jakub Narębski
 301k 65 65 gold badges 219 219 silver badges 230 230 bronze badges
 7
 * Note: the example was not tested!
 – Jakub Narębski
 Jul 14, 2009 at 15:51
 * 7
 For your own repositories you need to specifically enable upload-archive if using git-daemon (git:// style urls) with git config daemon.uploadarch true on the remote repository. By default git daemon disables remote archive with "fatal: remote error: access denied or repository not exported: ..."
 – patthoyts
 Feb 7, 2013 at 9:34
 * +1 The git archive approach was my first try - but then I noticed that requiring tar on the client machine wasn't exactly convenient for Windows users. We ended up fetching from our local cgit server. It works, but it's not as fast as I'd like it to be (and it still requires running unix2dos or similiar on Windows machines since we store files with Unix line endings in the Git repository).
 – Frerich Raabe
 Mar 10, 2014 at 19:47
 * 1
 @FrerichRaabe Use -o fetched.zip. Also see --format=<fmt> option.
 – akhan
 May 6, 2014 at 19:47
 * 5
 For what it's worth, it doesn't look like this works on GitHub hosted repositories. See help.github.com/articles/can-i-archive-a-repository and groups.google.com/forum/#!topic/github/z8vLHcX0HxY
 – vmrob
 Aug 20, 2014 at 22:19
 | Show 2 more comments
 42

 Not in general but if you are using Github:

 For me wget to the raw url turned out to be the best and easiest way to download one particular file.

 Open the file in the browser and click on "Raw" button. Now refresh your browser, copy the url and do a wget or curl on it.

 wget example:

 wget 'https://github.abc.abc.com/raw/abc/folder1/master/folder2/myfile.py?token=DDDDnkl92Kw8829jhXXoxBaVJIYW-h7zks5Vy9I-wA%3D%3D' -O myfile.py
 

 Curl example:

 curl 'https://example.com/raw.txt' > savedFile.txt
 
 Share
 Improve this answer
 Follow
 edited Jan 2, 2021 at 21:01
 Kermit
 4,459 4 4 gold badges 39 39 silver badges 69 69 bronze badges
 answered Aug 5, 2015 at 23:15
 Ankur Agarwal Ankur Agarwal
 23k 40 40 gold badges 133 133 silver badges 204 204 bronze badges
 6
 * 5
 This is the easiest solution, and works for any raw txt one could find. curl https://example.com/raw.txt > savedFile.txt
 – JacobPariseau
 Jan 5, 2017 at 23:06
 * wget example doesn't work, curl example does though.
 – Kyle Baker
 Feb 9, 2017 at 21:56
 * Works just fine for me. Did you put your url in quotes on the commandline ?
 – Ankur Agarwal
 Feb 10, 2017 at 21:24
 * this does not preserve git history
 – crypdick
 Jun 30, 2018 at 2:34
 * 1
 The solution is asked for Git, the answer endorses Github is git and is nowheere related to git. It's based on additional APIs offered by a prominent git solution provider!
 – Ravinder Payal
 Mar 22, 2021 at 18:24
 | Show 1 more comment
 17

 To export a single file from a remote:

 git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -x
 

 This will download the file README.md to your current directory.

 If you want the contents of the file exported to STDOUT:

 git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -xO
 

 You can provide multiple paths at the end of the command.

 Share
 Improve this answer
 Follow
 answered Sep 10, 2015 at 21:45
 Kousha Kousha
 1,565 14 14 silver badges 18 18 bronze badges
 Add a comment |
 8

 It looks like a solution to me: http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html

 git show HEAD~4:index.html > local_file
 

 where 4 means four revision from now and ~ is a tilde as mentioned in the comment.

 Share
 Improve this answer
 Follow
 edited May 27, 2020 at 3:08
 Evan Carroll
 75.9k 45 45 gold badges 251 251 silver badges 443 443 bronze badges
 answered Jan 2, 2012 at 14:48
 Michal Stefanow Michal Stefanow
 12.4k 11 11 gold badges 67 67 silver badges 89 89 bronze badges
 6
 * Make sure to notice that is it NOT the 'minus sign' '-' between 'HEAD' and '4', but the 'tilde' '~'. Apparently I haven't read the git docs well enough, or my glasses need updating ;-)
 – Dennis
 Jul 4, 2012 at 16:04
 * 23
 This doesn't seem to get the file from a remote repository though, like the OP needs.
 – Mike Weller
 Jan 18, 2013 at 13:11
 * Or: git show HEAD:./my_other_file > local_file if the file isn't in your root dir:)
 – kenorb
 Jun 21, 2013 at 11:52
 * 1
 Kind request for all downvoters - please explain and clarify what's not OK - we are here to learn and share :)
 – Michal Stefanow
 Jul 26, 2013 at 10:05
 * 10
 @MichalStefanow: Mike Weller has it; specifically, this doesn't work on a remote repository. You need a local clone at the very least, even if you then have remotes set up on it.
 – Rob Howard
 Sep 26, 2013 at 14:08
 | Show 1 more comment
 7

 I solved in this way:

 git archive --remote=ssh://git@gitlab.com/user/mi-repo.git BranchName /path-to-file/file_name | tar -xO /path-to-file/file_name > /path-to-save-the-file/file_name
 

 If you want, you could replace "BranchName" for "HEAD"

 Share
 Improve this answer
 Follow
 edited Sep 22, 2018 at 18:24
 answered Sep 22, 2018 at 18:14
 matiasmasca matiasmasca
 587 7 7 silver badges 14 14 bronze badges
 Add a comment |
 6

 I use this

 $ cat ~/.wgetrc
 check_certificate = off
 
 $ wget https://raw.github.com/jquery/jquery/master/grunt.js
 HTTP request sent, awaiting response... 200 OK
 Length: 11339 (11K) [text/plain]
 Saving to: `grunt.js'
 
 Share
 Improve this answer
 Follow
 edited Sep 4, 2012 at 3:43
 answered Jul 14, 2012 at 21:41
 Zombo Zombo
 1
 4
 * Works for me even without wgetrc tweaks: wget https://raw.github.com/bk322/bk_automates/master/bkubuntu/bkubuntu.bash
 – Adobe
 Sep 3, 2012 at 7:54
 * 1
 My message is more helpful: ERROR: Certificate verification error for raw.github.com: unable to get local issuer certificate. To connect to raw.github.com insecurely, use '--no-check-certificate'.
 – Kos
 Jan 18, 2013 at 13:42
 * 4
 This works for public repositories only. For private repositories you need authentication.
 – rikas
 Jan 5, 2015 at 18:27
 * Mac didn't have wget, so I used curl but I had to use curl -H 'Cache-Control: no-cache, no-store' https://raw.githubusercontent.com/org/repo/master/file > outfile otherwise it does not download if the file has already been downloaded
 – Arundale Ramanathan
 Jun 5, 2022 at 13:15
 Add a comment |
 6

 A nuanced variant of some of the answers here that answers the OP's question:

 git archive --remote=git@archive-accepting-git-server.com:foo/bar.git \
 HEAD path/to/file.txt | tar -xO path/to/file.txt > file.txt
 
 Share
 Improve this answer
 Follow
 edited Sep 14, 2018 at 15:58
 answered Dec 8, 2016 at 7:50
 Willem van Ketwich Willem van Ketwich
 5,397 7 7 gold badges 49 49 silver badges 57 57 bronze badges
 4
 * This does not work, github does not support git archive.
 – Philipp
 Sep 12, 2018 at 15:05
 * 3
 Err.. git is used for more than just GitHub
 – Willem van Ketwich
 Sep 14, 2018 at 5:38
 * 2
 You explicitly set --remote to a github URL in your answer though ;-)
 – Philipp
 Sep 14, 2018 at 8:33
 * 1
 Thanks for the feedback. Have amended the answer accordingly.
 – Willem van Ketwich
 Sep 14, 2018 at 13:22
 Add a comment |
 5

 It seems to me the easiest way to use the following:

 wget https://github.com/name/folder/file.zip?raw=true
 
 Share
 Improve this answer
 Follow
 edited Apr 22, 2020 at 20:49
 Richard Lalancette
 2,381 24 24 silver badges 29 29 bronze badges
 answered Apr 22, 2020 at 19:57
 ihsinme ihsinme
 65 1 1 silver badge 3 3 bronze badges
 1
 * Thank you, simple indeed. To get rid of '?raw=true' at the end of saved file one can use: -O your-file-name at the end of the command above.
 – timanix
 Feb 23, 2022 at 8:21
 Add a comment |
 5

 If no other answer worked (i.e. restrictive GitLab access), you can do a "selective-checkout" by:

 1. git clone --no-checkout --depth=1 --no-tags URL
 2. git restore --staged DIR-OR-FILE
 3. git checkout DIR-OR-FILE

 Although this solution is 100% git compliant and you can checkout a directory, it's not disk nor network optimal as doing a wget/curl on a file.

 Share
 Improve this answer
 Follow
 answered May 5, 2021 at 22:25
 ATorras ATorras
 3,973 2 2 gold badges 31 31 silver badges 39 39 bronze badges
 Add a comment |
 4

 If you repository supports tokens (for example GitLab) then generate a token for your user then navigate to the file you will download and click on RAW output to get the URL. To download the file use:

 curl --silent --request GET --header 'PRIVATE-TOKEN: replace_with_your_token' \
 'http://git.example.com/foo/bar.sql' --output /tmp/bar.sql
 
 Share
 Improve this answer
 Follow
 answered Mar 6, 2018 at 12:48
 panticz panticz
 1,969 24 24 silver badges 16 16 bronze badges
 Add a comment |
 3

 If your Git repository hosted on Azure-DevOps (VSTS) you can retrieve a single file with Rest API.

 The format of this API looks like this:

 https://dev.azure.com/{organization}/_apis/git/repositories/{repositoryId}/items?path={pathToFile}&api-version=4.1?download=true
 

 For example:

 https://dev.azure.com/{organization}/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items?scopePath=/MyWebSite/MyWebSite/Views/Home/_Home.cshtml&download=true&api-version=4.1
 
 Share
 Improve this answer
 Follow
 answered Sep 16, 2018 at 11:07
 Shayki Abramczyk Shayki Abramczyk
 34.3k 16 16 gold badges 86 86 silver badges 108 108 bronze badges
 2
 * It requires api token, doesn't it?
 – Yuri Pozniak
 Nov 6, 2019 at 23:18
 * Depends how you run the api
 – Shayki Abramczyk
 Nov 7, 2019 at 10:09
 Add a comment |
 3

 This is specific for git repos hosted on GitHub

 Try the 'api' command of Github's command line app, gh, to make an authenticated call to Github's 'get repository contents' endpoint.

 The basic command is:

 $gh api /repos/{owner}/{repo}/contents/<path_to_the_file>
 

 As an added bonus, when you do this from inside a directory that contains a clone of the repo you're trying to get the file from, the {owner} and {repo} part will be automatically filled in.

 https://docs.github.com/en/rest/reference/repos#get-repository-content

 The response will be a JSON object. If the <path_to_the_file> indeed points to a file, the JSON will include a 'size', 'name', several url fields to access the file, as well as a 'content' field, which is a base64 encoded version of the file contents.

 To get the file contents, you can curl the value of the "download_url", or just decode the 'content' field. You can do that by piping the base64 command, like this:

 $gh api /repos/{owner}/{repo}/contents/<path-to-the-file> --jq '.content' | base64 -d
 
 Share
 Improve this answer
 Follow
 answered Nov 14, 2021 at 6:44
 dontascii dontascii
 41 1 1 bronze badge
 Add a comment |
 2

 For single file, just use wget command.

 First, follow the pic below to click "raw" to get the url, otherwise you will download code embedded in html.

 Then, the browser will open a new page with url start with https://raw.githubusercontent.com/...

 just enter the command in the terminal:

 #wget https://raw.githubusercontent.com/...
 

 A while the file will put in your folder.

 Share
 Improve this answer
 Follow
 answered Jun 16, 2017 at 15:52
 malajisi malajisi
 2,075 21 21 silver badges 18 18 bronze badges
 1
 * Yep, this also works nice within Python, or other programming languages, with a REST-funcionality. For example for downloading modules from different repositories..
 – Lars GJ
 Oct 10, 2018 at 13:46
 Add a comment |
 2

 The following 2 commands worked for me:

 git archive --remote={remote_repo_git_url} {branch} {file_to_download} -o {tar_out_file}

 Downloads file_to_download as tar archive from branch of remote repository whose url is remote_repo_git_url and stores it in tar_out_file

 tar -x -f {tar_out_file}.tar extracts the file_to_download from tar_out_file

 Share
 Improve this answer
 Follow
 answered Nov 10, 2020 at 20:11
 rok rok
 8,520 17 17 gold badges 60 60 silver badges 117 117 bronze badges
 Add a comment |
 1

 I use curl, it works with public repos or those using https basic authentication via a web interface.

 curl -L --retry 20 --retry-delay 2 -O https://github.com/ACCOUNT/REPO/raw/master/PATH/TO/FILE/FILE.TXT -u USER:PASSWORD

 I've tested it on github and bitbucket, works on both.

 Share
 Improve this answer
 Follow
 answered Jun 4, 2015 at 2:24
 JasonS JasonS
 7,235 5 5 gold badges 41 41 silver badges 60 60 bronze badges
 1
 * work also on gitlab and gitea !
 – DevTheJo
 Jul 4, 2022 at 17:15
 Add a comment |
 1

 Yisrael Dov's answer is the straightforward one, but it doesn't allow compression. You can use --format=zip, but you can't directly unzip that with a pipe command like you can with tar, so you need to save it as a temporary file. Here's a script:

 #!/bin/bash
 
 BASENAME=$0
 
 function usage {
 echo "usage: $BASENAME <remote-repo> <file> ..."
 exit 1
 }
 
 [ 2 -gt "$#" ] && { usage; }
 
 REPO=$1
 shift
 FILES=$@
 
 TMPFILE=`mktemp`.zip
 git archive -9 --remote=$REPO HEAD $FILES -o $TMPFILE
 unzip $TMPFILE
 rm $TMPFILE
 

 This works with directories too.

 Share
 Improve this answer
 Follow
 edited May 23, 2017 at 11:54
 Community Bot
 1 1 1 silver badge
 answered Jul 19, 2016 at 5:11
 naught101 naught101
 18.2k 19 19 gold badges 88 88 silver badges 135 135 bronze badges
 Add a comment |
 1

 Github Enterprise Solution

 HTTPS_DOMAIN=https://git.your-company.com
 ORGANISATION=org
 REPO_NAME=my-amazing-library
 FILE_PATH=path/to/some/file
 BRANCH=develop
 GITHUB_PERSONAL_ACCESS_TOKEN=<your-access-token>
 
 URL="${HTTPS_DOMAIN}/raw/${ORGANISATION}/${REPO_NAME}/${BRANCH}/${FILE_PATH}"
 
 curl -H "Authorization: token ${GITHUB_PERSONAL_ACCESS_TOKEN}" ${URL} > "${FILE_PATH}"
 
 Share
 Improve this answer
 Follow
 answered Jun 4, 2019 at 12:55
 Oliver Pearmain Oliver Pearmain
 19.4k 13 13 gold badges 84 84 silver badges 89 89 bronze badges
 3
 * Where do we find the GITHUB_PERSONAL_ACCESS_TOKEN ?
 – ShadSterling
 Mar 23, 2020 at 18:38
 * 1
 You can create a personal access token by going to https://<your-github-server>/settings/tokens and hitting "Generate new token" button.
 – Oliver Pearmain
 Mar 24, 2020 at 19:51
 * Hmm, we have automations that are given a username and password, which are used to authenticate to multiple systems that use the same SSO, so I was hoping for a way to automate generating a token given a username & password.
 – ShadSterling
 Mar 25, 2020 at 13:36
 Add a comment |
 0

 If you want to get a file from a specific hash + a remote repository I've tried git-archive and it didn't work.

 You would have to use git clone and once the repository is cloned you would have then to use git-archive to make it work.

 I post a question about how to do it more simpler in git archive from a specific hash from remote

 Share
 Improve this answer
 Follow
 edited May 23, 2017 at 12:18
 Community Bot
 1 1 1 silver badge
 answered Dec 14, 2016 at 21:12
 Fernanda Martins Fernanda Martins
 111 2 2 silver badges 6 6 bronze badges
 Add a comment |
 0

 for bitbucket directly from browser (I used safari...) right-click on 'View Raw" and choose "Download Linked File":

 Share
 Improve this answer
 Follow
 answered Aug 30, 2018 at 5:59
 ingconti ingconti
 10.6k 3 3 gold badges 64 64 silver badges 47 47 bronze badges
 Add a comment |
 0

 If you don't mind cloning the entire directory, this small bash/zsh function will have the end result of cloning a single file into your current directory (by cloning the repo into a temp directory and removing it afterwards).

 Pro: You only get the file you want

 Con: You still have to wait for the whole repo to clone

 git-single-file () {
 if [ $# -lt 2 ]
 then
 echo "Usage: $0 <repo url> <file path>"
 return
 fi
 TEMP_DIR=$(mktemp -d)
 git clone $1 $TEMP_DIR
 cp $TEMP_DIR/$2 .
 rm -rf $TEMP_DIR
 }
 
 Share
 Improve this answer
 Follow
 answered Oct 28, 2018 at 20:30
 Christopher Shroba Christopher Shroba
 6,576 7 7 gold badges 37 37 silver badges 67 67 bronze badges
 1
 * This answer may help make sparse clone, that is without overhead.
 – Yuri Pozniak
 Nov 6, 2019 at 23:24
 Add a comment |
 0

 If your goal is just to download the file there's a hassle-free application called gget:

 gget github.com/gohugoio/hugo 'hugo_extended_*_Linux-ARM.deb'
 

 The above example would download single file from hugo repository.

 https://github.com/dpb587/gget

 Share
 Improve this answer
 Follow
 answered Jun 9, 2020 at 5:21
 Granitosaurus Granitosaurus
 19.9k 4 4 gold badges 55 55 silver badges 81 81 bronze badges
 Add a comment |
 -1

 Related to @Steven Penny's answer, I also use wget. Furthermore, to decide which file to send the output to I use -O .

 If you are using gitlabs another possibility for the url is:

 wget "https://git.labs.your-server/your-repo/raw/master/<path-to-file>" -O <output-file>
 

 Unless you have the certificate or you access from a trusted server for the gitlabs installation you need --no-check-certificate as @Kos said. I prefer that rather than modifying .wgetrc but it depends on your needs.

 If it is a big file you might consider using -c option with wget. To be able to continue downloading the file from where you left it if the previous intent failed in the middle.

 Share
 Improve this answer
 Follow
 answered Jun 19, 2014 at 11:44
 Hector Llorens Hector Llorens
 135 5 5 bronze badges
 Add a comment |

 Your Answer

 Thanks for contributing an answer to Stack Overflow!

 * Please be sure to answer the question. Provide details and share your research!

 But avoid …

 * Asking for help, clarification, or responding to other answers.
 * Making statements based on opinion; back them up with references or personal experience.

 To learn more, see our tips on writing great answers.

 Sign up or log in

 Sign up using Google
 Sign up using Facebook
 Sign up using Email and Password
 Submit

 Post as a guest

 Name
 Email

 Required, but never shown

 Post as a guest

 Name
 Email

 Required, but never shown

 Post Your Answer Discard

 By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

 * The Overflow Blog
 * Announcing more ways to learn and grow your skills
 * Why developer experience is the key to better software, straight from the...
 * Featured on Meta
 * Accessibility Update: Colors
 * 2022: a year in moderation
 * Collectives: The next iteration
 * We’re bringing advertisements for technology courses to Stack Overflow
 * Temporary policy: ChatGPT is banned

 Linked

 3
 Pull one file from Git without having to clone the entire repository
 1884
 How do I clone a subdirectory only of a Git repository?
 571
 How to sparsely checkout only one single file from a git repository?
 544
 Managing large binary files with Git
 65
 git export from github remote repository
 8
 How can I check out a single file from GitHub without cloning the whole repository?
 12
 Git fetch single file from remote repository programatically
 7
 How to download single file from a git repository using python
 4
 Cloning single file from Git repository
 5
 Error: GitHub can’t show files that are this big right now - but the file is only 1.06 MB
 See more linked questions

 Related

 7884
 How do I remove local (untracked) files from the current Git working tree?
 5142
 How do I add an empty directory to a Git repository?
 5288
 How do I resolve merge conflicts in a Git repository?
 5525
 How do I reset or revert a file to a specific revision?
 3133
 How do I find and restore a deleted file in a Git repository?
 7657
 How do I make Git forget about a file that was tracked, but is now in .gitignore?
 5312
 Reset local repository branch to be just like remote repository HEAD
 5766
 How do I change the URI (URL) for a remote Git repository?
 5196
 How to determine the URL that a local Git repository was originally cloned from
 7613
 How do I revert a Git repository to a previous commit?

 Hot Network Questions

 * How would you say "mindless fun" in German
 * Understanding how to terminate recurrence relations in quantum SHO
 * Why do Pokemon sometimes have dark stripes in Pokemon Go?
 * Is this Stanford certificate real?
 * Can poor grades in the bachelor's affect working in the academia?
 * Stable words in the Proto-Indo-European language family
 * Do distinctions and concepts exist?
 * Table of unequal sequence
 * Can someone choose to not use their “get out of jail free” card?
 * Juvenile SF Novel With Conflict between Earth Humans and Colonists
 * A very difficult functional equation
 * How big of an explosion can you make with a fusion reactor?
 * Why are there 23 members of the grand jury?
 * (Rev. 2) Command-line Tower of Hanoi game
 * Meaning of "nude" in Saxo Grammaticus
 * Can "né" or "née" be used to refer to a religious's secular name?
 * How are local varibales stored on Atmega
 * How to make softbody interact with an animated rigid body
 * How can an Indian homemaker spouse be guaranteed a Schengen tourist visa?
 * Inconsistent interline spacing in enumitem
 * Some conjectures about prime gaps
 * Why are atoms electrically neutral?
 * How to Draw Such a Graph?
 * When did Kievan Rus' rulers become culturally Slavic?
 more hot questions
 Question feed

 Subscribe to RSS

 Question feed

 To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

 Stack Overflow

 * Questions
 * Help

 Products

 * Teams
 * Advertising
 * Collectives
 * Talent

 Company

 * About
 * Press
 * Work Here
 * Legal
 * Privacy Policy
 * Terms of Service
 * Contact Us
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.2.6.43220

 Your privacy

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

 Accept all cookies Customize settings
This is specific for git repos hosted on GitHub 
Try the 'api' command of Github's command line app, gh, to make an authenticated call to Github's 'get repository contents' endpoint.
The basic command is:
 $gh api /repos/{owner}/{repo}/contents/&lt;path_to_the_file&gt;
 
As an added bonus, when you do this from inside a directory that contains a clone of the repo you're trying to get the file from, the {owner} and {repo} part will be automatically filled in.
 https://docs.github.com/en/rest/reference/repos#get-repository-content 
The response will be a JSON object. If the &lt;path_to_the_file&gt; indeed points to a file, the JSON will include a 'size', 'name', several url fields to access the file, as well as a 'content' field, which is a base64 encoded version of the file contents.
To get the file contents, you can curl the value of the "download_url", or just decode the 'content' field. You can do that by piping the base64 command, like this:
 $gh api /repos/{owner}/{repo}/contents/&lt;path-to-the-file&gt; --jq '.content' | base64 -d
Retrieve a single file from a repository
https://stackoverflow.com/questions/1125476/retrieve-a-single-file-from-a-repository
https://stackoverflow.com/questions/71920474/git-download-a-file-from-a-private-repo
git download a file from a private repo
2022-04-19
Mnr
<div><div>
<div>
<div>
<div>
<p>I want to download a file from a private repo. I want to do this with "git" (not gitlab/github api).</p>
<p>Git provide this way <a href="https://git-scm.com/docs/git-archive">https://git-scm.com/docs/git-archive</a>. You can fetch a file <a href="https://stackoverflow.com/a/18331440/13663683">like this</a></p>
<pre><code>git archive --remote=ssh://host/pathto/repo.git HEAD README.md
</code></pre>
<p>However I cannot use ssh. I want to download file using http and a token I have.</p>
<p>I can clone my repo like this:</p>
<pre><code>git clone -b branch http://oauth2:$gitToken@${gitRepo}
</code></pre>
<p>but if u make a command like this:</p>
<pre><code>git archive --remote=http://oauth2:$gitToken@${gitRepo} branch filename 
</code></pre>
<p>You encounter an error:</p>
<pre><code>fatal: operation not supported by protocol
</code></pre>
<p><strong>So the question is how we can download a file from a private git repo using a token.</strong>
(git-archive approach or other (git) way)</p>
<p>Thanks</p>
</div>
<div>
<div>
<p>
 asked 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/13663683/mnr"><p></p></a>
</div>
</div>
</div>
<p></p>
</div>
<div>
<div>
<div>
<div>
<p>GitHub (and I suspect most public Git hosting service provider) does not support remote archiving through HTTPS.</p>
<p>The more modern git way would be the recent <code>git clone --filter</code>/<code>sparse-checkout</code> approach</p>
<pre><code>git --filter=blob:none --sparse https://[token]@github.com/CSSEGISandData/COVID-19.git
 COVID-19/
git sparse-checkout init --cone
git sparse-checkout add /the folder you want/
</code></pre>
<p>See also <a href="https://stackoverflow.com/a/71366001/6309">this approach</a>, where I edit <code>.git\info\sparse-checkout</code></p>
</div>
<div>
<div>
<p>
 answered 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/6309/vonc"><p></p></a>
</div>
</div>
</div>
<p></p>
</div>
<div>
<div>
<p>I have modified previous answer and have got working script for downloading only one file with read access token</p>
<pre><code> git --depth 1 --filter=blob:none \
 -b master https://&lt;my_token_name&gt;:&lt;my_token&gt;@&lt;my_git_repo&gt;.git tmp_dir &amp;&amp; \
 tmp_dir &amp;&amp; \
 git sparse-checkout &lt;path_to_file_in_repo&gt; &amp;&amp; \
 git checkout master &amp;&amp; \
 -f &lt;path_to_file_in_repo&gt;
</code></pre>
</div>
<div>
<div>
<p>
 answered 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/9964552/dmitriy-grachev"><p></p></a>
</div>
</div>
</div>
<div>
<div>
<div>
<p>I was hoping there was a straightforward solution to this. But the only way I could achieve downloading files was "sparse-checkout" as <a href="https://stackoverflow.com/a/71921698/13663683">they said</a>.</p>
<p>However I modified the way to get only a file, not a directory so it differ from that instruction.</p>
<p>For downloading file u can do this:</p>
<pre><code>git clone --depth 1 --filter=blob:none --no-checkout -b branchName http://oauth2:$gitToken@$gitRepo repoFolder
cd repoFolder
git sparse-checkout set $fileName
</code></pre>
<p>Those git clone flags are for minimizing getting extra files.</p>
<p>And this will fetch your file in that directory.</p>
</div>
<div>
<div>
<p>
 answered 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/13663683/mnr"><p></p></a>
</div>
</div>
</div>
<p></p>
</div>
<h2>
</h2>
</div>
</div></div>||||I|||| Join Stack Overflow

 By clicking “Sign up”, you agree to our terms of service and acknowledge you have read our privacy policy.

 Sign up with Google
 Sign up with GitHub

 OR

 Email
 Password
 Sign up

 Already have an account? Log in
Skip to main content
 Stack Overflow
 1. About
 2. Products
 3. For Teams
 1. Stack Overflow Public questions & answers
 2. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
 3. Talent Build your employer brand
 4. Advertising Reach developers & technologists worldwide
 5. Labs The future of collective knowledge sharing
 6. About the company
 Loading…

 1. current community

 + Stack Overflow
 help chat
 + Meta Stack Overflow

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 2. 
 3. Log in
 4. Sign up
 1. 
 1. Home
 2. Questions
 3. Tags
 4. 
 5. Users
 6. Companies
 7. Labs
 8. Discussions
 9. Collectives

 10. Communities for your favorite technologies. Explore all Collectives

 2. Teams

 Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

 Explore Teams Create a free Team
 3. Teams

 4. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

 Collectives™ on Stack Overflow

 Find centralized, trusted content and collaborate around the technologies you use most.

 Learn more about Collectives

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 Get early access and see previews of new features.

 Learn more about Labs

 git download a file from a private repo

 Ask Question
 Asked 2 years ago
 Modified 1 year, 2 months ago
 Viewed 2k times
 This question shows research effort; it is useful and clear
 1
 Save this question.
 Show activity on this post.

 I want to download a file from a private repo. I want to do this with "git" (not gitlab/github api).

 Git provide this way https://git-scm.com/docs/git-archive. You can fetch a file like this

 git archive --remote=ssh://host/pathto/repo.git HEAD README.md
 

 However I cannot use ssh. I want to download file using http and a token I have.

 I can clone my repo like this:

 git clone -b branch http://oauth2:$gitToken@${gitRepo}
 

 but if u make a command like this:

 git archive --remote=http://oauth2:$gitToken@${gitRepo} branch filename 
 

 You encounter an error:

 fatal: operation not supported by protocol
 

 So the question is how we can download a file from a private git repo using a token. (git-archive approach or other (git) way)

 Thanks

 * git
 * download
 * token
 * private
 * archive
 Share
 Share a link to this question
 Copy linkCC BY-SA 4.0
 Improve this question
 Follow
 Follow this question to receive notifications
 edited Apr 19, 2022 at 12:12
 mnr
 asked Apr 19, 2022 at 6:22
 mnr mnr
 93 7 7 bronze badges
 3
 * 2
 Does this answer your question? git archive fatal: Operation not supported by protocol
 – Ôrel
 Apr 19, 2022 at 7:28
 * 1
 Ty for comment. I'm afraid it does not. I'm using git v2.25.1, that is rather new version. Also I want to use token in request because it is a private repo.
 – mnr
 Apr 19, 2022 at 11:36
 * 1
 Git doesn't really download files , but rather downloads commits . See VonC's answer for ways to make Git download fewer commits than usual. When you use git archive you're not invoking Git as usual, which is why you have to use the ssh method to do this with GitHub. Other sites may have their own special rules.
 – torek
 Apr 19, 2022 at 17:03
 Add a comment |

 3 Answers

 Sorted by: Reset to default
 Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
 This answer is useful
 2
 Save this answer.
 Show activity on this post.

 GitHub (and I suspect most public Git hosting service provider) does not support remote archiving through HTTPS.

 The more modern git way would be the recent git clone --filter/sparse-checkout approach

 git clone --filter=blob:none --sparse https://[token]@github.com/CSSEGISandData/COVID-19.git
 cd COVID-19/
 git sparse-checkout init --cone
 git sparse-checkout add /the folder you want/
 

 See also this approach, where I edit .git\info\sparse-checkout

 Share
 Share a link to this answer
 Copy linkCC BY-SA 4.0
 Improve this answer
 Follow
 Follow this answer to receive notifications
 answered Apr 19, 2022 at 8:16
 VonC VonC
 1
 4
 * Ty for Answering. In sparse-checkout u can get a specific directory. Its not for getting a specific file. Am I right? And for limitation in public Git hosts u mentioned, can it be solved in personal git host? Then how u can add token to request?
 – mnr
 Apr 19, 2022 at 11:41
 * @mnr Yes to the first: that would limit the number of files to one folder. I am not sure regarding the second question: I suppose that indeed you can add support for remote archive on a Git server you control, but I do not know (yet) how.
 – VonC
 Apr 19, 2022 at 12:07
 * Thank u for helping
 – mnr
 Apr 20, 2022 at 4:23
 * @mnr Don't forget to accept an answer for this or your past question. You can [accept your own answer]( stackoverflow.com/help/self-answer , starting tomorrow).
 – VonC
 Apr 20, 2022 at 6:01
 Add a comment |
 This answer is useful
 2
 Save this answer.
 Show activity on this post.

 I have modified previous answer and have got working script for downloading only one file with read access token

 git clone --depth 1 --filter=blob:none \
 -b master https://<my_token_name>:<my_token>@<my_git_repo>.git tmp_dir && \
 cd tmp_dir && \
 git sparse-checkout set <path_to_file_in_repo> && \
 git checkout master && \
 test -f <path_to_file_in_repo>
 
 Share
 Share a link to this answer
 Copy linkCC BY-SA 4.0
 Improve this answer
 Follow
 Follow this answer to receive notifications
 answered Mar 1, 2023 at 15:50
 Dmitriy Grachev Dmitriy Grachev
 359 2 2 silver badges 5 5 bronze badges
 Add a comment |
 This answer is useful
 1
 Save this answer.
 Show activity on this post.

 I was hoping there was a straightforward solution to this. But the only way I could achieve downloading files was "sparse-checkout" as they said.

 However I modified the way to get only a file, not a directory so it differ from that instruction.

 For downloading file u can do this:

 git clone --depth 1 --filter=blob:none --no-checkout -b branchName http://oauth2:$gitToken@$gitRepo repoFolder
 cd repoFolder
 git sparse-checkout set $fileName
 

 Those git clone flags are for minimizing getting extra files.

 And this will fetch your file in that directory.

 Share
 Share a link to this answer
 Copy linkCC BY-SA 4.0
 Improve this answer
 Follow
 Follow this answer to receive notifications
 answered Apr 20, 2022 at 3:56
 mnr mnr
 93 7 7 bronze badges
 0
 Add a comment |

 Your Answer

 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 

 Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

 Thanks for contributing an answer to Stack Overflow!

 * Please be sure to answer the question. Provide details and share your research!

 But avoid …

 * Asking for help, clarification, or responding to other answers.
 * Making statements based on opinion; back them up with references or personal experience.

 To learn more, see our tips on writing great answers.

 Sign up or log in

 Sign up using Google
 Sign up using Facebook
 Sign up using Email and Password
 Submit

 Post as a guest

 Name
 Email

 Required, but never shown

 Post as a guest

 Name
 Email

 Required, but never shown

 Post Your Answer Discard

 By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

 Not the answer you're looking for? Browse other questions tagged
 * git
 * download
 * token
 * private
 * archive
 or ask your own question.

 * The Overflow Blog
 * Supporting the world’s most-used database engine through 2050
 * Net neutrality is in; TikTok and noncompetes are out
 * Featured on Meta
 * Testing a new version of Stack Overflow Jobs
 * Pausing the 1-rep voting experiment on Stack Overflow: reflecting on the...
 * Temporary policy: Generative AI (e.g., ChatGPT) is banned

 Linked

 292
 Retrieve a single file from a repository
 41
 git archive fatal: Operation not supported by protocol
 0
 Perform git sparse-checkout of everything except a list of subdirectories

 Related

 2
 Download files from git remote repository
 131
 How can I download a single raw file from a private github repo using the command line?
 1
 Download a specific file from .git repo
 6
 Download directory from private github repo using wget
 0
 how to download all files in a private git repo in bash
 0
 Download from a private Github repo
 22
 Github GET on private repo with access token
 2
 How to download a binary file from private Github repository?
 0
 Download single file/folder from a Private GitHub repository (it contains multiple folders)
 2
 How to download a private repository from Gitlab without exposing the access token in the url?

 Hot Network Questions

 * I was about to join a company, but rejected a day before cause of their observed work ethics. Now they are threatening me
 * Progress on Gromov's Conjecture of the bound of total Betti numbers
 * Inner voice when reading mathematics
 * Why doesn't b = a; f[a_] := b; f[2] return 2?
 * If microwave ovens run at 2.4 GHz, what is the long-term effect on living tissue of exposure to this frequency, but with a thousand times less energy?
 * Why does Helix Nebula look like an eye?
 * Redshift of CMB
 * How to obtain meat when you have no hunters in the colony?
 * I created a quiver image but when I compile it clashes
 * What to check when chainsaw chain won't move freely?
 * Mixture distributions: an intuition on why we cannot infer the number of mixture components by visual inspection
 * HST can apparently operate in one-gyro mode, if need be. Why was the Compton Gamma Ray Observatory de-orbited when it lost one of its three gyros?
 * Value of describing game mechanics in a data file instead of hardcoding?
 * Limit of integral of sum of cosine functions by CLT?
 * Why is construction required in geometry?
 * Does Hebrews 13:2 teach that God's elect have become angels in disguise?
 * How to obtain the list of outermost graph nodes
 * Sort a file by lines, regardless of their content
 * Are there written historical records of ships possibly affected by underwater volcanic activity?
 * Can a district attorney dismiss their own traffic ticket?
 * How do I separate these double exposed images?
 * How do objects move in a linear path if you can never apply a force to the center of mass of it?
 * What's the optimal area a rectangle can have inside this parabola?
 * Is there a true 1 dimensional object?
 Question feed

 Subscribe to RSS

 Question feed

 To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

 Stack Overflow

 * Questions
 * Help

 Products

 * Teams
 * Advertising
 * Collectives
 * Talent

 Company

 * About
 * Press
 * Work Here
 * Legal
 * Privacy Policy
 * Terms of Service
 * Contact Us
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2024.4.30.8420

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
 Accept all cookies Necessary cookies only
 Customize settings
I want to download a file from a private repo. I want to do this with "git" (not gitlab/github api).
Git provide this way https://git-scm.com/docs/git-archive. You can fetch a file like this 
 git archive --remote=ssh://host/pathto/repo.git HEAD README.md
 
However I cannot use ssh. I want to download file using http and a token I have.
I can clone my repo like this:
 git clone -b branch http://oauth2:$gitToken@${gitRepo}
 
but if u make a command like this:
 git archive --remote=http://oauth2:$gitToken@${gitRepo} branch filename 
 
You encounter an error:
 fatal: operation not supported by protocol
 
 So the question is how we can download a file from a private git repo using a token.
(git-archive approach or other (git) way)
Thanks
 asked 
   
 
GitHub (and I suspect most public Git hosting service provider) does not support remote archiving through HTTPS.
The more modern git way would be the recent git clone --filter/sparse-checkout approach
 git --filter=blob:none --sparse https://[token]@github.com/CSSEGISandData/COVID-19.git
 COVID-19/
git sparse-checkout init --cone
git sparse-checkout add /the folder you want/
 
See also this approach, where I edit .git\info\sparse-checkout 
 answered 
   
 
I have modified previous answer and have got working script for downloading only one file with read access token
  git --depth 1 --filter=blob:none \
 -b master https://&lt;my_token_name&gt;:&lt;my_token&gt;@&lt;my_git_repo&gt;.git tmp_dir &amp;&amp; \
 tmp_dir &amp;&amp; \
 git sparse-checkout &lt;path_to_file_in_repo&gt; &amp;&amp; \
 git checkout master &amp;&amp; \
 -f &lt;path_to_file_in_repo&gt;
 
 answered 
   
I was hoping there was a straightforward solution to this. But the only way I could achieve downloading files was "sparse-checkout" as they said.
However I modified the way to get only a file, not a directory so it differ from that instruction.
For downloading file u can do this:
 git clone --depth 1 --filter=blob:none --no-checkout -b branchName http://oauth2:$gitToken@$gitRepo repoFolder
cd repoFolder
git sparse-checkout set $fileName
 
Those git clone flags are for minimizing getting extra files.
And this will fetch your file in that directory.
 answered
git download a file from a private repo
https://stackoverflow.com/questions/71920474/git-download-a-file-from-a-private-repo
https://www.grepper.com/answers/47853/git+clone+with+folder+name
git clone with folder name - Code Examples & Solutions
2020-05-23T00:00:00Z
Distinct Dormouse
<html><body><div><div>
<div>
 <div>
 
 <h2>
 git clone with folder name
 
 </h2>
 
 
 </div>
 
 <p><span>
 Popularity</span>
 10/10 <span>Helpfulness</span>
 4/10
 <span>Language </span>
 <a href="https://www.grepper.com/answers/tag/shell">shell</a>
 </p>
 
	 
 
 
 
 <div>
 
 <p><a href="https://www.grepper.com/profile/distinct-dormouse-os81suftn8yv">
 
 <span></span>
 
 </a>
 </p>
 
 
 </div>
 
 
	
 
 </div>
 <hr/>
<div>
 
 
 <p><span>
 Popularity</span>
 10/10 <span>Helpfulness</span>
 3/10
 <span>Language </span>
 <a href="https://www.grepper.com/answers/tag/shell">shell</a>
 </p>
 
 
 
 
 
 
 <div>
 
 <p><a href="https://www.grepper.com/profile/fahim-foysal-rabby">
 
 <span></span>
 
 </a>
 </p>
 <div>
 <p>
 Contributed on Jan 12 2021 </p>
 
 
 <p><a href="https://www.grepper.com/profile/fahim-foysal-rabby">Fahim Foysal</a></p><p>
 0 Answers  Avg Quality 2/10 
 </p>
 </div>
 
 </div>
 
 
	
 
 </div>
 <hr/>
<div>
 
 
 <p><span>
 Popularity</span>
 10/10 <span>Helpfulness</span>
 2/10
 <span>Language </span>
 <a href="https://www.grepper.com/answers/tag/shell">shell</a>
 </p>
 
 
 
 
 
 
 <div>
 
 <p><a href="https://www.grepper.com/profile/riya">
 
 <span></span>
 
 </a>
 </p>
 <div>
 <p>
 Contributed on Feb 22 2021 </p>
 
 
 <p><a href="https://www.grepper.com/profile/riya">Clumsy Crane</a></p><p>
 0 Answers  Avg Quality 2/10 
 </p>
 </div>
 
 </div>
 
 
	
 
 </div>
 <hr/>
<div>
 
 
 <p><span>
 Popularity</span>
 10/10 <span>Helpfulness</span>
 2/10
 <span>Language </span>
 <a href="https://www.grepper.com/answers/tag/shell">shell</a>
 </p>
 
	 
 
 
 
 <div>
 
 <p><a href="https://www.grepper.com/profile/antti-veikkolainen">
 
 <span></span>
 
 </a>
 </p>
 <div>
 <p>
 Contributed on Nov 06 2021 </p>
 
 
 <p><a href="https://www.grepper.com/profile/antti-veikkolainen">Av3</a></p><p>
 0 Answers  Avg Quality 2/10 
 </p>
 </div>
 
 </div>
 
 
	
 
 </div>
 <hr/>
<div>
 
 
 <p><span>
 Popularity</span>
 10/10 <span>Helpfulness</span>
 2/10
 <span>Language </span>
 <a href="https://www.grepper.com/answers/tag/shell">shell</a>
 </p>
 
	 
 
 
 
 <div>
 
 <p><a href="https://www.grepper.com/profile/nervous-newt-roej8aa0ihut">
 
 
 </a>
 </p>
 <div>
 <p>
 Contributed on Apr 16 2021 </p>
 
 
 <p><a href="https://www.grepper.com/profile/nervous-newt-roej8aa0ihut">Nervous Newt</a></p><p>
 0 Answers  Avg Quality 2/10 
 </p>
 </div>
 
 </div>
 
 
	
 
 </div>
 <hr/>
<div>
 
 
 <p><span>
 Popularity</span>
 7/10 <span>Helpfulness</span>
 2/10
 <span>Language </span>
 <a href="https://www.grepper.com/answers/tag/shell">shell</a>
 </p>
 
 
 
 
 
 
 <div>
 
 <p><a href="https://www.grepper.com/profile/you">
 
 
 </a>
 </p>
 <div>
 <p>
 Contributed on Sep 11 2023 </p>
 
 
 <p><a href="https://www.grepper.com/profile/you">YOU.com</a></p><p>
 0 Answers  Avg Quality 2/10 
 </p>
 </div>
 
 </div>
 
 
	
 
 </div>
 <hr/>
<div>
 
 
 <p><span>
 Popularity</span>
 9/10 <span>Helpfulness</span>
 1/10
 <span>Language </span>
 <a href="https://www.grepper.com/answers/tag/"></a>
 </p>
 
 
 
 
 
 
 <div>
 
 <p><a href="https://www.grepper.com/profile/you">
 
 
 </a>
 </p>
 <div>
 <p>
 Contributed on Sep 12 2023 </p>
 
 
 <p><a href="https://www.grepper.com/profile/you">YOU.com</a></p><p>
 0 Answers  Avg Quality 2/10 
 </p>
 </div>
 
 </div>
 
 
	
 
 </div>
 <hr/>
</div></div></body></html>
git clone with folder name
 
 
 
 
 
 
  
 Popularity
 10/10 Helpfulness
 4/10
 Language 
 shell
 
 
	 
 
 
 
 
 
  
 
  
 
 
 
 
 
 
 
 
	
 
 
 
 
 
  
 Popularity
 10/10 Helpfulness
 3/10
 Language 
 shell
 
 
 
 
 
 
 
 
 
  
 
  
 
 
 
 
 
 Contributed on Jan 12 2021 
 
 
  Fahim Foysal  
 0 Answers  Avg Quality 2/10 
 
 
 
 
 
 
	
 
 
 
 
 
  
 Popularity
 10/10 Helpfulness
 2/10
 Language 
 shell
 
 
 
 
 
 
 
 
 
  
 
  
 
 
 
 
 
 Contributed on Feb 22 2021 
 
 
  Clumsy Crane  
 0 Answers  Avg Quality 2/10 
 
 
 
 
 
 
	
 
 
 
 
 
  
 Popularity
 10/10 Helpfulness
 2/10
 Language 
 shell
 
 
	 
 
 
 
 
 
  
 
  
 
 
 
 
 
 Contributed on Nov 06 2021 
 
 
  Av3  
 0 Answers  Avg Quality 2/10 
 
 
 
 
 
 
	
 
 
 
 
 
  
 Popularity
 10/10 Helpfulness
 2/10
 Language 
 shell
 
 
	 
 
 
 
 
 
  
 
 
 
 
 
 
 Contributed on Apr 16 2021 
 
 
  Nervous Newt  
 0 Answers  Avg Quality 2/10 
 
 
 
 
 
 
	
 
 
 
 
 
  
 Popularity
 7/10 Helpfulness
 2/10
 Language 
 shell
 
 
 
 
 
 
 
 
 
  
 
 
 
 
 
 
 Contributed on Sep 11 2023 
 
 
  YOU.com  
 0 Answers  Avg Quality 2/10 
 
 
 
 
 
 
	
 
 
 
 
 
  
 Popularity
 9/10 Helpfulness
 1/10
 Language 
  
 
 
 
 
 
 
 
 
 
  
 
 
 
 
 
 
 Contributed on Sep 12 2023 
 
 
  YOU.com  
 0 Answers  Avg Quality 2/10
git clone with folder name - Code Examples & Solutions
https://www.codegrepper.com/code-examples/shell/git+clone+repository+stack+overflow
https://stackoverflow.com/questions/1213430/how-to-fully-delete-a-git-repository-created-with-init
How to fully delete a git repository created with init?
2016-09-13
User
<div><div>
 
<div>
 
 
<div>
 
 <p>I created a git repository with <code>git init</code>. I'd like to delete it entirely and init a new one.</p>
 
 <div>
 
 <div>
 
 <div>
 <a href="/users/608639/jww"><p></p></a>
 </div>
 <div>
 <p><a href="/users/608639/jww">jww</a></p><p>95k88 gold badges396 silver badges860 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 asked Jul 31, 2009 at 16:06
 </p>
 <div>
 <a href="/users/105813/user105813"><p></p></a>
 </div>
 
</div>
 </div>
 
</div>
 <p>0</p>
 </div>
 <div>
 
 
<div>
 
 
<div>
 
 <div>
<p>Git keeps all of its files in the <code>.git</code> directory. Just remove that one and init again.</p>
<p>If you can't find it, it's because it is hidden.</p>
<ul>
<li><p>In Windows 7, you need to go to your folder, click on <kbd>Organize</kbd> on the top left, then click on <kbd>Folder and search options</kbd>, then click on the <kbd>View</kbd> tab and click on the <kbd>Show hidden files, folders and drives</kbd> radio button.</p></li>
<li><p>On a Mac OS:</p>
<ul>
<li><p>Open a Terminal (via Spotlight: press <kbd>CMD</kbd> + <kbd>SPACE</kbd>, type <code>terminal</code> and press <kbd>Enter</kbd>) and run:</p>
<pre><code>defaults write com.apple.finder AppleShowAllFiles 1 &amp;&amp; killall Finder
</code></pre>
<p>Note: The keyboard shortcut to show hidden files in Finder is 
<kbd>CMD</kbd> + <kbd>SHIFT</kbd> + <kbd>.</kbd> so it is no longer necessary to modify the 
finder config this way</p></li>
<li><p>You could also type <code>cd</code> (the space is important), drag and drop your git repo folder from Finder to the terminal window, press <kbd>return</kbd>, then type <code>rm -fr .git</code>, then <kbd>return</kbd> again.</p></li>
</ul></li>
<li><p>On Ubuntu, use shortcut <kbd>Ctrl + H</kbd>.</p></li>
</ul>
 </div>
 <div>
 
 <div>
 
 <div>
 <a href="/users/10607772/double-beep"><p></p></a>
 </div>
 <div>
 <p><a href="/users/10607772/double-beep">double-beep</a></p><p>4,85716 gold badges32 silver badges41 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 answered Jul 31, 2009 at 16:08
 </p>
 <div>
 <a href="/users/1466/kristof-provost"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1466/kristof-provost">Kristof Provost</a>Kristof Provost</p><p>25.8k2 gold badges24 silver badges28 bronze badges
 </p>
 </div>
</div>
 </div>
 
</div>
 <p>6</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p>If you really want to remove all of the repository, leaving only the working directory then it should be as simple as this.</p>
<pre><code>rm -rf .git
</code></pre>
<p>The usual provisos about <code>rm -rf</code> apply. Make sure you have an up to date backup and are absolutely sure that you're in the right place before running the command. etc., etc.</p>
 </div>
 <div>
 <p>
 answered Jul 31, 2009 at 16:08
 </p>
 <div>
 <a href="/users/19563/cb-bailey"><p></p></a>
 </div>
 <div>
 <p><a href="/users/19563/cb-bailey">CB Bailey</a>CB Bailey</p><p>733k101 gold badges626 silver badges651 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>5</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p>If you want to delete all .git folders in a project use the following command:</p>
<pre><code>find . -type f | grep -i "\.git" | xargs rm
</code></pre>
<p>This will also delete all the .git folders and .gitignore files from all subfolders</p>
 </div>
 <div>
 <p>
 answered Apr 16, 2012 at 9:25
 </p>
 <div>
 <a href="/users/580173/ejazz"><p></p></a>
 </div>
 <div>
 <p><a href="/users/580173/ejazz">ejazz</a>ejazz</p><p>2,4581 gold badge19 silver badges29 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>2</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p>after cloning the repo</p>
<pre><code>cd /repo folder/
</code></pre>
<p>to go to the file directory then</p>
<pre><code>ls -a
</code></pre>
<p>to see all files hidden and unhidden</p>
<pre><code>.git .. .gitignore .etc
</code></pre>
<p>if you like you can check the repo origin</p>
<pre><code>git remote -v
</code></pre>
<p>now delete .git which contains everything about git</p>
<pre><code>rm -rf .git
</code></pre>
<p>after deleting, you would discover that there is no git linked check remote again</p>
<pre><code>git remote -v
</code></pre>
<p>now you can init your repository with</p>
<pre><code>git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/Leonuch/flex.git
git push -u origin main
</code></pre>
 </div>
 <div>
 
 <div>
 
 <div>
 <a href="/users/1376618/anatoly"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1376618/anatoly">Anatoly</a></p><p>19.4k3 gold badges25 silver badges41 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 answered Oct 13, 2019 at 21:10
 </p>
 <div>
 <a href="/users/12029144/leouonline"><p></p></a>
 </div>
 <div>
 <p><a href="/users/12029144/leouonline">leouonline</a>leouonline</p><p>2983 silver badges5 bronze badges
 </p>
 </div>
</div>
 </div>
 
</div>
 <p>1</p>
 </div>
 
<div>
 
 <div>
<p>Alternative to killing TortoiseGit:</p>
<ul>
<li>Open the TortoiseGit-Settings (right click to any folder, TortoiseGit → Settings)</li>
<li>Go to the <strong>Icon Overlays</strong> option. </li>
<li>Change the <em>Status Cache</em> from <strong>Default</strong> to <strong>None</strong></li>
<li>Now you can delete the directory (either with Windows Explorer or <code>rmdir /S /Q</code>)</li>
<li>Set back the <em>Status Cache</em> from <strong>None</strong> to <strong>Default</strong> and you should be fine again...</li>
</ul>
 </div>
 <div>
 
 <div>
 
 <div>
 <a href="/users/1087848/11684"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1087848/11684">11684</a></p><p>7,34612 gold badges47 silver badges70 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 answered Nov 30, 2010 at 9:51
 </p>
 <div>
 <a href="/users/520162/eckes"><p></p></a>
 </div>
 <div>
 <p><a href="/users/520162/eckes">eckes</a>eckes</p><p>62.8k28 gold badges169 silver badges198 bronze badges
 </p>
 </div>
</div>
 </div>
 
</div>
 
<div>
 
 
<div>
 
 <div>
<p>Where $GIT_DIR is the path to the folder to be searched (the git repo path), execute the following in terminal.</p>
<pre><code>find $GIT_DIR -name *.git* -ok rm -Rf {} \;
</code></pre>
<p>This will recursively search for any directories or files containing ".git" in the file/directory name within the specified Git directory. This will include .git/ and .gitignore files and any other .git-like assets. The command is interactive and will ask before removing. To proceed with the deletion, simply enter y, then Enter.</p>
 </div>
 <div>
 <p>
 answered Aug 14, 2012 at 17:06
 </p>
 <div>
 <a href="/users/845289/scorpius"><p></p></a>
 </div>
 <div>
 <p><a href="/users/845289/scorpius">Scorpius</a>Scorpius</p><p>9991 gold badge10 silver badges22 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>0</p>
 </div>
 
<div>
 
 <div>
<p>To fully delete the <code>.git</code> repository in your computer (in Windows 8 and above):</p>
<ol>
<li>The <code>.git</code> repository is normally hidden in windows </li>
<li>So you need to mark the "hidden items" to show the hidden folders </li>
<li>At the top site of you directory you find "view" option </li>
<li>Inside "view" option you find "hidden items" and mark it </li>
<li>Then you see the <code>.git</code> repository then you can delete it</li>
</ol>
 </div>
 <div>
 
 <div>
 
 <div>
 <a href="/users/1324/paul-roub"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1324/paul-roub">Paul Roub</a></p><p>36.2k27 gold badges82 silver badges89 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 answered Aug 7, 2017 at 16:45
 </p>
 <div>
 <a href="/users/6391888/squal"><p></p></a>
 </div>
 <div>
 <p><a href="/users/6391888/squal">squal </a>squal </p><p>1853 silver badges14 bronze badges
 </p>
 </div>
</div>
 </div>
 
</div>
 
<div>
 
 
<div>
 
 <div>
<p>I tried:</p>
<p><code>rm -rf .git</code> and also </p>
<blockquote>
 <p>Git keeps all of its files in the .git directory. Just remove that one
 and init again.</p>
</blockquote>
<p>Neither worked for me. Here's what did:</p>
<ul>
<li>Delete all files except for <code>.git</code></li>
<li>git add . -A</li>
<li>git commit -m "deleted entire project"</li>
<li>git push</li>
</ul>
<p>Then create / restore the project from backup:</p>
<ul>
<li>Create new project files (or copy paste a backup)</li>
<li>git add . -A</li>
<li>git commit -m "recreated project"</li>
<li>git push</li>
</ul>
 </div>
 <div>
 <p>
 answered Feb 4, 2015 at 18:24
 </p>
 <div>
 <a href="/users/87796/p-brian-mackey"><p></p></a>
 </div>
 <div>
 <p><a href="/users/87796/p-brian-mackey">P.Brian.Mackey</a>P.Brian.Mackey</p><p>42.6k65 gold badges231 silver badges344 bronze badges
 </p>
 </div>
</div>
 
</div>
 <p>0</p>
 </div>
 
<div>
 
 
<div>
 
 <div>
<p>you can use :</p>
<blockquote>
<p>git remote remove origin</p>
</blockquote>
<p>to remove a linked repo then:</p>
<blockquote>
<p>git remote add origin </p>
</blockquote>
<p>to add new one</p>
 </div>
 <div>
 <p>
 answered Jan 28, 2021 at 9:39
 </p>
 <div>
 <a href="/users/3677294/monirrouissi"><p></p></a>
 </div>
 
</div>
 
</div>
 <p>1</p>
 </div>
 
<div>
 
 <div>
<p>You can create an <strong>alias</strong> for it. I am using ZSH shell with Oh-my-Zsh and here is an handy alias:</p>
<pre><code># delete and re-init git
# usage: just type 'gdelinit' in a local repository
alias gdelinit="trash .git &amp;&amp; git init"
</code></pre>
<p>I am using <a href="https://github.com/sindresorhus/trash">Trash</a> to trash the <code>.git</code> folder since using <code>rm</code> is really dangerous:</p>
<pre><code>trash .git
</code></pre>
<p>Then I am re-initializing the git repo:</p>
<pre><code>git init
</code></pre>
 </div>
 <div>
 
 <div>
 
 <div>
 <a href="/users/3453226/spongebob"><p></p></a>
 </div>
 <div>
 <p><a href="/users/3453226/spongebob">spongebob</a></p><p>8,13714 gold badges48 silver badges83 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 answered Dec 2, 2015 at 8:21
 </p>
 <div>
 <a href="/users/950111/ahmad-awais"><p></p></a>
 </div>
 <div>
 <p><a href="/users/950111/ahmad-awais">Ahmad Awais</a>Ahmad Awais</p><p>31.5k5 gold badges74 silver badges55 bronze badges
 </p>
 </div>
</div>
 </div>
 
</div>
 
<div>
 
 <div>
<p>No worries, Agreed with the above answers:</p>
<p>But for Private project, please follow the steps for Gitlab:</p>
<ol>
<li>Login to your account</li>
<li>Click on Settings -&gt; General</li>
<li>Select your Repository (that you wants to delete)</li>
<li>Click on 'Advanced' on the bottom-most</li>
<li>Click on 'Remove Project'</li>
<li><p>You will be asked to type your project name</p>
<p>This action can lead to data loss. To prevent accidental actions we ask you to confirm your intention. 
Please type 'sample_project' to proceed or close this modal to cancel.</p></li>
<li><p>Now your project is deleted successfully.</p></li>
</ol>
 </div>
 <div>
 <p>
 answered Aug 9, 2019 at 6:28
 </p>
 <div>
 <a href="/users/5778347/deepak-keynes"><p></p></a>
 </div>
 <div>
 <p><a href="/users/5778347/deepak-keynes">Deepak Keynes</a>Deepak Keynes</p><p>2,1615 gold badges26 silver badges55 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <div>
<p>In windows:</p>
<ol>
<li>Press Start Button</li>
<li>Search Resource Monitor</li>
<li>Under CPU Tab -&gt; type .git -&gt; right click rundll32 and end process</li>
</ol>
<p>Now you can delete .git folder</p>
 </div>
 <div>
 
 <div>
 
 <div>
 <a href="/users/7311767/stephen-rauch"><p></p></a>
 </div>
 <div>
 <p><a href="/users/7311767/stephen-rauch">Stephen Rauch</a>♦</p><p>46.7k31 gold badges109 silver badges131 bronze badges
 </p>
 </div>
</div>
 <div>
 <p>
 answered Nov 8, 2017 at 4:51
 </p>
 <div>
 <a href="/users/4915855/sahil-khurana"><p></p></a>
 </div>
 
</div>
 </div>
 
</div>
 
<div>
 
 <div>
<p><strong>Windows cmd prompt:</strong>
(You could try the below command directly in <strong>windows cmd</strong> if you are not comfortable with <strong>grep, rm -rf, find, xargs etc.,</strong> commands in <strong>git bash</strong> )</p>
<p>Delete <strong><em>.git</em></strong> recursively inside the project folder by the following command in cmd: </p>
<blockquote>
 <p>FOR /F "tokens=*" %G IN ('DIR /B /AD /S <em>.git</em>') DO RMDIR /S /Q "%G"</p>
</blockquote>
 </div>
 <div>
 <p>
 answered Jan 29, 2018 at 16:38
 </p>
 <div>
 <a href="/users/1308589/sridharkritha"><p></p></a>
 </div>
 <div>
 <p><a href="/users/1308589/sridharkritha">SridharKritha</a>SridharKritha</p><p>7,7172 gold badges45 silver badges41 bronze badges
 </p>
 </div>
</div>
 
</div>
 
<div>
 
 <p>true,like mine was stored in USERS,so had to open USERS go to View on you upper left find Options,open it and edit folders'view options in view still to display hidden files/folders,all your folders will be displayed and you can deleted the repo manually,remember to hide the files/folders once done with the delete. </p>
 <div>
 <p>
 answered Jan 25, 2019 at 1:12
 </p>
 <div>
 <a href="/users/10964933/shema05"><p></p></a>
 </div>
 
</div>
 
</div>
 
 <h2>
 
 </h2>
 </div>
 </div>||||I|||| Stack Overflow
 1. About
 2. Products
 3. For Teams
 1. Stack Overflow Public questions & answers
 2. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
 3. Talent Build your employer brand
 4. Advertising Reach developers & technologists worldwide
 5. About the company
 Loading…

 1. current community

 + Stack Overflow
 help chat
 + Meta Stack Overflow

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 2. 
 3. Log in
 4. Sign up
 1. Home
 2. 
 1. Public
 2. Questions
 3. Tags
 4. Users
 5. Companies
 6. Collectives
 7. Explore Collectives
 3. 
 1. Teams
 Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
 2. Teams
 3. Create free Team

 Collectives™ on Stack Overflow

 Find centralized, trusted content and collaborate around the technologies you use most.

 Learn more about Collectives

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 How to fully delete a git repository created with init?

 Ask Question
 Asked 13 years, 6 months ago
 Modified 1 year, 2 months ago
 Viewed 1.7m times
 1629

 I created a git repository with git init. I'd like to delete it entirely and init a new one.

 * git
 * git-init
 Share
 Improve this question
 Follow
 edited Sep 13, 2016 at 22:48
 jww
 95k 88 88 gold badges 396 396 silver badges 860 860 bronze badges
 asked Jul 31, 2009 at 16:06
 user105813 user105813
 16.8k 4 4 gold badges 20 20 silver badges 17 17 bronze badges
 0
 Add a comment |

 14 Answers

 Sorted by: Reset to default
 Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
 2048

 Git keeps all of its files in the .git directory. Just remove that one and init again.

 If you can't find it, it's because it is hidden.

 * In Windows 7, you need to go to your folder, click on Organize on the top left, then click on Folder and search options, then click on the View tab and click on the Show hidden files, folders and drives radio button.

 * On a Mac OS:

 + Open a Terminal (via Spotlight: press CMD + SPACE, type terminal and press Enter) and run:

 defaults write com.apple.finder AppleShowAllFiles 1 && killall Finder
 

 Note: The keyboard shortcut to show hidden files in Finder is CMD + SHIFT + . so it is no longer necessary to modify the finder config this way

 + You could also type cd (the space is important), drag and drop your git repo folder from Finder to the terminal window, press return, then type rm -fr .git, then return again.

 * On Ubuntu, use shortcut Ctrl + H.

 Share
 Improve this answer
 Follow
 edited Sep 4, 2019 at 13:49
 double-beep
 4,857 16 16 gold badges 32 32 silver badges 41 41 bronze badges
 answered Jul 31, 2009 at 16:08
 Kristof Provost Kristof Provost
 25.8k 2 2 gold badges 24 24 silver badges 28 28 bronze badges
 6
 * 25
 Note: After you have removed the hidden .git file, you should enter defaults write com.apple.finder AppleShowAllFiles NO && killall Finder to hide the hidden files once again.
 – CodeBiker
 May 29, 2016 at 1:12
 * 22
 To show/hide files on macOS (in Finder): cmd + shift + . This was a top result on Google for me so people will probably find it a lot even though it's old.
 – ludvigeriksson
 May 8, 2017 at 12:54
 * 12
 @Van those are bash flags (options), and they can go in whatever order you'd like :)
 – De Novo
 Mar 4, 2018 at 20:00
 * That solution was not enough when I added an iOS project as submodule. It appeared as a red folder with "iOSapp @ 1d12345" that couldn't be accesses nor could I push new changes (it said always it matches master already). I used the answer with "find . -type f | grep -i "\.git" | xargs rm" that removed whatever created the problem and now it works.
 – André
 Dec 10, 2020 at 7:32
 * Hi I just do it accidentally and all my work vanishes. Is there a way to undo this?
 – Abdullah Saud
 Apr 27, 2021 at 11:27
 | Show 1 more comment
 1005

 If you really want to remove all of the repository, leaving only the working directory then it should be as simple as this.

 rm -rf .git
 

 The usual provisos about rm -rf apply. Make sure you have an up to date backup and are absolutely sure that you're in the right place before running the command. etc., etc.

 Share
 Improve this answer
 Follow
 answered Jul 31, 2009 at 16:08
 CB Bailey CB Bailey
 733k 101 101 gold badges 626 626 silver badges 651 651 bronze badges
 5
 * 14
 And you can use this on Windows too, in git shell
 – Hammad Khan
 Nov 14, 2018 at 15:37
 * 1
 It repeatedly says it can't: rm: cannot remove 'Rift/.git/objects/pack': Directory not empty , and yes I ran rm -rf
 – Vendetta
 Nov 16, 2020 at 3:23
 * 1
 Side note, -rf is two options joined together: -r for recursive removal, -f to force the action
 – nCardot
 Apr 21, 2021 at 15:39
 * Hi I just do it accidentally and all my work vanishes. Is there a way to undo this?
 – Abdullah Saud
 Apr 27, 2021 at 11:27
 * @Abdullah Saud No way you could undo it when running that command. The warning should be put earlier.
 – MaXi32
 Jan 14 at 4:49
 Add a comment |
 70

 If you want to delete all .git folders in a project use the following command:

 find . -type f | grep -i "\.git" | xargs rm
 

 This will also delete all the .git folders and .gitignore files from all subfolders

 Share
 Improve this answer
 Follow
 edited Feb 6, 2014 at 15:15
 answered Apr 16, 2012 at 9:25
 ejazz ejazz
 2,458 1 1 gold badge 19 19 silver badges 29 29 bronze badges
 2
 * 11
 This deleted my french.gite.txt
 – Anonymous Coward
 Jan 31, 2019 at 13:36
 * Had issues with syncing an iOS project as submodule. Just removing .git directory didn't work somehow, this solved it though. Probably some more files hidden in subfolders?
 – André
 Dec 10, 2020 at 7:29
 Add a comment |
 28

 after cloning the repo

 cd /repo folder/
 

 to go to the file directory then

 ls -a
 

 to see all files hidden and unhidden

 .git .. .gitignore .etc
 

 if you like you can check the repo origin

 git remote -v
 

 now delete .git which contains everything about git

 rm -rf .git
 

 after deleting, you would discover that there is no git linked check remote again

 git remote -v
 

 now you can init your repository with

 git init
 git add README.md
 git commit -m "first commit"
 git remote add origin https://github.com/Leonuch/flex.git
 git push -u origin main
 
 Share
 Improve this answer
 Follow
 edited Nov 24, 2020 at 9:52
 Anatoly
 19.4k 3 3 gold badges 25 25 silver badges 41 41 bronze badges
 answered Oct 13, 2019 at 21:10
 leouonline leouonline
 298 3 3 silver badges 5 5 bronze badges
 1
 * 2
 This is a duplicate of an existing answer . All the other steps add nothing to the answer.
 – John Kugelman
 Apr 4, 2020 at 0:49
 Add a comment |
 16

 Alternative to killing TortoiseGit:

 * Open the TortoiseGit-Settings (right click to any folder, TortoiseGit → Settings)
 * Go to the Icon Overlays option.
 * Change the Status Cache from Default to None
 * Now you can delete the directory (either with Windows Explorer or rmdir /S /Q)
 * Set back the Status Cache from None to Default and you should be fine again...
 Share
 Improve this answer
 Follow
 edited Mar 10, 2013 at 18:50
 11684
 7,346 12 12 gold badges 47 47 silver badges 70 70 bronze badges
 answered Nov 30, 2010 at 9:51
 eckes eckes
 62.8k 28 28 gold badges 169 169 silver badges 198 198 bronze badges
 Add a comment |
 11

 Where $GIT_DIR is the path to the folder to be searched (the git repo path), execute the following in terminal.

 find $GIT_DIR -name *.git* -ok rm -Rf {} \;
 

 This will recursively search for any directories or files containing ".git" in the file/directory name within the specified Git directory. This will include .git/ and .gitignore files and any other .git-like assets. The command is interactive and will ask before removing. To proceed with the deletion, simply enter y, then Enter.

 Share
 Improve this answer
 Follow
 answered Aug 14, 2012 at 17:06
 Scorpius Scorpius
 999 1 1 gold badge 10 10 silver badges 22 22 bronze badges
 0
 Add a comment |
 5

 To fully delete the .git repository in your computer (in Windows 8 and above):

 1. The .git repository is normally hidden in windows
 2. So you need to mark the "hidden items" to show the hidden folders
 3. At the top site of you directory you find "view" option
 4. Inside "view" option you find "hidden items" and mark it
 5. Then you see the .git repository then you can delete it
 Share
 Improve this answer
 Follow
 edited Aug 7, 2017 at 16:50
 Paul Roub
 36.2k 27 27 gold badges 82 82 silver badges 89 89 bronze badges
 answered Aug 7, 2017 at 16:45
 squal squal
 185 3 3 silver badges 14 14 bronze badges
 Add a comment |
 4

 I tried:

 rm -rf .git and also

 Git keeps all of its files in the .git directory. Just remove that one and init again.

 Neither worked for me. Here's what did:

 * Delete all files except for .git
 * git add . -A
 * git commit -m "deleted entire project"
 * git push

 Then create / restore the project from backup:

 * Create new project files (or copy paste a backup)
 * git add . -A
 * git commit -m "recreated project"
 * git push
 Share
 Improve this answer
 Follow
 answered Feb 4, 2015 at 18:24
 P.Brian.Mackey P.Brian.Mackey
 42.6k 65 65 gold badges 231 231 silver badges 344 344 bronze badges
 0
 Add a comment |
 4

 you can use :

 git remote remove origin

 to remove a linked repo then:

 git remote add origin

 to add new one

 Share
 Improve this answer
 Follow
 answered Jan 28, 2021 at 9:39
 MonirRouissi MonirRouissi
 503 7 7 silver badges 7 7 bronze badges
 1
 * 1
 this will not remove the repo from local computer.
 – samkart
 Feb 9, 2021 at 11:29
 Add a comment |
 3

 You can create an alias for it. I am using ZSH shell with Oh-my-Zsh and here is an handy alias:

 # delete and re-init git
 # usage: just type 'gdelinit' in a local repository
 alias gdelinit="trash .git && git init"
 

 I am using Trash to trash the .git folder since using rm is really dangerous:

 trash .git
 

 Then I am re-initializing the git repo:

 git init
 
 Share
 Improve this answer
 Follow
 edited Dec 2, 2015 at 14:37
 spongebob
 8,137 14 14 gold badges 48 48 silver badges 83 83 bronze badges
 answered Dec 2, 2015 at 8:21
 Ahmad Awais Ahmad Awais
 31.5k 5 5 gold badges 74 74 silver badges 55 55 bronze badges
 Add a comment |
 1

 No worries, Agreed with the above answers:

 But for Private project, please follow the steps for Gitlab:

 1. Login to your account
 2. Click on Settings -> General
 3. Select your Repository (that you wants to delete)
 4. Click on 'Advanced' on the bottom-most
 5. Click on 'Remove Project'

 6. You will be asked to type your project name

 This action can lead to data loss. To prevent accidental actions we ask you to confirm your intention. Please type 'sample_project' to proceed or close this modal to cancel.

 7. Now your project is deleted successfully.

 Share
 Improve this answer
 Follow
 answered Aug 9, 2019 at 6:28
 Deepak Keynes Deepak Keynes
 2,161 5 5 gold badges 26 26 silver badges 55 55 bronze badges
 Add a comment |
 0

 In windows:

 1. Press Start Button
 2. Search Resource Monitor
 3. Under CPU Tab -> type .git -> right click rundll32 and end process

 Now you can delete .git folder

 Share
 Improve this answer
 Follow
 edited Nov 8, 2017 at 5:14
 Stephen Rauch ♦
 46.7k 31 31 gold badges 109 109 silver badges 131 131 bronze badges
 answered Nov 8, 2017 at 4:51
 sahil khurana sahil khurana
 49 2 2 bronze badges
 Add a comment |
 0

 Windows cmd prompt: (You could try the below command directly in windows cmd if you are not comfortable with grep, rm -rf, find, xargs etc., commands in git bash )

 Delete .git recursively inside the project folder by the following command in cmd:

 FOR /F "tokens=*" %G IN ('DIR /B /AD /S .git') DO RMDIR /S /Q "%G"

 Share
 Improve this answer
 Follow
 edited Jan 29, 2018 at 16:49
 answered Jan 29, 2018 at 16:38
 SridharKritha SridharKritha
 7,717 2 2 gold badges 45 45 silver badges 41 41 bronze badges
 Add a comment |
 -2

 true,like mine was stored in USERS,so had to open USERS go to View on you upper left find Options,open it and edit folders'view options in view still to display hidden files/folders,all your folders will be displayed and you can deleted the repo manually,remember to hide the files/folders once done with the delete.

 Share
 Improve this answer
 Follow
 answered Jan 25, 2019 at 1:12
 Shema05 Shema05
 1
 Add a comment |
 Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

 Not the answer you're looking for? Browse other questions tagged
 * git
 * git-init
 or ask your own question.

 * The Overflow Blog
 * Announcing more ways to learn and grow your skills
 * Why developer experience is the key to better software, straight from the...
 * Featured on Meta
 * Accessibility Update: Colors
 * 2022: a year in moderation
 * Collectives: The next iteration
 * We’re bringing advertisements for technology courses to Stack Overflow
 * Temporary policy: ChatGPT is banned

 Linked

 452
 How do I delete a local repository in git?
 16
 How to delete my repository on Git using command line?
 -1
 How to undo git folder I created because of mistake?
 0
 How do I delete every change to my local git repository?
 0
 Removing untracked files from git
 0
 How to remove Visual Studio solution from Git source control
 0
 Remove Unintended Git Tracking
 0
 Accidentally made my whole C drive a local git repository
 930
 Is there a command to undo git init?
 12
 removing local git repositary from Xcode Project?
 See more linked questions

 Related

 10875
 How do I undo 'git add' before commit?
 25328
 How do I undo the most recent local commits in Git?
 9163
 How do I force "git pull" to overwrite local files?
 6231
 Move the most recent commit(s) to a new branch with Git
 19892
 How do I delete a Git branch locally and remotely?
 2435
 How do I delete a file from a Git repository?
 5766
 How do I change the URI (URL) for a remote Git repository?
 5420
 How do I push a new local branch to a remote Git repository and track it too?
 7613
 How do I revert a Git repository to a previous commit?
 11078
 How do I rename a local Git branch?

 Hot Network Questions

 * Are there any Oxford Colleges which will accommodate a family including children
 * Is this Stanford certificate real?
 * Why are none of these chips identifiable?
 * Why are there 23 members of the grand jury?
 * Why does Ontario have a quota on milk production?
 * Lightning Field Labels displayed Inline for some Users
 * GFCI Light Is On, Test Button Doesn't Work
 * In an underdetermined linear regression setup, why does the residual sum of squares equal 0?
 * Slings for giant
 * What is the point of Feinting from a distance?
 * Agile Principles: What does it mean sustainable development?
 * Polynomial approximation of max function
 * Crashing the sha1() function in PHP?
 * Idiomatic guitar notation; notating L.V. passages?
 * Why does faster cooling produce smaller grains?
 * Why are atoms electrically neutral?
 * Compute "negative" version of vector of vectors
 * Why do Pokemon sometimes have dark stripes in Pokemon Go?
 * Can a vampire rest in a coffin in a bag of holding?
 * Anydice: "highest 1 of 3d6 1d12" function. Is this correct?
 * Why is the solstice the first day of summer, and not the central day?
 * How do we use lowercase `\mathcal` or `\mathscr` in latex overleaf?
 * Can a helicopter's main rotor mince a human?
 * Computers lose power, lights dim. I suspect the mains power supply; is there anything I can do?
 more hot questions
 Question feed

 Subscribe to RSS

 Question feed

 To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

 Stack Overflow

 * Questions
 * Help

 Products

 * Teams
 * Advertising
 * Collectives
 * Talent

 Company

 * About
 * Press
 * Work Here
 * Legal
 * Privacy Policy
 * Terms of Service
 * Contact Us
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.2.6.43220

 Your privacy

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

 Accept all cookies Customize settings
I created a git repository with git init. I'd like to delete it entirely and init a new one.
 
 
 
 
 
 
    
 
 
  jww  95k88 gold badges396 silver badges860 bronze badges
 
 
 
 
 asked Jul 31, 2009 at 16:06
 
 
    
 
 
 
 
 0
 
 
 
 
 
 
 
 
Git keeps all of its files in the .git directory. Just remove that one and init again.
If you can't find it, it's because it is hidden.
 In Windows 7, you need to go to your folder, click on Organize on the top left, then click on Folder and search options, then click on the View tab and click on the Show hidden files, folders and drives radio button. 
 On a Mac OS:
 Open a Terminal (via Spotlight: press CMD + SPACE, type terminal and press Enter) and run:
 defaults write com.apple.finder AppleShowAllFiles 1 &amp;&amp; killall Finder
 
Note: The keyboard shortcut to show hidden files in Finder is 
CMD + SHIFT + . so it is no longer necessary to modify the 
finder config this way 
 You could also type cd (the space is important), drag and drop your git repo folder from Finder to the terminal window, press return, then type rm -fr .git, then return again. 
 
 On Ubuntu, use shortcut Ctrl + H. 
 
 
 
 
 
 
    
 
 
  double-beep  4,85716 gold badges32 silver badges41 bronze badges
 
 
 
 
 answered Jul 31, 2009 at 16:08
 
 
    
 
 
  Kristof ProvostKristof Provost 25.8k2 gold badges24 silver badges28 bronze badges
 
 
 
 
 6
 
 
 
 
 
 
If you really want to remove all of the repository, leaving only the working directory then it should be as simple as this.
 rm -rf .git
 
The usual provisos about rm -rf apply. Make sure you have an up to date backup and are absolutely sure that you're in the right place before running the command. etc., etc.
 
 
 
 answered Jul 31, 2009 at 16:08
 
 
    
 
 
  CB BaileyCB Bailey 733k101 gold badges626 silver badges651 bronze badges
 
 
 
 5
 
 
 
 
 
 
If you want to delete all .git folders in a project use the following command:
 find . -type f | grep -i "\.git" | xargs rm
 
This will also delete all the .git folders and .gitignore files from all subfolders
 
 
 
 answered Apr 16, 2012 at 9:25
 
 
    
 
 
  ejazzejazz 2,4581 gold badge19 silver badges29 bronze badges
 
 
 
 2
 
 
 
 
 
 
after cloning the repo
 cd /repo folder/
 
to go to the file directory then
 ls -a
 
to see all files hidden and unhidden
 .git .. .gitignore .etc
 
if you like you can check the repo origin
 git remote -v
 
now delete .git which contains everything about git
 rm -rf .git
 
after deleting, you would discover that there is no git linked check remote again
 git remote -v
 
now you can init your repository with
 git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/Leonuch/flex.git
git push -u origin main
 
 
 
 
 
 
 
    
 
 
  Anatoly  19.4k3 gold badges25 silver badges41 bronze badges
 
 
 
 
 answered Oct 13, 2019 at 21:10
 
 
    
 
 
  leouonlineleouonline 2983 silver badges5 bronze badges
 
 
 
 
 1
 
 
 
 
Alternative to killing TortoiseGit:
Open the TortoiseGit-Settings (right click to any folder, TortoiseGit → Settings)
Go to the Icon Overlays option. 
Change the Status Cache from Default to None 
Now you can delete the directory (either with Windows Explorer or rmdir /S /Q)
Set back the Status Cache from None to Default and you should be fine again...
 
 
 
 
 
 
    
 
 
  11684  7,34612 gold badges47 silver badges70 bronze badges
 
 
 
 
 answered Nov 30, 2010 at 9:51
 
 
    
 
 
  eckeseckes 62.8k28 gold badges169 silver badges198 bronze badges
 
 
 
 
 
 
 
 
 
Where $GIT_DIR is the path to the folder to be searched (the git repo path), execute the following in terminal.
 find $GIT_DIR -name *.git* -ok rm -Rf {} \;
 
This will recursively search for any directories or files containing ".git" in the file/directory name within the specified Git directory. This will include .git/ and .gitignore files and any other .git-like assets. The command is interactive and will ask before removing. To proceed with the deletion, simply enter y, then Enter.
 
 
 
 answered Aug 14, 2012 at 17:06
 
 
    
 
 
  ScorpiusScorpius 9991 gold badge10 silver badges22 bronze badges
 
 
 
 0
 
 
 
 
To fully delete the .git repository in your computer (in Windows 8 and above):
The .git repository is normally hidden in windows 
So you need to mark the "hidden items" to show the hidden folders 
At the top site of you directory you find "view" option 
Inside "view" option you find "hidden items" and mark it 
Then you see the .git repository then you can delete it
 
 
 
 
 
 
    
 
 
  Paul Roub  36.2k27 gold badges82 silver badges89 bronze badges
 
 
 
 
 answered Aug 7, 2017 at 16:45
 
 
    
 
 
  squal squal  1853 silver badges14 bronze badges
 
 
 
 
 
 
 
 
 
I tried:
 rm -rf .git and also 
 Git keeps all of its files in the .git directory. Just remove that one
 and init again.
Neither worked for me. Here's what did:
Delete all files except for .git 
git add . -A
git commit -m "deleted entire project"
git push
Then create / restore the project from backup:
Create new project files (or copy paste a backup)
git add . -A
git commit -m "recreated project"
git push
 
 
 
 answered Feb 4, 2015 at 18:24
 
 
    
 
 
  P.Brian.MackeyP.Brian.Mackey 42.6k65 gold badges231 silver badges344 bronze badges
 
 
 
 0
 
 
 
 
 
 
you can use :
git remote remove origin
to remove a linked repo then:
git remote add origin 
to add new one
 
 
 
 answered Jan 28, 2021 at 9:39
 
 
    
 
 
 
 1
 
 
 
 
You can create an alias for it. I am using ZSH shell with Oh-my-Zsh and here is an handy alias:
 # delete and re-init git
# usage: just type 'gdelinit' in a local repository
alias gdelinit="trash .git &amp;&amp; git init"
 
I am using Trash to trash the .git folder since using rm is really dangerous:
 trash .git
 
Then I am re-initializing the git repo:
 git init
 
 
 
 
 
 
 
    
 
 
  spongebob  8,13714 gold badges48 silver badges83 bronze badges
 
 
 
 
 answered Dec 2, 2015 at 8:21
 
 
    
 
 
  Ahmad AwaisAhmad Awais 31.5k5 gold badges74 silver badges55 bronze badges
 
 
 
 
 
 
 
No worries, Agreed with the above answers:
But for Private project, please follow the steps for Gitlab:
Login to your account
Click on Settings -&gt; General
Select your Repository (that you wants to delete)
Click on 'Advanced' on the bottom-most
Click on 'Remove Project'
 You will be asked to type your project name
This action can lead to data loss. To prevent accidental actions we ask you to confirm your intention. 
Please type 'sample_project' to proceed or close this modal to cancel. 
 Now your project is deleted successfully. 
 
 
 
 answered Aug 9, 2019 at 6:28
 
 
    
 
 
  Deepak KeynesDeepak Keynes 2,1615 gold badges26 silver badges55 bronze badges
 
 
 
 
 
 
In windows:
Press Start Button
Search Resource Monitor
Under CPU Tab -&gt; type .git -&gt; right click rundll32 and end process
Now you can delete .git folder
 
 
 
 
 
 
    
 
 
  Stephen Rauch♦ 46.7k31 gold badges109 silver badges131 bronze badges
 
 
 
 
 answered Nov 8, 2017 at 4:51
 
 
    
 
 
 
 
 
 
 
 Windows cmd prompt:
(You could try the below command directly in windows cmd if you are not comfortable with grep, rm -rf, find, xargs etc., commands in git bash )
Delete  .git  recursively inside the project folder by the following command in cmd: 
 FOR /F "tokens=*" %G IN ('DIR /B /AD /S .git') DO RMDIR /S /Q "%G"
 
 
 
 answered Jan 29, 2018 at 16:38
 
 
    
 
 
  SridharKrithaSridharKritha 7,7172 gold badges45 silver badges41 bronze badges
 
 
 
 
 
 true,like mine was stored in USERS,so had to open USERS go to View on you upper left find Options,open it and edit folders'view options in view still to display hidden files/folders,all your folders will be displayed and you can deleted the repo manually,remember to hide the files/folders once done with the delete. 
 
 
 answered Jan 25, 2019 at 1:12
How to fully delete a git repository created with init?
https://stackoverflow.com/questions/1213430/how-to-fully-delete-a-git-repository-created-with-init
https://stackoverflow.com/questions/27868636/how-can-i-delete-a-remote-git-repository-from-the-command-line-git-bash
How can I delete a remote Git repository from the command line (Git Bash)?
2015-07-07
Dahiana Dahiana 1
<div><div>
<div>
<div>
<div>
<p><strong>I'm trying to remove a remote git repository through Git Bash.</strong> I'm aware that I can just delete it through GitHub; however, I want to learn how to do so through the command line. I don't just want to remove the files in it, or replace it, I want to completely delete it. I spent the last two days sifting through forums, articles, blogs and tutorials, and nothing has worked.</p>
<p><strong>Some initial info:</strong></p>
<pre><code>$ git remote -v
thisbranch https://github.com/thisuser/test-repo.git (fetch)
thisbranch https://github.com/thisuser/test-repo.git (push)
$ git status
On branch master
nothing to commit, working directory clean
$ git log
Author: *info*
Date: *info*
 adding a new file
Author: *info*
Date: *info*
 Initial commit
$ git remote -v show thisbranch
* remote thisbranch
 Fetch URL: https://github.com/thisuser/test-repo.git
 Push URL: https://github.com/thisuser/test-repo.git
 HEAD branch: master
 Remote branch:
 master tracked
 Local ref configured for 'git push':
 master pushes to master (up to date)
</code></pre>
<p><strong>Some things I tried:</strong></p>
<pre><code>$ git remote remove https://github.com/thisuser/test-repo.git
error: Could not remove config section 'remote.https://github.com/thisuser/test-repo.git'
$ git remote remove master
error: Could not remove config section 'remote.master'
$ git remote remove thisbranch
*returns nothing*
$ git remote -v
*returns nothing*
</code></pre>
<p>I've realized I also never get returned the name 'origin'.</p>
</div>
<div>
<div>
<a href="https://stackoverflow.com/users/4370109/brian-tompsett-%e6%b1%a4%e8%8e%b1%e6%81%a9"><p></p></a>
</div>
<div>
<div>
<p>
 asked 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/4437786/dahiana"><p></p></a>
</div>
</div>
</div>
</div>
<p></p>
</div>
<div>
<div>
<div>
<div>
<p>As pointed out by BrokenBinary in <a href="https://stackoverflow.com/questions/27868636/how-to-remove-a-git-repository/27868996#comment44139745_27868636">his comment</a>,</p>
<blockquote>
<p>This can't be done.</p>
</blockquote>
<p>You cannot delete a remote repo (hosted on GitHub or elsewhere) with Git commands, be it in Git Bash or elsewhere, period. You can <a href="https://stackoverflow.com/questions/2003505/delete-a-git-branch-both-locally-and-remotely">delete remote branches</a> (assuming you have write access to them), yes, but not an entire remote repository.</p>
<p>However, the <a href="https://developer.github.com/v3/repos/#delete-a-repository">GitHub Developer API</a> allows you to delete, at the command line, a repo hosted on GitHub.</p>
</div>
<div>
<div>
<p>
 answered 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/2541573/jub0bs"><p></p></a>
</div>
<div>
<p><a href="https://stackoverflow.com/users/2541573/jub0bs">jub0bs</a></p><p>
</p>
</div>
</div>
</div>
<p></p>
</div>
<div>
<div>
<p>As Jubobs mentioned above you can use github dev API, so:
<code>curl -u :username -X "DELETE" https://api.github.com/repos/:username/:repo</code>
Where <strong>:username</strong> = your username(github handler) and <strong>:repo</strong> = a name of a repo you want to get rid of</p>
<div>
<div>
<p>
 answered 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/4814774/dago"><p></p></a>
</div>
<div>
<p><a href="https://stackoverflow.com/users/4814774/dago">daGo</a></p><p>
</p>
</div>
</div>
</div>
<p></p>
</div>
<div>
<div>
<p>First, switch to sudo mode:</p>
<pre><code>gh auth refresh -h github.com -s delete_repo
</code></pre>
<p>After the signup, then you can:</p>
<pre><code>gh repo delete
</code></pre>
<p><strong>Note</strong>: This only deletes the remote repository. To delete it locally, go to the parent directory and use the command <code>rm -r dirname</code>; replace <code>dirname</code> with the name of your directory.</p>
</div>
<div>
<div>
<div>
<a href="https://stackoverflow.com/users/3025856/jeremy-caney"><p></p></a>
</div>
<div>
<p><a href="https://stackoverflow.com/users/3025856/jeremy-caney">Jeremy Caney</a></p><p>
</p>
</div>
</div>
<div>
<div>
<p>
 answered 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/19078158/sixtus-agbo"><p></p></a>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<p>Yes, you can delete the repository.</p>
<p>First we need to add repository:</p>
<pre><code>git remote add origin &lt;repository link&gt;
</code></pre>
<p>If we want to delete repository:</p>
<pre><code>git remote remove origin 
</code></pre>
<p>then press enter button</p>
</div>
<div>
<div>
<a href="https://stackoverflow.com/users/4294399/general-grievance"><p></p></a>
</div>
<div>
<div>
<p>
 answered 
</p>
</div>
<div>
<a href="https://stackoverflow.com/users/22062393/alam-vinod"><p></p></a>
</div>
</div>
</div>
</div>
<p></p>
</div>
<h2>
</h2>
</div>
</div></div>||||I|||| Join Stack Overflow

 By clicking “Sign up”, you agree to our terms of service and acknowledge you have read our privacy policy.

 Sign up with Google
 Sign up with GitHub

 OR

 Email
 Password
 Sign up

 Already have an account? Log in
Skip to main content
 Stack Overflow
 1. About
 2. Products
 3. For Teams
 1. Stack Overflow Public questions & answers
 2. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
 3. Talent Build your employer brand
 4. Advertising Reach developers & technologists worldwide
 5. Labs The future of collective knowledge sharing
 6. About the company
 Loading…

 1. current community

 + Stack Overflow
 help chat
 + Meta Stack Overflow

 your communities

 Sign up or log in to customize your list.

 more stack exchange communities

 company blog
 2. 
 3. Log in
 4. Sign up
 1. 
 1. Home
 2. Questions
 3. Tags
 4. 
 5. Users
 6. Companies
 7. Labs
 8. Discussions
 9. Collectives

 10. Communities for your favorite technologies. Explore all Collectives

 2. Teams

 Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

 Explore Teams Create a free Team
 3. Teams

 4. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

 Collectives™ on Stack Overflow

 Find centralized, trusted content and collaborate around the technologies you use most.

 Learn more about Collectives

 Teams

 Q&A for work

 Connect and share knowledge within a single location that is structured and easy to search.

 Learn more about Teams

 Get early access and see previews of new features.

 Learn more about Labs

 How can I delete a remote Git repository from the command line (Git Bash)?

 Ask Question
 Asked 9 years, 3 months ago
 Modified 3 months ago
 Viewed 31k times
 This question shows research effort; it is useful and clear
 17
 Save this question.
 Show activity on this post.

 I'm trying to remove a remote git repository through Git Bash. I'm aware that I can just delete it through GitHub; however, I want to learn how to do so through the command line. I don't just want to remove the files in it, or replace it, I want to completely delete it. I spent the last two days sifting through forums, articles, blogs and tutorials, and nothing has worked.

 Some initial info:

 $ git remote -v
 thisbranch https://github.com/thisuser/test-repo.git (fetch)
 thisbranch https://github.com/thisuser/test-repo.git (push)
 
 $ git status
 On branch master
 nothing to commit, working directory clean
 
 $ git log
 Author: *info*
 Date: *info*
 adding a new file
 Author: *info*
 Date: *info*
 Initial commit
 
 $ git remote -v show thisbranch
 * remote thisbranch
 Fetch URL: https://github.com/thisuser/test-repo.git
 Push URL: https://github.com/thisuser/test-repo.git
 HEAD branch: master
 Remote branch:
 master tracked
 Local ref configured for 'git push':
 master pushes to master (up to date)
 

 Some things I tried:

 $ git remote remove https://github.com/thisuser/test-repo.git
 error: Could not remove config section 'remote.https://github.com/thisuser/test-repo.git'
 
 $ git remote remove master
 error: Could not remove config section 'remote.master'
 
 $ git remote remove thisbranch
 *returns nothing*
 
 $ git remote -v
 *returns nothing*
 

 I've realized I also never get returned the name 'origin'.

 * git
 * github
 * repository
 Share
 Share a link to this question
 Copy linkCC BY-SA 3.0
 Improve this question
 Follow
 Follow this question to receive notifications
 edited Jul 7, 2015 at 12:32
 Brian Tompsett - 汤莱恩
 5,817 72 72 gold badges 60 60 silver badges 131 131 bronze badges
 asked Jan 9, 2015 at 20:23
 dahiana dahiana
 1,231 2 2 gold badges 10 10 silver badges 11 11 bronze badges
 1
 * In Oct. 2021, there is now gh repo delete : see my answer below .
 – VonC
 Oct 26, 2021 at 6:04
 Add a comment |

 5 Answers

 Sorted by: Reset to default
 Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
 This answer is useful
 14
 Save this answer.
 Show activity on this post.

 As pointed out by BrokenBinary in his comment,

 This can't be done.

 You cannot delete a remote repo (hosted on GitHub or elsewhere) with Git commands, be it in Git Bash or elsewhere, period. You can delete remote branches (assuming you have write access to them), yes, but not an entire remote repository.

 However, the GitHub Developer API allows you to delete, at the command line, a repo hosted on GitHub.

 Share
 Share a link to this answer
 Copy linkCC BY-SA 4.0
 Improve this answer
 Follow
 Follow this answer to receive notifications
 edited Jan 16 at 5:36
 answered Jan 9, 2015 at 20:48
 jub0bs jub0bs
 63.7k 26 26 gold badges 188 188 silver badges 191 191 bronze badges
 0
 Add a comment |
 This answer is useful
 8
 Save this answer.
 Show activity on this post.

 As Jubobs mentioned above you can use github dev API, so: curl -u :username -X "DELETE" https://api.github.com/repos/:username/:repo Where :username = your username(github handler) and :repo = a name of a repo you want to get rid of

 Share
 Share a link to this answer
 Copy linkCC BY-SA 3.0
 Improve this answer
 Follow
 Follow this answer to receive notifications
 answered Oct 3, 2017 at 20:33
 daGo daGo
 2,802 30 30 silver badges 24 24 bronze badges
 0
 Add a comment |
 This answer is useful
 1
 Save this answer.
 Show activity on this post.

 This now (Oct. 2021) can be done from command-line, using gh repo delete, added in the GitHub CLI gh 2.2.0

 This is from the feature request 3625 (Add feature to delete a repository from command line), fixed in PR 4451, and commit 5a3b9ce.

 It does wrap the curl -u :username -X "DELETE" https://api.github.com/repos/:username/:repo mentioned in daGo's 2017 answer.

 Share
 Share a link to this answer
 Copy linkCC BY-SA 4.0
 Improve this answer
 Follow
 Follow this answer to receive notifications
 answered Oct 26, 2021 at 6:03
 VonC VonC
 1
 Add a comment |
 This answer is useful
 1
 Save this answer.
 Show activity on this post.

 First, switch to sudo mode:

 gh auth refresh -h github.com -s delete_repo
 

 After the signup, then you can:

 gh repo delete
 

 Note: This only deletes the remote repository. To delete it locally, go to the parent directory and use the command rm -r dirname; replace dirname with the name of your directory.

 Share
 Share a link to this answer
 Copy linkCC BY-SA 4.0
 Improve this answer
 Follow
 Follow this answer to receive notifications
 edited Jun 13, 2022 at 5:18
 Jeremy Caney
 7,419 87 87 gold badges 53 53 silver badges 80 80 bronze badges
 answered Jun 12, 2022 at 23:31
 Sixtus Agbo Sixtus Agbo
 23 6 6 bronze badges
 Add a comment |
 This answer is useful
 1
 Save this answer.
 Show activity on this post.

 Yes, you can delete the repository.

 First we need to add repository:

 git remote add origin <repository link>
 

 If we want to delete repository:

 git remote remove origin 
 

 then press enter button

 Share
 Share a link to this answer
 Copy linkCC BY-SA 4.0
 Improve this answer
 Follow
 Follow this answer to receive notifications
 edited Aug 3, 2023 at 12:20
 General Grievance
 4,857 35 35 gold badges 34 34 silver badges 48 48 bronze badges
 answered Jul 13, 2023 at 20:15
 alam vinod alam vinod
 11 1 1 bronze badge
 1
 * This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post ; instead, provide answers that don't require clarification from the asker . - From Review
 – user12256545
 Jul 17, 2023 at 18:25
 Add a comment |

 Your Answer

 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 

 Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

 Thanks for contributing an answer to Stack Overflow!

 * Please be sure to answer the question. Provide details and share your research!

 But avoid …

 * Asking for help, clarification, or responding to other answers.
 * Making statements based on opinion; back them up with references or personal experience.

 To learn more, see our tips on writing great answers.

 Sign up or log in

 Sign up using Google
 Sign up using Facebook
 Sign up using Email and Password
 Submit

 Post as a guest

 Name
 Email

 Required, but never shown

 Post as a guest

 Name
 Email

 Required, but never shown

 Post Your Answer Discard

 By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

 Not the answer you're looking for? Browse other questions tagged
 * git
 * github
 * repository
 or ask your own question.

 * The Overflow Blog
 * Supporting the world’s most-used database engine through 2050
 * Net neutrality is in; TikTok and noncompetes are out
 * Featured on Meta
 * Testing a new version of Stack Overflow Jobs
 * Pausing the 1-rep voting experiment on Stack Overflow: reflecting on the...
 * Temporary policy: Generative AI (e.g., ChatGPT) is banned

 Linked

 2
 Delete remote repo from git using terminal
 1
 Remove a repository with a valid ssh key

 Related

 19
 How can I delete files in the remote repository?
 108
 Git - how delete file from remote repository
 2
 How can I remove everything from a remote Git repository?
 1
 Purge a repository from my Github, using the command line
 54
 How to delete remote repository on github?
 6
 Is it possible to remove a remote repository on Github through the command line?
 0
 How can I clear remote repository in git?
 0
 Delete git repository from server from local command line
 0
 git: can't seem to delete remote
 2
 Removing Repository In Git Bash Without Deleting The Folder

 Hot Network Questions

 * Energy and Power: Power Spectral Density is units of Energy
 * How disadvantageous would symmetrical, four-fingered hands be?
 * Why would solid rubber tubes be no good to some?
 * What is special about the Poisson/Binomial distributions such that they have special regression estimation techniques?
 * Redshift of CMB
 * What is it called when the root is omitted from a chord?
 * Why does Helix Nebula look like an eye?
 * Inner voice when reading mathematics
 * How long can a period be?
 * How to have italicized double-storey "a"?
 * How would I make text bend (as if it's being folded) but not on an isometric grid?
 * How does ls know how to output its results?
 * HST can apparently operate in one-gyro mode, if need be. Why was the Compton Gamma Ray Observatory de-orbited when it lost one of its three gyros?
 * Value of describing game mechanics in a data file instead of hardcoding?
 * What is an undulator (as used for CW)?
 * Why did XML lose out to XHTML, then HTML 5, on the web?
 * Identify the more acidic hydrogen in 1-(methylsulfanyl)propane-2-thione
 * Does weight of an aircraft affect its ability to pull G?
 * Why are random integers generated by multiplying by MAX_SAFE_INTEGER not evenly distributed between odd and even?
 * How to obtain meat when you have no hunters in the colony?
 * Dielectric and dipole
 * Why doesn't b = a; f[a_] := b; f[2] return 2?
 * jacking up a car on "grass paving block" - how to secure jack and supports?
 * Dimension of fibers under continuous maps
 Question feed

 Subscribe to RSS

 Question feed

 To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

 Stack Overflow

 * Questions
 * Help

 Products

 * Teams
 * Advertising
 * Collectives
 * Talent

 Company

 * About
 * Press
 * Work Here
 * Legal
 * Privacy Policy
 * Terms of Service
 * Contact Us
 * Cookie Settings
 * Cookie Policy

 Stack Exchange Network

 * Technology
 * Culture & recreation
 * Life & arts
 * Science
 * Professional
 * Business
 * API
 * Data
 * Blog
 * Facebook
 * Twitter
 * LinkedIn
 * Instagram

 Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2024.4.30.8420

 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
 Accept all cookies Necessary cookies only
 Customize settings
I'm trying to remove a remote git repository through Git Bash. I'm aware that I can just delete it through GitHub; however, I want to learn how to do so through the command line. I don't just want to remove the files in it, or replace it, I want to completely delete it. I spent the last two days sifting through forums, articles, blogs and tutorials, and nothing has worked.
 Some initial info: 
 $ git remote -v
thisbranch https://github.com/thisuser/test-repo.git (fetch)
thisbranch https://github.com/thisuser/test-repo.git (push)
$ git status
On branch master
nothing to commit, working directory clean
$ git log
Author: *info*
Date: *info*
 adding a new file
Author: *info*
Date: *info*
 Initial commit
$ git remote -v show thisbranch
* remote thisbranch
 Fetch URL: https://github.com/thisuser/test-repo.git
 Push URL: https://github.com/thisuser/test-repo.git
 HEAD branch: master
 Remote branch:
 master tracked
 Local ref configured for 'git push':
 master pushes to master (up to date)
 
 Some things I tried: 
 $ git remote remove https://github.com/thisuser/test-repo.git
error: Could not remove config section 'remote.https://github.com/thisuser/test-repo.git'
$ git remote remove master
error: Could not remove config section 'remote.master'
$ git remote remove thisbranch
*returns nothing*
$ git remote -v
*returns nothing*
 
I've realized I also never get returned the name 'origin'.
   
 asked 
   
 
As pointed out by BrokenBinary in his comment,
This can't be done.
You cannot delete a remote repo (hosted on GitHub or elsewhere) with Git commands, be it in Git Bash or elsewhere, period. You can delete remote branches (assuming you have write access to them), yes, but not an entire remote repository.
However, the GitHub Developer API allows you to delete, at the command line, a repo hosted on GitHub.
 answered 
   
 jub0bs  
 
As Jubobs mentioned above you can use github dev API, so:
curl -u :username -X "DELETE" https://api.github.com/repos/:username/:repo
Where :username = your username(github handler) and :repo = a name of a repo you want to get rid of
 answered 
   
 daGo  
 
First, switch to sudo mode:
 gh auth refresh -h github.com -s delete_repo
 
After the signup, then you can:
 gh repo delete
 
 Note: This only deletes the remote repository. To delete it locally, go to the parent directory and use the command rm -r dirname; replace dirname with the name of your directory.
   
 Jeremy Caney  
 answered 
   
Yes, you can delete the repository.
First we need to add repository:
 git remote add origin &lt;repository link&gt;
 
If we want to delete repository:
 git remote remove origin 
 
then press enter button
   
 answered
How can I delete a remote Git repository from the command line (Git Bash)?
https://stackoverflow.com/questions/27868636/how-can-i-delete-a-remote-git-repository-from-the-command-line-git-bash