0

I Stowed a couple of files which were in a folder. This folder is a config folder and has other temp or system specific files which I don't want in my Git backed Stow backup.

What is the best way to only commit intentional files in Stow's git repository? Is it more or less not using git commit -a?

Regards

3
  • have you tried adding the files/directories you want excluded to .gitignore? it's kind of the opposite of what you're asking for, though - everything that isn't explicitly excluded is still included. See man gitignore for details, including details on the kinds of pattern matching it does.
    – cas
    Commented Nov 29, 2021 at 8:37
  • @cas yes that's one option but using gitignore or stow ignore means I will have to constantly add files to them which isn't a very sustainable proposal to me. I think as long as I don't do git add . I will be good. git commit -a stages changed and deleted files but not new files. I don't want to add additional efforts or complexity than what is required by Stow. Commented Nov 29, 2021 at 9:38
  • 1
    Add just a single line with * to .gitignore. Then git add will only add files/dirs if you force it to with the -f option.
    – cas
    Commented Nov 29, 2021 at 11:23

1 Answer 1

1

Add a line with just * to the .gitignore file. git add will then only add files or directories when you force it to with the -f option.

For example:

$ mkdir /tmp/git-test
$ cd /tmp/git-test
$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /tmp/git-test/.git/

$ date > file1.txt
$ date > file2.txt
$ date > file3.txt

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    file1.txt
    file2.txt
    file3.txt

nothing added to commit but untracked files present (use "git add" to track)

$ echo '*' > .gitignore
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

$ git add file1.txt
The following paths are ignored by one of your .gitignore files:
file1.txt
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"

$ git add -f file1.txt
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   file1.txt

$ git commit -m 'initial commit'
[master (root-commit) 2007ca3] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt

$ date >> file1.txt 
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   file1.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -m '2nd commit' . 
[master dba5fea] 2nd commit
 1 file changed, 1 insertion(+)

$ git commit -m '3rd commit' *
error: pathspec 'file2.txt' did not match any file(s) known to git
error: pathspec 'file3.txt' did not match any file(s) known to git

$ date >> file1.txt 
$ git commit -a -m '3rd commit' 
[master e679fde] 3rd commit
 1 file changed, 1 insertion(+)

$ mkdir foo
$ date >> foo/bar.txt 
$ git status
On branch master
nothing to commit, working tree clean
$ git add -f foo
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   foo/bar.txt

$ git commit -a -m '4th commit' 
[master 4048a90] 4th commit
 1 file changed, 1 insertion(+)
 create mode 100644 foo/bar.txt

You must log in to answer this question.

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