本文参考了:https://blog.csdn.net/u010697394/article/details/55252429
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013752340242354807e192f02a44359908df8a5643103a000
Git在未进行commit操作之前,存在三种状态:Untracked files,Changes not staged for commit及Changes to be committed,每种状态之间可以随意进行互相转换。了解这三种状态各自所对应的不同情况,能够帮助你方便有效的使用Git来管理项目。
类似状态机,我们在每个状态下既可以前进也可以后退。我们以文件hello.c进行说明。
一、本地状态
我在本地单独创建了一个文本hello.c,在里面添加了一行内容
#新建文件
$ vi hello.c
#显示本地文件
$ ls -a
./ ../ .git/ hello.c README.md readme.txt
#git status命令用于查看项目的当前状态。
$ git status
On branch master
Your branch is up-to-date with 'mylocal/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.c
nothing added to commit but untracked files present (use "git add" to track)
说明:在修改后,查看状态,可以看到,Git提示hello.c为“Untracked files”,并且提示使用“git add …”的命令将文件包含到待提交清单中。
二、暂存状态
按照提示,使用“git add hello.c”命令,然后,使用“git status”查看。
#add至本地仓库
$ git add hello.c
warning: LF will be replaced by CRLF in hello.c.
The file will have its original line endings in your working directory.
#查看状态
$ git status
On branch master
Your branch is up-to-date with 'mylocal/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.c
文件hello.c状态变成了“Changes to be committed”,也就是说hello.c在暂存区域生成了快照(以前讲过git add 是把修改提交到暂存区),等待被提交。
后退至“Untracked files”文件状态
正如Git所提示的那样,通过“git rm –cached hello.c”命令,可以将文件状态还原为未暂存状态,即回到“Untracked files”文件状态。
#后退至“Untracked files”文件状态
git rm --cached hello.c
git status
三、修改状态
现在,hello.c已经可以被提交到git目录中了,但是暂时不提交。继续想hello.c修改内容,之后用“git status”查看:
#修改了文件
$ vi hello.c
#查看状态
$ git status
On branch master
Your branch is up-to-date with 'mylocal/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.c
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: hello.c
修改文件状态
可以看到,除了之前的“Changes to be committed”状态,下面又多了一条“Changes not staged for commit”状态,表明文件已经修改,但是还没有放入暂存区域,也就是没生成快照。
如果现在进行commit操作,只是将修改之前的文件快照提交到了git目录,最近的修改则不会提交,一定记住:只有暂存区域的文件(即:文件状态为“Changes to be committed”)才会被提交。
正如提示,通过“git add hello.c”命令将已修改文件更新到暂存区域中,如果想撤销修改,可以使用“git checkout – hello.c”命令。那就执行git checkout –hello.c看一下是否恢复
#(前进)修改文件更新到暂存区
git add hello.c
git status
#(后退)撤销修改
git checkout -- hello.c
git status
撤销修改操作
经验证确实恢复了修改。
四、提交状态
在添加至暂存状态后,可以提交到远程仓库了。
#查看状态
$ git status
On branch master
Your branch is up-to-date with 'mylocal/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.c
pp@LAPTOP-56MVEABB MINGW64 /f/gitCode (master)
$ git commit -m "add hello.c file"
[master 411c9ce] add hello.c file
1 file changed, 7 insertions(+)
create mode 100644 hello.c
pp@LAPTOP-56MVEABB MINGW64 /f/gitCode (master)
$ git status
On branch master
Your branch is ahead of 'mylocal/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
实际工作中我们会遇到一个文件修改好几次,也同时修改多个文件。这时候总不能一个修改一次add 到暂存区,在commit到分支,也不提倡。
针对一个文件的修改你最终改完执行add,再commit
git add hello.c
git commit -m "add hello.c file"
针对多个文件,你可以执行 git add .(注意add 后边的符号 点),在git commit -m “”。
git add .
git commit -m "add all files"
将本地仓库mylocal提交至远程仓库
git push mylocal master
然后在网站上即可以看到最新的同步了。