查看当前工作区的状态(git status)
niko@niko-notebook:~/StydyGit$ git status On branch master # 当前所处的分支 No commits yet nothing to commit (create/copy files and use "git add" to track)当创建一个新文件的时候,这个新的文件处于未被跟踪的状态:
niko@niko-notebook:~/StydyGit$ echo "Hello, Git." > file.txt # 创建一个文件 niko@niko-notebook:~/StydyGit$ git status On branch master No commits yet Untracked files: # 列出未被追踪的文件 (use "git add <file>..." to include in what will be committed) file.txt nothing added to commit but untracked files present (use "git add" to track)
将工作区的文件提交到暂存区,使得Git可以跟踪新文件(git add)
niko@niko-notebook:~/StydyGit$ git add . niko@niko-notebook:~/StydyGit$ git status On branch master No commits yet Changes to be committed: # 列出更改的文件 (use "git rm --cached <file>..." to unstage) new file: file.txt
将暂存区的文件提交到本地库(git commit)
niko@niko-notebook:~/StydyGit$ git commit -m "add file.txt" [master (root-commit) 5f9adfe] add file.txt 1 file changed, 1 insertion(+) create mode 100644 file.txt
查看提交日志(git log)
niko@niko-notebook:~/StydyGit$ git log commit e6442a41c339e0fd6d01656cbea24775471ee1ed (HEAD -> master) # HEAD表示当前版本 Author: niko <niko@163.com> Date: Mon Sep 24 12:34:38 2018 +0800 modifiy file.txt # 提交的注释信息 commit 5f9adfeade857255ef647f855899965274a5fe95 # 40为16进制SHA-256值 Author: niko <niko@163.com> Date: Mon Sep 24 12:16:20 2018 +0800 add file.txt
查看提交日志(git log)
多次修改后的git log
niko@niko-notebook:~/StydyGit$ git log commit b4a54420dcea8d480b80797e032d8ed74b430a97 (HEAD -> master) Author: niko <niko@163.com> Date: Mon Sep 24 12:40:27 2018 +0800 modifiy file.txt commit 17acb2424f04c7ec0298e0c59039d19bce8369b6 Author: niko <niko@163.com> Date: Mon Sep 24 12:39:08 2018 +0800 add file3.txt commit 80cbf7f5c44e0507439bd97a8976d28b746f4f45 Author: niko <niko@163.com> Date: Mon Sep 24 12:38:26 2018 +0800 add file2.txt commit e6442a41c339e0fd6d01656cbea24775471ee1ed Author: niko <niko@163.com> Date: Mon Sep 24 12:34:38 2018 +0800 modifiy file.txt commit 5f9adfeade857255ef647f855899965274a5fe95 Author: niko <niko@163.com> Date: Mon Sep 24 12:16:20 2018 +0800 add file.txt使用git log --pretty=oneline
niko@niko-notebook:~/StydyGit$ git log --pretty=oneline b4a54420dcea8d480b80797e032d8ed74b430a97 (HEAD -> master) modifiy file.txt 17acb2424f04c7ec0298e0c59039d19bce8369b6 add file3.txt 80cbf7f5c44e0507439bd97a8976d28b746f4f45 add file2.txt e6442a41c339e0fd6d01656cbea24775471ee1ed modifiy file.txt 5f9adfeade857255ef647f855899965274a5fe95 add file.txt使用git log --oneline
niko@niko-notebook:~/StydyGit$ git log --oneline b4a5442 (HEAD -> master) modifiy file.txt 17acb24 add file3.txt 80cbf7f add file2.txt e6442a4 modifiy file.txt 5f9adfe add file.txt使用git reflog
niko@niko-notebook:~/StydyGit$ git reflog b4a5442 (HEAD -> master) HEAD@{0}: commit: modifiy file.txt 17acb24 HEAD@{1}: commit: add file3.txt 80cbf7f HEAD@{2}: commit: add file2.txt e6442a4 HEAD@{3}: commit: modifiy file.txt 5f9adfe HEAD@{4}: commit (initial): add file.txt
版本回退(git reset)
参数:
git reset --soft 回退本地库
git reset --mixed 默认,回退暂存区、本地库
git reset --hard 回退暂存区、工作区、本地库
git reset --soft图示
git reset --mixed图示
git reset --hard图示
找回文件(git reset)
niko@niko-notebook:~/StydyGit$ ls # 查看file2.txt文件是否存在
file.txt file2.txt file3.txt
niko@niko-notebook:~/StydyGit$ rm -rf file2.txt # 删除file2.txt文件
niko@niko-notebook:~/StydyGit$ ls # 确保file2.txt文件已经删除
file.txt file3.txt
niko@niko-notebook:~/StydyGit$ git status # 查看文件跟踪状态
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: file2.txt