Git的初步了解

文件放在git仓库操作

git add、git status、git commit的作用
git add的作用就是把一个文件添加到git仓库暂存区。例如把本地文件readme.txt添加到git仓库暂存区(运行正常的话则不会有报错):

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git add readme

git commit的作用就是把文件提交到git仓库(将暂存区里的改动全部提交到分支)。

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git commit -m "wrote a readme file"
[master (root-commit) 4506e13] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme

把文件放在git仓库,分为两步走,先git add 再git commit。

git status的作用就是看看git仓库暂存区中的文件与本地文件是否一样(当使用git commit 命令以后,git仓库暂存区则为空),如果一样,并且没有使用git commit命令,则显示如下:提示readme这个文件被修改了,应该提交到git的分支上面。

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   readme

如果使用git commit命令以后,则显示git仓库暂存区为空,如下所示:

$ git status
On branch master
nothing to commit, working tree clean

如果不一样则显示:

$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   readme

no changes added to commit (use "git add" and/or "git commit -a")

显示结果提示我们:readme这个文件被修改了,并且没有放在git仓库中。这时我们可以用git diff + "文件名"这个命令来查看现在的文件和git仓库中的文件有什么不一样:

$ git diff readme
diff --git a/readme b/readme
index ccb61d4..14ec998 100644
--- a/readme
+++ b/readme
@@ -1,2 +1,2 @@
-git is a distributed version control system
+git is a  version control system
 git is free software
\ No newline at end of file

提示我们在文件的第一行原来是git is a distributed version control system,现在是git is a version control system。

版本的回退

git log的作用的查看提交到git仓库文件的日志或者说是历史记录,最上面的是最新提交的版本,最下面的是最初的版本。
commit:表示提交的版本id
author:表示提交的用户信息
date:表示提交的时间
最下面是提交到git仓库时写的备注信息

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git log
commit d49bd91870125e075206e4fb5051d55048027f19 (HEAD -> master)
Author: lushenghui <[email protected]>
Date:   Sat Feb 22 18:29:31 2020 +0800

    delete distributes

commit f5551a97832e35dd32d9b9f69ba1a033d6445fee
Author: lushenghui <[email protected]>
Date:   Sat Feb 22 17:49:29 2020 +0800

    add distributes

commit 4506e13aad0b4266b28a07f27e953cf183388646
Author: lushenghui <[email protected]>
Date:   Sat Feb 22 17:43:04 2020 +0800

    wrote a readme file

版本回退,采用的是 git reset --hard + commit id,使用cat命令查看版本回退以后的文件。

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git reset --hard  f5551a97832e35dd32d9b9f69ba1a033d6445fee
HEAD is now at f5551a9 add distributes

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ cat readme
git is a distributed version control system
git is free software

或者采用$ git reset --hard head命令,head表示的当前的版本,就是最新的版本,head^表示倒数第二个版本,以此类推。

$ git log
commit d49bd91870125e075206e4fb5051d55048027f19 (HEAD -> master)
Author: lushenghui <[email protected]>
Date:   Sat Feb 22 18:29:31 2020 +0800

    delete distributes

commit f5551a97832e35dd32d9b9f69ba1a033d6445fee
Author: lushenghui <[email protected]>
Date:   Sat Feb 22 17:49:29 2020 +0800

    add distributes

commit 4506e13aad0b4266b28a07f27e953cf183388646
Author: lushenghui <[email protected]>
Date:   Sat Feb 22 17:43:04 2020 +0800

    wrote a readme file

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git reset --hard head^
HEAD is now at f5551a9 add distributes

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ cat readme
git is a distributed version control system
git is free software

