Linux学习篇(六):学习 git

逛 github 时面对git 、make 无从下手?本文先来介绍 git 。

git 用于 文本文件的 版本管理,git 作者 与 Linux 内核作者是同一个人——林纳斯。

初次学习 git 搭配 BeyondCompare 会更香哦。

 

安装git:

<<huaecase@Huaecase ~ >>$  sudo apt install git

  

建立工作区( 建议在一个 空文件夹内):

<<huaecase@Huaecase ~/gitwork >>$  mkdir gitwork
<<huaecase@Huaecase ~/gitwork >>$  cd gitwork
<<huaecase@Huaecase ~/gitwork >>$  git init

  产生了隐藏文件:

<<huaecase@Huaecase ~/gitwork >>$  ls -a .git

 

新建文本文件 huae:

<<huaecase@Huaecase ~/gitwork >>$  vim huae

  添加内容到huae:

echo "first" > huae       # '>' 和 '>>' 代表输出重定向 '>' 为替换模式 而 '>>' 为追加。

  

将huae提交到git 缓存区:

<<huaecase@Huaecase ~/gitwork >>$ git add huae

 

将huae提交到 git 本地仓库:

<<huaecase@Huaecase ~/gitwork >>$  git commit huae -m "first commit"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name (for <huaecase@Huaecase.localdomain>) not allowed

  首次使用要设置绑定的邮箱及账户名,解决后再次 commit。

<<huaecase@Huaecase ~/gitwork >>$  git config --global user.email "Huae@mail.com"
<<huaecase@Huaecase ~/gitwork >>$  git config --global user.name "Huae"
<<huaecase@Huaecase ~/gitwork >>$  git commit huae -m "first commit"
[master (root-commit) 6f44e7b] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 huae

 

git status 查看状态:

<<huaecase@Huaecase ~/gitwork >>$  git status
On branch master
nothing to commit, working tree clean

 

git  log 查看日志:

<<huaecase@Huaecase ~/gitwork >>$  git log
commit 6f44e7bb3e1f590d3c9ce5ea389becb41a29ec66 (HEAD -> master)
Author: Huae <Huaecase@gmail.com>
Date:   Mon Oct 5 17:35:24 2020 +0800

    first commit

  git log 是站在 当前 版本节点 看日志,版本切换后git  log 内容 也会跟着更改。  而  git reflog 则是 站在 上帝视角看日志。

 

将huae 中的 内容更改为 second并提交:

<<huaecase@Huaecase ~/gitwork >>$  echo "second" > huae
<<huaecase@Huaecase ~/gitwork >>$  git add huae
<<huaecase@Huaecase ~/gitwork >>$  git commit huae -m "second commit"

  此时git log 和 git reflog:

<<huaecase@Huaecase ~/gitwork >>$    git log
commit cd64c55d4fedeffdea73379bd36b9c472430d2b7 (HEAD -> master)
Author: Huae <Huaecase@gmail.com>
Date: Mon Oct 5 17:45:21 2020 +0800

second commit

commit 6f44e7bb3e1f590d3c9ce5ea389becb41a29ec66
Author: Huae <Huaecase@gmail.com>
Date: Mon Oct 5 17:35:24 2020 +0800

first commit
<<huaecase@Huaecase ~/gitwork >>$    git reflog
cd64c55 (HEAD -> master) HEAD@{0}: commit: second commit
6f44e7b HEAD@{1}: commit (initial): first commit

 

回退到上一版本:

<<huaecase@Huaecase ~/gitwork >>$ git reset --hard 6f44              #6f44 是第一次提交后HEAD指向的 hard 的前一部分

  或者

<<huaecase@Huaecase ~/gitwork >>$ git reset --hard HEAD^             # HEAD 相当于 指针, ^ 表示前一版本

  此时的 git log 和 git reflog:

<<huaecase@Huaecase ~/gitwork >>$  git log
commit 6f44e7bb3e1f590d3c9ce5ea389becb41a29ec66 (HEAD -> master)
Author: Huae <Huaecase@gmail.com>
Date:   Mon Oct 5 17:35:24 2020 +0800

    first commit
<<huaecase@Huaecase ~/gitwork >>$  git reflog
6f44e7b (HEAD -> master) HEAD@{0}: reset: moving to 6f44
cd64c55 HEAD@{1}: commit: second commit
6f44e7b (HEAD -> master) HEAD@{2}: commit (initial): first commit
<<huaecase@Huaecase ~/gitwork >>$

  

 

posted @ 2020-10-05 18:08  星云体  阅读(223)  评论(0编辑  收藏  举报