跳至正文

读懂各种git add的方法

废话不多说,直接进入正题

1. git add *

这个命令主要是用于递归添加git工作区的所有目录和文件(不添加隐藏目录和文件 例如.htaccess等)

我们举一个简单的例子看看:

~ $ cd /tmp/
.../usr/tmp $ ls
Python-3.6.9  gitdemo  goaccess
.../usr/tmp $ rm -rf gitdemo/
.../usr/tmp $ mkdir -p gitdemo/subdir1
.../usr/tmp $ cd gitdemo/
.../tmp/gitdemo $ git init
Initialized empty Git repository in /data/data/com.termux/files/usr/tmp/gitdemo/.git/
.../tmp/gitdemo $ mkdir igndir
.../tmp/gitdemo $ ls
igndir  subdir1
.../tmp/gitdemo $ ls -a
.  ..  .git  igndir  subdir1
.../tmp/gitdemo $

接下来,我们创建一个文件和目录,并且创建一个隐藏文件和目录:

.../tmp/gitdemo $ touch .hidefile
.../tmp/gitdemo $ mkdir .hidedir
.../tmp/gitdemo $ la
No command la found, did you mean:
 Command ld in package binutils
 Command ln in package coreutils
 Command lp in package cups
 Command lf in package lf
 Command lha in package lhasa
 Command lr in package lr
 Command lz in package mtools
.../tmp/gitdemo $ ls -a
.  ..  .git  .hidedir  .hidefile  igndir  subdir1
.../tmp/gitdemo $ touch README.md
.../tmp/gitdemo $ touch subdir1/file1
.../tmp/gitdemo $ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .hidefile
        README.md
        subdir1/
nothing added to commit but untracked files present (use "git add" to track)
.../tmp/gitdemo $ tree -a .
.
├── .git
│   ├── HEAD
│   ├── config
│   ├── description
│   ├── hooks
│   │   ├── applypatch-msg.sample
│   │   ├── commit-msg.sample
│   │   ├── fsmonitor-watchman.sample
│   │   ├── post-update.sample
│   │   ├── pre-applypatch.sample
│   │   ├── pre-commit.sample
│   │   ├── pre-merge-commit.sample
│   │   ├── pre-push.sample
│   │   ├── pre-rebase.sample
│   │   ├── pre-receive.sample
│   │   ├── prepare-commit-msg.sample
│   │   ├── push-to-checkout.sample
│   │   └── update.sample
│   ├── info
│   │   └── exclude
│   ├── objects
│   │   ├── info
│   │   └── pack
│   └── refs
│       ├── heads
│       └── tags
├── .hidedir
├── .hidefile
├── README.md
├── igndir
│   └── .hidefile
└── subdir1
    └── file1

12 directories, 21 files
.../tmp/gitdemo $

接下来,执行git add *并提交之后,你就会发现.hidefile变成了“未被追踪的文件”,但是igndir/.hidefile却被添加了

本人有幸在提交网站源码的时候忽略了根目录下的.htaccess文件导致了奇奇怪怪的问题,现在已经改掉了

.../tmp/gitdemo $ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    igndir/.hidefile

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

no changes added to commit (use "git add" and/or "git commit -a")
.../tmp/gitdemo $ git add *
.../tmp/gitdemo $ git commit -m "删除igndir/.hidefile"
[master cafd94f] 删除igndir/.hidefile
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 igndir/.hidefile
.../tmp/gitdemo $ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .hidefile

nothing added to commit but untracked files present (use "git add" to track)
.../tmp/gitdemo $

那么,如果再加上.gitignore呢?

.../tmp/gitdemo $ cat .gitignore
igndir/
ignfile

.../tmp/gitdemo $ git add *
The following paths are ignored by one of your .gitignore files:
igndir
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"
.../tmp/gitdemo $ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        .igndir/
        .ignfile

nothing added to commit but untracked files present (use "git add" to track)
.../tmp/gitdemo $

可以看到git给出了一个提示:以下路径被你的某个.gitignore文件忽略,这意味着匹配.gitignore规则的文件不会因为git add *被添加到暂存区中(除非使用参数-f

所以,执行git add *的时候并不会添加根目录下的隐藏文件(夹),并且包括.gitignore,需要自己手动执行添加命令:

小知识:当你已经提交了不想提交的内容,但提交后又在.gitignore定义了忽略它的规则,那么,你应该使用

git rm -r igndir/

后重新添加并提交一次即可

4
为博客质量打分

给我的博客打个分吧,喜欢的话还请多多支持!

Please add vote reason
标签:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注