90

I have a folder under version control. I want to make a copy of it to send around, but don't want to include all the .git directories and the files underneath it.

Is there a way to remove all the git files instead of manually deleting all of them?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user291701
  • 38,411
  • 72
  • 187
  • 285

9 Answers9

100

How to remove all .git directories under a folder in Linux.

Run this find command, it will list all .git directories under the current folder:

find . -type d -name ".git" \
&& find . -name ".gitignore" \
&& find . -name ".gitmodules"

Prints:

./.git
./.gitmodules
./foobar/.git
./footbar2/.git
./footbar2/.gitignore

There should only be like 3 or 4 .git directories because git only has one .git folder for every project. You can rm -rf yourpath each of the above by hand.

If you feel like removing them all in one command and living dangerously:

//Retrieve all the files named ".git" and pump them into 'rm -rf'
//WARNING if you don't understand why/how this command works, DO NOT run it!

( find . -type d -name ".git" \
  && find . -name ".gitignore" \
  && find . -name ".gitmodules" ) | xargs rm -rf

//WARNING, if you accidentally pipe a `.` or `/` or other wildcard
//into xargs rm -rf, then the next question you will have is: "why is
//the bash ls command not found?  Requiring an OS reinstall.
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • @Jay Taylor, do I type the backslashes or is that part of the command? – Eric Leschinski May 12 '15 at 22:54
  • Yes- the trailing slashes are for readability. In this form I suppose it qualifies as a "multi-line command". – Jay Taylor May 12 '15 at 23:16
  • Consider removing the `.repo` folder (at the top of the tree) when removing all Git-related folders (if doing Android development). http://source.android.com/source/developing.html – CJBS Jun 10 '15 at 18:18
  • I would remove the `-type d` clause from the `.git` find statement, so the submodules .git files are also deleted. – pperejon Mar 28 '17 at 10:25
96

The .git folder is only stored in the root directory of the repo, not all the sub-directories like subversion. You should be able to just delete that one folder, unless you are using Submodules...then they will have one too.

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
  • 8
    Ah terrific, was thinking it was distributed among all subdirs like svn! – user291701 Jan 27 '11 at 22:47
  • 8
    Why would you *delete* it? Use `git archive`; it's for this purpose, or even just tar up everything except the .git directory. – Cascabel Jan 28 '11 at 00:44
  • 1
    You may want to also delete your `.gitignore` file and such. I guess you could write a script to do that, but it might be simpler to just use git-archive and then manually delete them from the archived version. – Tyler Jan 29 '11 at 22:39
  • 3
    [Here](http://www.gitready.com/intermediate/2009/01/29/exporting-your-repository.html) you can find examples of how to use `git archive` which Jefromi mentions. There is also a link back to Stackoverflow question [160608](http://stackoverflow.com/questions/160608/how-to-do-a-git-export-like-svn-export). – Lernkurve Feb 06 '11 at 01:41
  • 3
    You are being unnecessarily clever. It's entirely possible to have a vendor directory with all sorts of other repos cloned into it. Git only stores a root `.git` but you can have repos everywhere, eg. `vendor/something/something/.git`. – srcspider Jun 07 '13 at 12:53
  • 1
    Don't forget about the `.gitmodules` file and any corresponding submodules as well.. – Jay Taylor May 12 '15 at 21:29
  • You can have a workspace folder that is linked to a given repo and the have a repo per app/service you have within your workspace, in this case you'll obviously have a `.git` folder per app/service. So your assumption that you could only have one `.git` at the root folder is completely wrong. – Shady Smaoui Feb 02 '22 at 03:03
25

cd to the repository then

find . -name ".git*" -exec rm -R {} \;

Make sure not to accidentally pipe a single dot, slash, asterisk, or other regex wildcard into find, or else rm will happily delete it.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
zePiet
  • 259
  • 3
  • 2
  • 2
    This is great but I get a notice after `find: ./.git: No such file or directory` it works though. Anyway to stop the notice from showing? – Rohan Deshpande Mar 07 '17 at 07:22
  • If you're sure about the outcome, you can add `2>/dev/null` by the end to get rid of the `find: ./.git: No such file or directory` error. – Thomas Brooks May 11 '23 at 10:13
16

You can use git-archive, for example:

git archive master | bzip2 > project.tar.bz2

Where master is the desired branch.

jweyrich
  • 31,198
  • 5
  • 66
  • 97
  • this takes 2h versus instantaneous `cp -r` – gcb May 30 '11 at 21:11
  • 3
    @gcb: `cp -r` doesn't deal with files and directories that are not part of the repository, which also includes ignored ones. However, if you are certain you don't need this and your processing/time is critical, sure, go ahead with `cp`. – jweyrich May 31 '11 at 00:58
  • 4
    To be fair, `git archive` has a drawback - It does not export submodules. One would need to `git submodule foreach --recursive git archive ...`. Extremely bad! – jweyrich May 29 '13 at 11:12
  • Refusing to include submodules is a feature, not a drawback. – William Pursell Jul 30 '18 at 12:31
8

In case someone else stumbles onto this topic - here's a more "one size fits all" solution.

If you are using .git or .svn, you can use the --exclude-vcs option for tar. This will ignore many different files/folders required by different version control systems.

If you want to read more about it, check out: http://www.gnu.org/software/tar/manual/html_section/exclude.html

OldTinfoil
  • 1,187
  • 19
  • 32
1

ls | xargs find 2>/dev/null | egrep /\.git$ | xargs rm -rf

This command (and it is just one command) will recursively remove .git directories (and files) that are in a directory without deleting the top-level git repo, which is handy if you want to commit all of your files without managing any submodules.

find 2>/dev/null | egrep /\.git$ | xargs rm -rf

This command will do the same thing, but will also delete the .git folder from the top-level directory.

Daniel Teichman
  • 590
  • 3
  • 10
1

Unlike other source control systems like SVN or CVS, git stores all of its metadata in a single directory, rather than in every subdirectory of the project. So just delete .git from the root (or use a script like git-export) and you should be fine.

Adrian Petrescu
  • 16,629
  • 6
  • 56
  • 82
1

With PowerShell

Get-ChildItem -Path .\path\to\folder -Filter ".git" -Recurse -Attributes "H" | Remove-Item -Force

More explanation:

Tobi Obeck
  • 1,918
  • 1
  • 19
  • 31
1

sudo find -name .git -exec rm -fr {} ;

Jay Lepore
  • 85
  • 1
  • 7