上一章我们已经创建了新的clean存储库,但是没有添加任何代码。在本章中,我们将添加一些代码并创建一些修订版。
获取存储库的状态
正如我们之前所说,我们有一个干净的存储库,我们可以检查这一点。
$ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track)
如我们所见 status
命令提供有关存储库的一些简要信息。我们在主分支上。我们将在未来寻找分支。我们没有什么要做的。当然,我们现在没有添加任何内容。
向修订添加文件
Git有一些基本的步骤需要提交。我们将通过编写应用程序来查看它们。下面我们可以找到提交的自然流程。
工作副本
我们将用以下代码创建我们的应用程序。
$ echo 'print("Hello Poftut")' > main.py
我们已经创建了一个文件 main.py
使用单行代码。
审阅更改
我们已经添加了一些代码到我们的项目,并希望看到工作副本的状态。当前使用的工作副本是文件的快照。
$ git status On branch master Initial commit Untracked files: (use "git add..." to include in what will be committed) main.py nothing added to commit but untracked files present (use "git add" to track)
如我们所见,mail.py位于未跟踪文件下。未跟踪文件是未添加到提交操作的文件。因此,如果要提交文件,应该将main.py添加到跟踪的文件中。
$ git add main.py $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached..." to unstage) new file: main.py
我们已将main.py文件添加到跟踪文件中。添加文件后,如果查看工作目录的状态,可以看到main.py被标记为新文件,并且没有其他未跟踪的文件。
相关文章: Git分支操作
请稍后查看状态
我们可以比默认状态更快地查看状态。
$ git status -s M main.p
-
status
显示当前工作状态。 -
-s
请尽快显示状态
显示更改的差异
我们可能希望看到更多关于变化差异的详细信息。这是解决办法。
$ git diff diff --git a/main.py b/main.py index 7865a88..9bce127 100644 --- a/main.py +++ b/main.py @@ -1 +1,2 @@ print("Hello Poftut") +print("Current version 3")
这是密码什么 改变了。
跳过转移
可以使用简单的 -a
参数如下。
$ git commit -a -m"Without staging" [master ca49f23] Without staging 1 file changed, 1 insertion(+)
-
-a
参数make to skip staging,这意味着没有以前的add
需要
提交更改
我们对git发出相同的commit命令。
$ git commit
我们看到下面的屏幕。
Start# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # # Initial commit # # Changes to be committed: # new file: main.py #
下面的屏幕显示有关提交的信息,并希望我们提供一些有关提交的文字。我们将开始添加到屏幕的开头,然后关闭它。
[master (root-commit) 1156333] Start 1 file changed, 1 insertion(+) create mode 100644 main.py
最后我们提交main.py。很难吗?目前可能是,但练习后这是一个非常简单的过程。
再次提交
我们已经完成了我们的项目 README.md
文件,更改了 main.py
到版本2。现在我们必须再次提交。
像往常一样,我们发布了一个 git status
命令查看当前情况。
$ git status On branch master Changes not staged for commit: (use "git add..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: main.py Untracked files: (use "git add ..." to include in what will be committed) README.md no changes added to commit (use "git add" and/or "git commit -a")
如我们所见,如果我们发布 commit
命令。我们需要把它保存到下面的跟踪文件中。
$ git add READMe.md $ git status On branch master Changes to be committed: (use "git reset HEAD..." to unstage) new file: README.md Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: main.py
我们已经添加了自述文件。所以我们可以继续承诺。
$ git commit -m"Version 2, Add README" [master 22dc9ad] Version 2, Add README 1 file changed, 1 insertion(+) create mode 100644 README.md
我们比以前更容易作出承诺,因为我们提供了 message
.
相关文章: Git-环境设置和基本配置
重新提交并更改上次提交
有些时候,我们会抓紧时间,在犯错误的时候犯错误。下面是我们可以重新提交上一次提交的解决方案
$ git add README.md $ git commit --amend [master 1cbc0b2] Gitignore Readme Date: Mon Oct 10 14:47:53 2016 +0000 3 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .gitignore rename main.py => main2.py (100%) $ git log -1 commit 1cbc0b2deb791334886063d4d36a3d1657d387e6 Author: John Doe <[email protected]> Date: Mon Oct 10 14:47:53 2016 +0000 Gitignore Readme
-
--amend
用于更新上次提交,并将新更改添加到上次提交。
取消暂存文件
Unstaging是从stage中删除一个文件,它将被取消跟踪。
$ vim LICENSE $ git add LICENSE $ git status On branch master Changes to be committed: (use "git reset HEAD..." to unstage) modified: LICENSE $ git reset HEAD LICENSE Unstaged changes after reset: M LICENSE$ git status On branch master Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: LICENSE no changes added to commit (use "git add" and/or "git commit -a")
-
reset
从指定的头中清除指定的文件 -
HEAD
是当前暂存文件的状态 -
LICENSE
取消暂存的文件