Linux学习篇(六):学习 git

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

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

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

 

安装git:

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

  

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

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

  产生了隐藏文件:

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

 

新建文本文件 huae:

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

  添加内容到huae:

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

  

将huae提交到git 缓存区:

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

 

将huae提交到 git 本地仓库:

1
2
3
4
5
6
7
8
9
10
11
12
13
<<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。

1
2
3
4
5
6
<<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 查看状态:

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

 

git  log 查看日志:

1
2
3
4
5
6
<<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并提交:

1
2
3
<<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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<<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

 

回退到上一版本:

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

  或者

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

  此时的 git log 和 git reflog:

1
2
3
4
5
6
7
8
9
10
11
<<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 @   星云体  阅读(228)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示