Git

stow 目錄和 git 中的非儲存文件

  • November 30, 2021

我存放了幾個文件夾中的文件。此文件夾是一個配置文件夾,並且有其他臨時或系統特定文件,我不希望在我的 Git 支持的 Stow 備份中使用這些文件。

僅在 Stow 的 git 儲存庫中送出有意文件的最佳方法是什麼?或多或少不使用git commit -a

問候

*在文件中添加一行.gitignoregit add然後只會在您使用該-f選項強制添加文件或目錄時添加文件或目錄。

例如:

$ 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

引用自:https://unix.stackexchange.com/questions/679493