您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
05 通过几次commit来认识工作区和暂存区
发布时间:2020-06-27 17:47:44编辑:雪饮阅读()
未追踪状态
当我们在git项目下新建立了文件和目录,我们用git status会发现未追踪状态仅仅存在新建的文件
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ mkdir images
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ touch index.html
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: 2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
只有我们新建立的目录有了文件之后才会显示该目录
$ touch images/1.jpg
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: 2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
images/
index.html
添加至暂存区
支持多个文件/目录的添加,多个用空格间隔
$ git add index.html images
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: 2.txt
new file: images/1.jpg
new file: index.html
提交
会发现暂存区一旦提交后就会自动清空
$ git commit -m 'Add index+logo'
[master 7c5fae5] Add index+logo
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 2.txt
create mode 100644 images/1.jpg
create mode 100644 index.html
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ git status
On branch master
nothing to commit, working tree clean
文件修改导致的待add之前的状态
会发现文件修改也会自动在暂存区待add之前的状态(类似上面的未追踪状态)
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ vi index.html
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ vi styles/styles.css
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ 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: index.html
modified: styles/styles.css
no changes added to commit (use "git add" and/or "git commit -a")
处理文件修改后导致的待add之前状态为add状态
由于这种情况是文件修改也就是说之前是有git管控的,所以此时不能像新增加文件一样用git add而是用git add -u
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ git add 3.txt
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: 3.txt
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: index.html
modified: styles/styles.css
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ git add -u
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in styles/styles.css.
The file will have its original line endings in your working directory
xy@DESKTOP-BG9HNHK MINGW64 /d/备份/git_learning (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: 3.txt
modified: index.html
modified: styles/styles.css
然后就可以像之前一样用commit了
关键字词:git,commit,add,u
下一篇:06 给文件重命名的简便方法