Но, когда я работаю с существующим файлом, он автоматически попадает в промежуточную область и после того, как я изменяю файл; чтобы переместить изменения в локальный репозиторий - мне нужно только зафиксировать изменения с помощью git commit -m.
Git не работает таким образом. Вы должны добавить изменения в область подготовки даже для существующих (отслеживаемых) файлов. Либо вы использовали git commit -a -m
, либо у вас был инструмент автоматического добавления изменений.
git add
делает две вещи. Он помечает файл как отслеженный и копирует отслеживаемые файлы в область подготовки.
Новые файлы не отслеживаются. Неотслеживаемые файлы не считаются частью Git хранилища. Они не будут добавлены в область подготовки с помощью таких команд, как git commit -a
.
-a, --all
Скажите команде автоматически создавать файлы, которые были изменены, и удалены, но новые файлы, о которых вы не сказали Git, не затрагиваются .
$ git init foo
Initialized empty Git repository in /Users/schwern/tmp/foo/.git/
$ cd foo/
$ touch this
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
this
nothing added to commit but untracked files present (use "git add" to track)
$ git commit -a
On branch master
Initial commit
Untracked files:
this
nothing added to commit but untracked files present
После того, как вы git add
из них, они отслеживаются и их изменения добавляются. Теперь, когда файл отслеживается, изменения будут перемещены в промежуточную область с помощью таких команд, как git commit -a
.
$ git add this
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: this
$ git commit -a -m 'commit this'
[master (root-commit) 519fada] commit this
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 this
$ echo 'something new' > this
$ 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: this
no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -a -m 'commit more of this'
[master 996ddb2] commit more of this
1 file changed, 1 insertion(+)
Вы можете установить файл как отслеженный, но не добавлять изменения с помощью git add --intent-to-add
.
Вы можете копировать только отслеженные файлы в указанную область с помощью git add -u
.
$ touch that
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
that
nothing added to commit but untracked files present (use "git add" to track)
$ echo 'another line' >> this
$ 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: this
Untracked files:
(use "git add <file>..." to include in what will be committed)
that
no changes added to commit (use "git add" and/or "git commit -a")
$ git add -u
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: this
Untracked files:
(use "git add <file>..." to include in what will be committed)
that