Previous chapter we have created our new clean repository but we did not added any code. In this chapter we will add some code and create some revision or versions of the code.
在上一章中,我们创建了新的干净存储库,但未添加任何代码。 在本章中,我们将添加一些代码并创建一些版本的代码。
获取存储库状态 (Getting Status of Repository)
As we stated before we have a clean repository we can check this.
如前所述,我们有一个干净的存储库,我们可以检查一下。
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
As we see status
command provides some brief information about repository. We are on the master branch. We will look branches in the future. We have nothing to commit. Sure we have not added anything for now.
如我们所见, status
命令提供了有关存储库的一些简要信息。 我们在master分支上。 将来我们会寻找分支机构。 我们没有什么可承诺的。 当然,我们暂时还没有添加任何东西。
将文件添加到修订版 (Adding Files to the Revision)
Git have some fundamental steps to commit. We will look them through writing our app. Below we can find the natural flow of a commit.
Git有一些基本步骤可以落实。 我们将通过编写我们的应用程序来查找它们。 在下面我们可以找到提交的自然流程。

工作副本 (Working Copy)
We will create our app with the following code.
我们将使用以下代码创建我们的应用。
$ echo 'print("Hello Poftut")' > main.py
We have created a file main.py
with single line code.
我们用单行代码创建了文件main.py
查看变更 (Review Changes)
We have added some code to our project and want to see status of working copy. Working copy is currently used snapshot of the files.
我们已经在项目中添加了一些代码,并希望查看工作副本的状态。 工作副本是当前使用的文件快照。
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
main.py
nothing added to commit but untracked files present (use "git add" to track)
As we see mail.py is under untracked files. Untracked file is a files that is not added to the commit operation. So If we want to commit our file we should add main.py into tracked files.
如我们所见,mail.py位于未跟踪的文件下。 未跟踪的文件是未添加到提交操作的文件。 因此,如果要提交文件,则应将main.py添加到跟踪的文件中。
$ git add main.py
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: main.py
We have added main.py file to the tracking files. After adding file if we look to the status of working directory we can see that main.py is tagged as new file and there is no other untracked file.
我们已经将main.py文件添加到跟踪文件中。 添加文件后,如果我们查看工作目录的状态,我们可以看到main.py被标记为新文件,并且没有其他未跟踪的文件。
即将审核状态(Review Status Shortly)
We can review status more shortly than default one.
与默认值相比,我们可以查看状态的时间更短。
$ git status -s
M main.p
status
show the current working status.status
显示当前的工作状态。-s
show status shortly please-s
请尽快显示状态
显示变化的差异(Show Diff of Changes)
We may want to see more detailed information about like the diff of the changes. Here is the solution.
我们可能希望看到有关更改差异的更多详细信息。 这是解决方案。
$ 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")
This is the code what is changed.
这是代码更改的地方。
跳过阶段 (Skipping Staging)
Staging can be skipped with simple -a
parameter like below.
可以使用简单的-a
参数跳过过渡,如下所示。
$ git commit -a -m"Without staging"
[master ca49f23] Without staging
1 file changed, 1 insertion(+)
-a
parameter make to skip staging which means no previousadd
needed-a
参数make跳过暂存,这意味着不需要先前add
提交变更 (Commit Changes)
We issue same commit command for git.
我们为git发出相同的commit命令。
$ git commit
And we get following screen.
然后我们得到以下屏幕。
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
#
Following screen show information about commit and wants some words about commit from us. We add Start to the beginning of the screen and then close it.
以下屏幕显示有关提交的信息,并希望我们提供一些有关提交的信息。 我们将“开始”添加到屏幕的开头,然后将其关闭。
[master (root-commit) 1156333] Start
1 file changed, 1 insertion(+)
create mode 100644 main.py
And finally we commit our main.py . Was it hard? For now may be but after practicing it this is very simple procedure.
最后,我们提交main.py。 很难吗目前可能是,但是在实践之后,这是一个非常简单的过程。
再次提交 (Commit Again)
We have worked on our project and added README.md
file, changed the main.py
to version 2. Now we have to commit again.
我们已经完成了项目,并添加了README.md
文件,将main.py
更改为版本2。现在,我们必须再次提交。
As we always do we issue a git status
command to see current situation.
像往常一样,我们发出git status
命令来查看当前情况。
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: main.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
no changes added to commit (use "git add" and/or "git commit -a")
As we see that REAME.md is not tracked if we issue commit
command. We need to it to the tracked files like below.
如我们所见,如果发出commit
命令,则不会跟踪REAME.md。 我们需要将其跟踪到如下所示的文件。
$ git add READMe.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README.md
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: main.py
We have added the README file. So we can continue to commit.
我们添加了自述文件。 这样我们就可以继续提交。
$ git commit -m"Version 2, Add README"
[master 22dc9ad] Version 2, Add README
1 file changed, 1 insertion(+)
create mode 100644 README.md
We have commited more easily than before because we have provided the message
.
因为我们提供了message
,所以比以前更容易做出承诺。
重新提交并更改最后一次提交(Recommit and Change Last Commit)
Some times we hurry up and make mistakes while committing. Here is the solution we can recommit our last commit
有时我们会着急并在犯错时犯错误。 这是我们可以重新提交上一次提交的解决方案
$ 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
used to update the last commit and new changes are added to the last commit.--amend
用于更新最后的提交,新的更改将添加到最后的提交。
取消暂存文件 (Unstaging a File)
Unstaging is removing a file from stage and it will be untracked.
取消登台是从舞台中删除文件,该文件将无法跟踪。
$ vim LICENSE
$ git add LICENSE
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." 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 <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
reset
clears specified file from specified HEADreset
从指定的HEAD清除指定的文件HEAD
is current staged files statusHEAD
是当前暂存文件的状态LICENSE
the file for unstagingLICENSE
文件以供暂存
翻译自: https://www.poftut.com/git-committing-changes-repository/