在知道commit id的情况下,git仓库文件版本可以替换。
如果不知道commit id,这时候我们可以用git reflog命令来查看以前的操作命令。

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git reflog
f5551a9 (HEAD -> master) HEAD@{0}: reset: moving to head^
d49bd91 HEAD@{1}: reset: moving to d49bd91870125e075206e4fb5051d55048027f19
f5551a9 (HEAD -> master) HEAD@{2}: reset: moving to f5551a97832e35dd32d9b9f69ba1a033d6445fee
d49bd91 HEAD@{3}: reset: moving to head^^
5a6cf11 HEAD@{4}: reset: moving to 5a6cf1103123db535c06c90b30123d67e6e59d19
4506e13 HEAD@{5}: reset: moving to 4506e13aad0b4266b28a07f27e953cf183388646
5a6cf11 HEAD@{6}: reset: moving to 5a6cf1103123db535c06c90b30123d67e6e59d19
d49bd91 HEAD@{7}: reset: moving to d49bd91870125e075206e4fb5051d55048027f19
5a6cf11 HEAD@{8}: commit: add distributed under the GPL
8e30537 HEAD@{9}: commit: add again distributed
d49bd91 HEAD@{10}: commit: delete distributes
f5551a9 (HEAD -> master) HEAD@{11}: commit: add distributes
4506e13 HEAD@{12}: commit (initial): wrote a readme file

根据提交到git仓库时写的备注,选择对应的commit id,就可以恢复到当时的文件(commit id可以只写前几位)。

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git reflog
f5551a9 (HEAD -> master) HEAD@{0}: reset: moving to head^
d49bd91 HEAD@{1}: reset: moving to d49bd91870125e075206e4fb5051d55048027f19
f5551a9 (HEAD -> master) HEAD@{2}: reset: moving to f5551a97832e35dd32d9b9f69ba1a033d6445fee
d49bd91 HEAD@{3}: reset: moving to head^^
5a6cf11 HEAD@{4}: reset: moving to 5a6cf1103123db535c06c90b30123d67e6e59d19
4506e13 HEAD@{5}: reset: moving to 4506e13aad0b4266b28a07f27e953cf183388646
5a6cf11 HEAD@{6}: reset: moving to 5a6cf1103123db535c06c90b30123d67e6e59d19
d49bd91 HEAD@{7}: reset: moving to d49bd91870125e075206e4fb5051d55048027f19
5a6cf11 HEAD@{8}: commit: add distributed under the GPL
8e30537 HEAD@{9}: commit: add again distributed
d49bd91 HEAD@{10}: commit: delete distributes
f5551a9 (HEAD -> master) HEAD@{11}: commit: add distributes
4506e13 HEAD@{12}: commit (initial): wrote a readme file

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git reset --hard 5a6cf11
HEAD is now at 5a6cf11 add distributed under the GPL

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ cat readme
git is a distributes version control system
git is free software distributed under the GPL.

撤销修改

撤销删除的情况分为三种情况。
第一种情况:已经提交到git分支,这时候只可以通过版本回退的方式进行撤销,见上。
第二种情况:提交到暂存库,还没用提交到git分支。使用两个命令进行回退:

  1. 使用git status命令,通过显示信息我们可以看到readme这个文件被修改了,可以通过git restore --staged 进行回退。
  2. 使用 git restore --staged 进行回退,再使用git status查看。
  3. 使用git restore 回退到最初的版本,这时我们可以使用cat命令来查看文件。
第一步:
Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   readme

第二布:
Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git restore --staged readme
Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   readme

no changes added to commit (use "git add" and/or "git commit -a")

第三步:
Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git restore readme
Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ cat readme
git is a  version control system
git is free software
git has a mutable index

第三种情况:只在工作区修改,没有提交到暂存区。采取方法为使用第二种情况的第三步,如下:

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ cat readme
git is a  version control system
git is free software
git has a mutable index called stage
git has a stage space
git is good

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   readme

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ git restore readme

Administrator@POM655NGG4C4IW2 MINGW64 /g/learngit (master)
$ cat readme
git is a  version control system
git is free software
git has a mutable index called stage
git has a stage space

灵活使用git status命令,根据提示来进行撤销修改。

刚开始学习git,有错误的地方欢迎大家批评指正
参考网站:廖雪峰的官方网站

你可能感兴趣的:(git)