一、建立本地仓库
1、git init
初始化本地仓库
$ mkdir git-test
$ cd git-test/
$ git init
Initialized empty Git repository in E:/document/git-test/.git/
二、workspace
和 index/stage
交互
1、在 workspace
中创建一个新文件,此时 workspace
与 stage
产生了差异,使用 git status
查看当前工作区各文件的状态,提示 Untracked files
# create a new file
$ echo "[0] This is an untracked file(untracked)" > workspace-stage-interaction.md
# view current status
$ git status
On branch master # current branch
No commits yet
Untracked files: # Untracked files message
(use "git add <file>..." to include in what will be committed)
workspace-stage-interaction.md
nothing added to commit but untracked files present (use "git add" to track)
2、workspace
⇒ \Rightarrow ⇒ stage
: git add
添加文件到暂存区
# add file to track
$ git add workspace-stage-interaction.md
# prompt for different line breaks
warning: LF will be replaced by CRLF in workspace-stage-interaction.md.
The file will have its original line endings in your working directory
# view current status
$ git status
On branch master
No commits yet
# Untracked files message has disappeared
Changes to be committed:
(use "git rm --cached <file>..." to unstage) # use this command to remove the file from stage
new file: workspace-stage-interaction.md
3、workspace
⇐ \Leftarrow ⇐ stage
: git checkout
从暂存区替换工作区文件
# change the file in workspace
$ echo "[1] changed by workspace(modified)" >> workspace-stage-interaction.md
# output the modified file
$ cat workspace-stage-interaction.md
[0] This is an untracked file(untracked)
[1] changed by workspace(modified)
# view current status
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: workspace-stage-interaction.md
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) # use this command to discard the changes
modified: workspace-stage-interaction.md # modified files message
# use 'git checkout -- <file>' to replace files in workspace(from stage) (use 'git checkout .' to replace all the files)
$ git checkout -- workspace-stage-interaction.md
# check file content
$ cat workspace-stage-interaction.md
[0] This is an untracked file(not add)
# the changes we made are lost
# view current stage
$ git status
On branch master
No commits yet
# the message of modified files has disappeared
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: workspace-stage-interaction.md
三、index/stage
和 repository
交互
1、stage
⇒ \Rightarrow ⇒ repository
: git commit
提交暂存区文件 git log
查看提交历史
# commit with message
$ git commit -m "stage-repository-interaction"
[master (root-commit) 43e7b67] stage-repository-interaction
1 file changed, 1 insertion(+)
create mode 100644 workspace-stage-interaction.md
# view current status
$ git status
On branch master
# workspace, stage and the repository keep the same
nothing to commit, working tree clean
# view commit log
$ git log
commit 43e7b676cc3550227ac6be32c3d67780c4445a4b (HEAD -> master)
Author: red1y <2670678040@qq.com> # The identity we set before
Date: Tue Jun 15 17:47:25 2021 +0800
stage-repository-interaction
四、workspace
和 repository
交互
1、workspace
⇐ \Leftarrow ⇐ repository
: git checkout HEAD
用本地仓库中 HEAD
指向的分支替换工作区文件
# create a new file
$ echo "[0] This is a file added by workspace(uncommitted)" > workspace-repository-interaction.md
$ cat workspace-repository-interaction.md
[0] This is a file added by workspace(not commit)
# change the file commited(workspace-stage-interaction.md)
$ echo "[2] changed by workspace(uncommitted)" >> workspace-stage-interaction.md
# view the file content
$ cat workspace-stage-interaction.md
[0] This is an untracked file(not add)
[2] changed by workspace(uncommitted)
# use 'git checkout HEAD .' to replace all the files in workspace(from repository) (use 'git checkout HEAD <file>') to replace single file
$ git checkout HEAD .
Updated 1 path from 6f5743b # show that one file is changed
# view file content
$ cat workspace-repository-interaction.md
[0] This is a file added by workspace(not commit)
# this file is not changed because it is not in the repository
$ cat workspace-stage-interaction.md
[0] This is an untracked file(not add)
# this file back to its origin