[Git] ~Git笔记_1/2 - Basic operations
A few common cmd for Rookie and will continuously add.
版本查看:
[jesse@localhost git]$ git --version
git version 1.7.1
命令路径:
[jesse@localhost demo]$ git --exec-path
/usr/libexec/git-core
配置文件设置: ~/.gitconfig
/* At least, something the code server need to know */
[jesse@localhost git]$ git config --global user.name "jesse"
[jesse@localhost git]$ git config --global user.email jesse1@gmail.com
Dir: ~/.gitconfig
1 [user] 2 name = jesse 3 email = jesse1@gmail.com
[jesse@localhost git]$ git config --global color.ui.true
<系统级配置文件> /etc/gitconfig (git config -e -- system ) <全局配置文件> ~/.gitconfig (git config -e --global) <版本库级别配置文件> /home/jesse/workMe/git/workspace/demo/.git/config (git config -e) |
==== demo测试 ====
"建立ProjSpace --> 修改 --> 提交 --> 查看"
[jesse@localhost workspace]$ pwd
/home/jesse/workMe/git/workspace
[jesse@localhost workspace]$ git init demo //建立工作目录demo
Initialized empty Git repository in /home/jesse/workMe/git/workspace/demo/.git/
[jesse@localhost demo]$ ls -a
. .. .git
/** demo目录中 为测试做些修改 **/
[jesse@localhost demo]$ touch welcome.txt
[jesse@localhost demo]$ echo "Hello." > welcome.txt
[jesse@localhost demo]$ cat welcome.txt
Hello.
- 提交过程:
[jesse@localhost demo]$ git add welcome.txt // 工作区 --> 缓存区
[jesse@localhost demo]$ git commit -m "initialized." // 缓存区 --> HEAD
[master (root-commit) 26f91c7] initialized.
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 welcome.txt
- Rewrite 提交 comment:
[jesse@localhost demo]$ git config --global core.editor vim
[jesse@localhost demo]$ git commit --amend
- 提交状态:
[jesse@localhost demo]$ git status -s
/* added or committed */
- 查看提交记录(show details):
[jesse@localhost .git]$ git log --pretty=fuller
commit cf10773ec27a2a5c852ddf8ea0ca43b850afa936
Author: jesse <jesse1@gmail.com>
AuthorDate: Thu Jan 5 09:29:59 2012 +0800
Commit: jesse <jesse1@gmail.com>
CommitDate: Thu Jan 5 09:29:59 2012 +0800
initialized.
- 查看提交记录(分支线,more concise and simple):
[jesse@localhost demo]$ git log --graph --oneline
* af26ced does master follow this new commit?
* 010b7b8 initialized.
Git Diff 对比之 "工作区 -- 暂存区 -- HEAD区" <-- 重点理解
查看状态:
[jesse@localhost demo]$ mkdir a/b/c -p
[jesse@localhost demo]$ touch a/b/c/hello.txt
[jesse@localhost demo]$ echo "Bye-Bye." >> welcome.txt
[jesse@localhost demo]$ echo "Hello." >> a/b/c/hello.txt
[jesse@localhost demo]$ git add . // add, 但未 commit
[jesse@localhost demo]$ echo "Bye-Bye." >> a/b/c/hello.txt
[jesse@localhost demo]$ git status -s
AM a/b/c/hello.txt
M welcome.txt
- 工作区 --> 暂存区: git diff
[jesse@localhost demo]$ git diff diff --git a/a/b/c/hello.txt b/a/b/c/hello.txt index 18832d3..e8577ea 100644 --- a/a/b/c/hello.txt +++ b/a/b/c/hello.txt @@ -1 +1,2 @@ Hello. +Bye-Bye.
- 暂存区 --> HEAD区: git diff --cached
[jesse@localhost demo]$ git diff --cached diff --git a/a/b/c/hello.txt b/a/b/c/hello.txt new file mode 100644 index 0000000..18832d3 --- /dev/null +++ b/a/b/c/hello.txt @@ -0,0 +1 @@ +Hello. diff --git a/welcome.txt b/welcome.txt index f6dfd68..b1c2e36 100644 --- a/welcome.txt +++ b/welcome.txt @@ -1,3 +1,4 @@ Hello. :) World. :p +Bye-Bye.
- 工作区 --> HEAD区: git diff HEAD
[jesse@localhost demo]$ git diff HEAD diff --git a/a/b/c/hello.txt b/a/b/c/hello.txt new file mode 100644 index 0000000..e8577ea --- /dev/null +++ b/a/b/c/hello.txt @@ -0,0 +1,2 @@ +Hello. +Bye-Bye. diff --git a/welcome.txt b/welcome.txt index f6dfd68..b1c2e36 100644 --- a/welcome.txt +++ b/welcome.txt @@ -1,3 +1,4 @@ Hello. :) World. :p +Bye-Bye.
* Git Commit Info:
[jesse@localhost demo]$ git log -l --pretty=raw <-- 查看某次的提交信息
commit 010b7b87a9b6b1fbe66b6867c80fa48560bf93e7 //提交的唯一标识 tree a7bf9f06fa0044b37291d75d8c217fb479fb319c //提交对应的目录树 parent 1bbc88dea49fd6219a4b07bb177251c40822a949 //上一次提交 author jesse <jesse1@gmail.com> 1325729369 +0800 committer jesse <jesse1@gmail.com> 1325729369 +0800
- .git 目录

