4

I have a git repo: rsh:

❯ cat rsh/.gitignore
*.o
shell
build/
rsh_history

And I'm attempting to tar it up using tar (GNU tar) 1.32.

The command I'm using is:

❯ gtar cvaf rsh.tar.gz --exclude-vcs-ignores --exclude-vcs rsh

Unfortunately, the build/ directory is included in the tarfile, in spite of build/ being specified in the .gitignore.

❯ gtar cvaf rsh.tar.gz --exclude-vcs-ignores --exclude-vcs rsh
...
rsh/build/
rsh/build/meson-private/
rsh/build/meson-private/install.dat
rsh/build/meson-private/meson.lock
rsh/build/meson-private/sanitycheckc.exe
rsh/build/meson-private/build.dat
rsh/build/meson-private/sanitycheckc.c
rsh/build/meson-private/cmd_line.txt
rsh/build/meson-private/meson_benchmark_setup.dat
rsh/build/meson-private/meson_test_setup.dat
rsh/build/meson-private/coredata.dat
rsh/build/compile_commands.json
rsh/build/rsh
rsh/build/rsh@exe/
rsh/build/.ninja_deps
rsh/build/build.ninja
rsh/build/meson-logs/
rsh/build/meson-logs/meson-log.txt
rsh/build/meson-info/
rsh/build/meson-info/intro-buildsystem_files.json
rsh/build/meson-info/intro-benchmarks.json
rsh/build/meson-info/intro-buildoptions.json
rsh/build/meson-info/intro-dependencies.json
rsh/build/meson-info/intro-installed.json
rsh/build/meson-info/meson-info.json
rsh/build/meson-info/intro-tests.json
rsh/build/meson-info/intro-projectinfo.json
rsh/build/meson-info/intro-targets.json
rsh/build/.ninja_log
...

Curiously enough, the .gitignore itself does get ignored (--exclude-vcs); as well the other files in the .gitignore (rsh_history).

5
  • it might be a difference in interpretation of the .gitignore file. try either build/* or build/** rather than just build/.
    – cas
    Aug 24, 2019 at 6:59
  • @cas neither of those worked; not did build/**/*
    – xrisk
    Aug 24, 2019 at 7:01
  • try **/build/* - that's the only way I've been able to get it to exclude an entire subdirectory. note: this will also exclude build/* found in any lower level subdirs.
    – cas
    Aug 24, 2019 at 7:27
  • **/build and build also seem to work (and match non-directories too). I would add these workarounds additionally to the .gitignore with a comment like # needed for tar --exclude-vcs-ignores
    – Freddy
    Aug 24, 2019 at 7:34
  • if you write that up as an answer, i'll upvote it and you can accept it in a day or so.
    – cas
    Aug 24, 2019 at 9:28

2 Answers 2

5

As mentioned in the comments to the question, both build and **/build work.

It should be noted though that this will also match in subdirectories. And as far as I've seen (someone please edit this answer if you know better), there is no way around this through either the .gitignore file or a simple command line option. Because tar doesn't understand either the leading slash syntax of git (so e.g. /build to match only build in the root directory), nor ! inclusions (e.g. !but_i_want_this_included/build).

The official docs say:

.gitignore

Contains shell-style globbing patterns. Applies to the directory where `.gitfile' is located and all its subdirectories.

1

If you need to only exclude directories in the same directory as the .gitignore file, you can add two rules in .gitignore:

#
# exclude log directory in the same directory as .gitignore
#

# - for git
/log/

# - and for the linux shell
./log

This way, git uses the entry with the leading / and does not match ./.

Shell-tools like tar on the other hand, can't handle leading /, but can interpret ./.

So your original command with the option --exclude-vcs-ignores will work.

There is one problem though:

This will only work, if you run tar from the same directory, where the .gitignore file is located. It will not work for other .gitignore files in directories below.

1
  • will accept after confirming
    – xrisk
    Feb 12, 2020 at 16:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .