lighthouse@VM-8-10-ubuntu:gitcode$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: hint: git config --global init.defaultBranch <name>
hint: hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: hint: git branch -m <name>
Initialized empty Git repository in /home/lighthouse/code/gitcode/.git/
lighthouse@VM-8-10-ubuntu:gitcode$ touch book
lighthouse@VM-8-10-ubuntu:gitcode$ vim book
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
lighthouse@VM-8-10-ubuntu:gitcode$ git add book
lighthouse@VM-8-10-ubuntu:gitcode$ git commit -m "commit my first file"
[master (root-commit) dc24b53] commit my first file
1 file changed, 1 insertion(+)
create mode 100644 book
什么是修改? ===> 比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改。对 book 文件进行一次修改:
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
Hello World
此时,仓库中的 book 和我们工作区的 book 是不同的,如何查看当前仓库的状态呢?
git status 命令用于查看在你上次提交之后是否有对文件进行再次修改。
lighthouse@VM-8-10-ubuntu:gitcode$ 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: book
no changes added to commit (use "git add" and/or "git commit -a")
lighthouse@VM-8-10-ubuntu:gitcode$ git diff book
diff --git a/book b/book
index 598dc4c..92a1e2d 100644
--- a/book
+++ b/book
@@ -1 +1,2 @@
Hello Island1314
+Hello World
知道了对当前文件做了什么修改后,再把它提交到本地仓库就放心多了。
git add 之后,就没有看到上面 no changes added to commit(use "git add" and/or "git commit -a") 的消息了。
接下来让我们继续 git commit 即可:
lighthouse@VM-8-10-ubuntu:gitcode$ git add book
lighthouse@VM-8-10-ubuntu:gitcode$ git commit -m "add modify Book file"
[master b2f0ee2] add modify Book file
1 file changed, 1 insertion(+)
lighthouse@VM-8-10-ubuntu:gitcode$ git status
On branch master
nothing to commit, working tree clean
但是现在 git log 并不能打印出 version 3 版本的 commit id,运气好的话我们可以从终端上去找找之前的记录,运气不好的话 commitid 已经被我们搞丢了,如下是找到了 version 3 的 commit id。
lighthouse@VM-8-10-ubuntu:gitcode$ git reset --hard c0117bd12effb75985b185a59ac69b21e20949c1
HEAD is now at c0117bd add version3
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
Hello World
hello version1
hello version2
hello version3
lighthouse@VM-8-10-ubuntu:gitcode$ git reflog
6c506d8 (HEAD -> master) HEAD@{0}: reset: moving to 6c506d833b9d90494255f572e414a2637941577e
c0117bd HEAD@{1}: reset: moving to c0117bd12effb75985b185a59ac69b21e20949c1
6c506d8 (HEAD -> master) HEAD@{2}: reset: moving to 6c506d833b9d90494255f572e414a2637941577e
c0117bd HEAD@{3}: commit: add version3
6c506d8 (HEAD -> master) HEAD@{4}: commit: add version2
6af6d8b HEAD@{5}: commit: add version1
b2f0ee2 HEAD@{6}: commit: add modify Book file
dc24b53 HEAD@{7}: commit (initial): commit my first file
这样,你就可以很方便的找到你的所有操作记录了,但 c0117bd 这个是啥东西?这个是 version3 的 commit id 的部分。没错,Git 版本回退的时候,也可以使用部分 commit id 来代表目标版本。示例如下:
lighthouse@VM-8-10-ubuntu:gitcode$ git reset --hard c0117bd
HEAD is now at c0117bd add version3
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
Hello World
hello version1
hello version2
hello version3
但是在实际开发中,由于长时间的开发了,导致 commit id 早就找不到了,可突然某一天,我又想回退到 version3,那该如何操作呢?貌似现在不可能了。
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
Hello World
hello version1
hello version2
hello version3
This piece of code is like shit # 新增代码
lighthouse@VM-8-10-ubuntu:gitcode$ 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: book
no changes added to commit (use "git add" and/or "git commit -a")
lighthouse@VM-8-10-ubuntu:gitcode$ git checkout -- book
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
Hello World
hello version1
hello version2
hello version3
2️⃣已经 add,但没有 commit
add 后还是保存到了暂存区,该怎么撤销呢??
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
Hello World
hello version1
hello version2
hello version3
This piece of code is like shit # 新增代码# add 存入暂存区
lighthouse@VM-8-10-ubuntu:gitcode$ git add book
lighthouse@VM-8-10-ubuntu:gitcode$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: book
# --mixed 是默认参数,使用时可省略
lighthouse@VM-8-10-ubuntu:gitcode$ git reset HEAD book
Unstaged changes after reset:
M book
# 此时暂存区就是干净的,工作区有修改
lighthouse@VM-8-10-ubuntu:gitcode$ 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: book
no changes added to commit (use "git add" and/or "git commit -a")
然后此时就回到了情况一的时候,再使用 git checkout -- book 丢弃掉工作区的修改即可。
3️⃣已经 add,也已经 commit
我们可以 git reset --hard HEAD^ 回退到上一个版本!
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
Hello World
hello version1
hello version2
hello version3
This piece of code is like shit # 新增代码# 提交
lighthouse@VM-8-10-ubuntu:gitcode$ git add book
lighthouse@VM-8-10-ubuntu:gitcode$ git commit -m "test quash"
[master 3a76865] test quash
1 file changed, 1 insertion(+)
# 回退
lighthouse@VM-8-10-ubuntu:gitcode$ git reset --hard HEAD^
HEAD is now at c0117bd add version3
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314
Hello World
hello version1
hello version2
hello version3
lighthouse@VM-8-10-ubuntu:gitcode$ ls book file
lighthouse@VM-8-10-ubuntu:gitcode$ rm file
注意:但这样直接删除是没有用的,反而徒增烦恼,git status 命令会立刻告诉你哪些文件被删除了。
lighthouse@VM-8-10-ubuntu:gitcode$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: file
no changes added to commit (use "git add" and/or "git commit -a")
lighthouse@VM-8-10-ubuntu:gitcode$ git checkout -- file
lighthouse@VM-8-10-ubuntu:gitcode$ git status
On branch master
nothing to commit, working tree clean