Skip to main content
  1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. 如何使用 Git
  6. 如何在 Command Line 中使用 Git
  7. 提交文件
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

提交文件

我们将添加一个新文件,并将其注册在我们刚才创建的存储库中。

使用以下文本内容在该目录中创建一个名为sample.txt的文件。

Anyone can learn Git with this tutorial and Backlog

我们可以使用 git 状态命令来确认“tutorial”目录的状态。

$ git status
On branch main
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	sample.txt
nothing added to commit but untracked files present (use "git add" to track)

我们可以从响应中得知sample.txt当前未被跟踪。您必须先将sample.txt添加到索引中才能对其进行跟踪。

使用 git add 命令,然后是您想添加到索引中的文件。如果要添加多个文件,请用空格将它们分隔。然后验证“sample.txt”是否已成功添加到索引中。

您可以指定.而不是单个文件名称,以将当前目录中的所有文件添加到索引中。

$ git add sample.txt
$ git status
On branch main
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   sample.txt

现在sample.txt已添加到索引中,我们可以继续提交文件。使用 git commit 命令,并再次检查状态。

$ git commit -m "first commit"
[main (root-commit) 13fc237] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 sample.txt


$ git status
On branch main
nothing to commit, working tree clean

命令的响应告诉我们没有更多的新更改要提交。

我们可以通过 git log 命令在存储库的历史日志中看到新添加的提交。

$ git log
  commit ac56e474afbbe1eab9ebce5b3ab48ac4c73ad60e
  Author: username
  Date:   Thu Jul 12 18:00:21 2022 +0900

      first commit

接下来,您已准备好与您的团队共享存储库。

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life