.git/ | branches/ | hooks/ | info/ | logs/ | objects/ | | 01/ | | | 0b7b87a9b6b1fbe66b6867c80fa48560bf93e7 ... ... | refs/ | | heads/ | | | master /* 分支中的“历史变化” */ | | tags/ | COMMIT_EDITMSG | HEAD/* ref--> refs/heads/master */ | config | description | index
- 查看该commit信息:
[jesse@localhost demo]$ git cat-file -t 010b7b
commit
[jesse@localhost demo]$ git cat-file -p 010b7b
tree a7bf9f06fa0044b37291d75d8c217fb479fb319c
parent 1bbc88dea49fd6219a4b07bb177251c40822a949
author jesse <jesse1@gmail.com> 1325729369 +0800
committer jesse <jesse1@gmail.com> 1325729369 +0800
initialized.
- ./.git/objects 内容存放处
jesse@BJSCWL1802 ~/test $ git cat-file -p e5d9 100644 blob 6d0548980902eed6e435e59a59844150a3c59353 welcome.txt jesse@BJSCWL1802 ~/test $ git cat-file -p 6d054 hello world add something
==== 参数选项 ( 参 考 ) ====
选项 说明 -p 按补丁格式显示每个更新之间的差异。 --stat 显示每次更新的文件修改统计信息。 --shortstat 只显示 --stat 中最后的行数修改添加移除统计。 --name-only 仅在提交信息后显示已修改的文件清单。 --name-status 显示新增、修改、删除的文件清单。 --abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。 --relative-date 使用较短的相对时间显示(比如,“2 weeks ago”)。 --graph 显示 ASCII 图形表示的分支合并历史。 --pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)--> 具体如下所示
--pretty=format 定制要显示的记录格式,这样的输出便于后期编程提取分析,像这样:
$ git log --pretty=format:"%h - %an, %ar : %s" ca82a6d - Scott Chacon, 11 months ago : changed the version number 085bb3b - Scott Chacon, 11 months ago : removed unnecessary test code a11bef0 - Scott Chacon, 11 months ago : first commit
常用的格式占位符写法及其代表的意义:
选项 说明 %H 提交对象(commit)的完整哈希字串 %h 提交对象的简短哈希字串 %T 树对象(tree)的完整哈希字串 %t 树对象的简短哈希字串 %P 父对象(parent)的完整哈希字串 %p 父对象的简短哈希字串 %an 作者(author)的名字 %ae 作者的电子邮件地址 %ad 作者修订日期(可以用 -date= 选项定制格式) %ar 作者修订日期,按多久以前的方式显示 %cn 提交者(committer)的名字 %ce 提交者的电子邮件地址 %cd 提交日期 %cr 提交日期,按多久以前的方式显示 %s 提交说明
====分离HEAD指针====
jesse123@BJSCWL1802 ~/tmp $ cat .git/HEAD
ref: refs/heads/master
===================
After detched HEAD:
===================
jesse123@BJSCWL1802 ~/tmp $ cat .git/HEAD
asd0f98asd08adasd0f98asdf0sd8fasd... // HEAD不再与master同步,分离了
===================
Add new branch:
===================
jesse123@BJSCWL1802 ~/test2 $ git log --pretty=oneline
33746bf3bbe0c9d92c509bdaf67539963db46ccf master branch
91230cd710402918d7f0068ef70f346b665b51df second line have added.
71ec4240bf41ccb4dd2897f33f5d0e6642281bed add first line.
jesse123@BJSCWL1802 ~/test2 $ git rev-parse HEAD master
33746bf3bbe0c9d92c509bdaf67539963db46ccf
33746bf3bbe0c9d92c509bdaf67539963db46ccf
jesse123@BJSCWL1802 ~/test2 $ git checkout 9123 -b sec_branch // Create new branch
Switched to a new branch 'sec_branch'
jesse123@BJSCWL1802 ~/test2 $ git rev-parse HEAD master sec_branch
91230cd710402918d7f0068ef70f346b665b51df
33746bf3bbe0c9d92c509bdaf67539963db46ccf
91230cd710402918d7f0068ef70f346b665b51df
jesse123@BJSCWL1802 ~/test2 $ git status -s -b
## sec_branch
jesse123@BJSCWL1802 ~/test2 $ git branch -v
* master 33746bf master branch
sec_branch 1ef951c add new branch.
jesse123@BJSCWL1802 ~/test2 $ git merge <commit>
jesse123@BJSCWL1802 ~/test2 $ git log --graph --pretty=oneline --stat
* 7e15ce9590363f10e4fdbb47e0f93f22b973e13d Merge commit '1ef95' |\ | * 1ef951c567d17d1a6814538619423758b87c5c22 add new branch. | | file.txt | 1 + | | 1 files changed, 1 insertions(+), 0 deletions(-) * | 33746bf3bbe0c9d92c509bdaf67539963db46ccf master branch |/ | file.txt | 1 + | 1 files changed, 1 insertions(+), 0 deletions(-) * 91230cd710402918d7f0068ef70f346b665b51df second line have added. | file.txt | 1 + | 1 files changed, 1 insertions(+), 0 deletions(-) * 71ec4240bf41ccb4dd2897f33f5d0e6642281bed add first line. file.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
==== 恢复方案====
- 暴力恢复: HEAD -->暂存区, 工作区
[jesse@localhost demo]$ git log --graph --oneline
* 759b436 initialized.
* 6623648 initialized.
* 26f91c7 initialized.
[jesse@localhost demo]$ git reset --hard 6623648
恢复上一级:
git reset --hard HEAD^
- 暴力恢复 git add 失误
git reset: HEAD --> 暂存区
- reset 引用(推荐)
git reset --soft <commit>
Git友必看:http://progit.org/book/zh
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律