做吃的教程網(wǎng)站品牌整合營銷方案
目錄
一、初始配置
二、添加文件
三、查看日志
四、修改文件
五、版本回退
六、撤銷修改
七、刪除文件
一、初始配置
Git版本控制器:記錄每次的修改以及版本迭代的一個管理系統(tǒng)。
# 初始化本地倉庫:git init(base) [root@localhost gitcode]# git init
重新初始化現(xiàn)存的 Git 版本庫于 /root/gitee/gitcode/.git/
(base) [root@localhost gitcode]# ls -a
. .. .git
(base) [root@localhost gitcode]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs├── heads└── tags9 directories, 13 files
(base) [root@localhost gitcode]#
# 查看本地倉庫配置(base) [root@localhost gitcode]# git config -l
push.default=matching
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
(base) [root@localhost gitcode]#
# 配置當前倉庫(base) [root@localhost gitcode]# git config user.name "ljc"
(base) [root@localhost gitcode]# git config user.email "1210451061@qq.com"
(base) [root@localhost gitcode]# git config -l
push.default=matching
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=ljc
user.email=1210451061@qq.com
(base) [root@localhost gitcode]#
# 刪除當前倉庫配置(base) [root@localhost gitcode]# git config --unset user.name
(base) [root@localhost gitcode]# git config --unset user.email
(base) [root@localhost gitcode]# git config -l
push.default=matching
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
(base) [root@localhost gitcode]#
# 配置當前機器的全部倉庫(base) [root@localhost gitcode]# git config --global user.name "ljc"
(base) [root@localhost gitcode]# git config --global user.email "1210451061@qq.com"
(base) [root@localhost gitcode]# git config -l
push.default=matching
user.name=ljc
user.email=1210451061@qq.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true# 刪除當前機器的全部倉庫配置(base) [root@localhost gitcode]# git config --global --unset user.name
(base) [root@localhost gitcode]# git config --global --unset user.email
(base) [root@localhost gitcode]# git config -l
push.default=matching
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
(base) [root@localhost gitcode]#
二、添加文件
?作區(qū):是在電腦上你要寫代碼或?件的?錄。
暫存區(qū):英?叫 stage 或 index 。?般存放在?.git ?錄下的 index ?件(.git/index)中,我們把暫存區(qū)有時也叫作索引(index)。
版本庫:?名倉庫,英?名 repository 。?作區(qū)有?個隱藏?錄?.git ,它不算?作區(qū),?是 Git 的版本庫。這個版本庫??的所有?件都可以被 Git 管理起來,每個?件的修改、刪除,Git 都能跟蹤,以便任何時刻都可以追蹤歷史,或者在將來某個時刻可以“還原”。
- 創(chuàng)建 Git 版本庫時,Git會為我們?動創(chuàng)建?個唯?的 master 分?,以及指向 master 的?個指針叫 HEAD。
- 對?作區(qū)修改(或新增)的?件執(zhí)??git add 命令時,暫存區(qū)?錄樹的?件索引會被更新。
- 執(zhí)?提交操作 git commit 時,master 分?會做相應的更新,可以簡單理解為暫存區(qū)的?錄樹才會被真正寫到版本庫中。
# 添加一個文件(base) [root@localhost gitcode]# ls
(base) [root@localhost gitcode]# touch file1
(base) [root@localhost gitcode]# vim file1
(base) [root@localhost gitcode]# cat file1
hello git(base) [root@localhost gitcode]# git add file1
(base) [root@localhost gitcode]# git commit -m "Add first file"
[master(根提交) fc3a350] Add first file1 file changed, 2 insertions(+)create mode 100644 file1
(base) [root@localhost gitcode]#
# 添加多個文件(base) [root@localhost gitcode]# touch file2 file3 file4
(base) [root@localhost gitcode]# git add .
(base) [root@localhost gitcode]# git commit -m "Add three files"
[master f2e9210] Add three files3 files changed, 0 insertions(+), 0 deletions(-)create mode 100644 file2create mode 100644 file3create mode 100644 file4
(base) [root@localhost gitcode]# git log
commit f2e92108d0fe7ec01a6c49d1372e4907cac6d96b
Author: ljc <1210451061@qq.com>
Date: Wed Feb 7 05:13:08 2024 +0800Add three filescommit fc3a3507b30d2f4374a71245a034e00f94ea8363
Author: ljc <1210451061@qq.com>
Date: Wed Feb 7 05:11:56 2024 +0800Add first file
(base) [root@localhost gitcode]#
三、查看日志
# git log 命令顯?從最近到最遠的提交?志
# 加上 --pretty=online 參數(shù)會將日志簡潔顯示(base) [root@localhost gitcode]# git log --pretty=oneline
f2e92108d0fe7ec01a6c49d1372e4907cac6d96b Add three files
fc3a3507b30d2f4374a71245a034e00f94ea8363 Add first file
(base) [root@localhost gitcode]## 通過 git log 可以看到的長字符串是每次提交的commit id(版本號),是一個哈希值
# 查看 .git(base) [root@localhost gitcode]# tree .git
.git
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── logs
│ ├── HEAD
│ └── refs
│ └── heads
│ └── master
├── objects
│ ├── 2c
│ │ └── 0f71d14208bc896178ce4eb92870c659c04202
│ ├── 74
│ │ └── cbb01783907aa0807236331230386d5e1241cf
│ ├── 7f
│ │ └── 112b196b963ff72675febdbb97da5204f9497e
│ ├── e6
│ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│ ├── f2
│ │ └── e92108d0fe7ec01a6c49d1372e4907cac6d96b
│ ├── fc
│ │ └── 3a3507b30d2f4374a71245a034e00f94ea8363
│ ├── info
│ └── pack
└── refs├── heads│ └── master└── tags18 directories, 24 files
(base) [root@localhost gitcode]#
# index 是暫存區(qū),add 后的內(nèi)容會添加進緩存區(qū)
# HEAD 是默認指向 master 分支的指針
# 默認的 master 保存的就是最新的 commit id(base) [root@localhost gitcode]# cat .git/HEAD
ref: refs/heads/master
(base) [root@localhost gitcode]# cat .git/refs/heads/master
f2e92108d0fe7ec01a6c49d1372e4907cac6d96b
(base) [root@localhost gitcode]# # objects 為Git的對象庫,里面包含了創(chuàng)建的各個版本的對象及內(nèi)容。
# 當執(zhí)行 git add 的時候,暫存區(qū)的對象樹被更新,
# 同時工作區(qū)修改(或新增)的文件內(nèi)容被寫入到對象庫中的一個新的對象中,
# 就位于 ./git/objects 目錄下(base) [root@localhost gitcode]# ls .git/objects/
2c 74 7f e6 f2 fc info pack# 查找 object 要將 commit id 分為兩部分,前2位是目錄名稱,后38位文件名稱
# 使用 git cat-file 查看版本庫對象的內(nèi)容
# <類型> 可以是其中之一:blob、tree、commit、tag
# -t 顯示對象類型
# -s 顯示對象大小
# -e 當沒有錯誤時退出并返回零
# -p 美觀地打印對象的內(nèi)容
# --textconv 對于數(shù)據(jù)(blob)對象,對其內(nèi)容執(zhí)行 textconv
# --batch 顯示從標準輸入提供的對象的信息和內(nèi)容
# --batch-check 顯示從標準輸入提供的對象的信息(base) [root@localhost gitcode]# git cat-file -p f2e92108d0fe7ec01a6c49d1372e4907cac6d96b
tree 74cbb01783907aa0807236331230386d5e1241cf
parent fc3a3507b30d2f4374a71245a034e00f94ea8363
author ljc <1210451061@qq.com> 1707253988 +0800
committer ljc <1210451061@qq.com> 1707253988 +0800Add three files
(base) [root@localhost gitcode]# git cat-file -p 74cbb01783907aa0807236331230386d5e1241cf
100644 blob 7f112b196b963ff72675febdbb97da5204f9497e file1
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file2
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file3
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file4
(base) [root@localhost gitcode]# # 查看 file1 對應的 commit id[root@localhost gitcode]# git cat-file -p 7f112b196b963ff72675febdbb97da5204f9497e
hello git(base) [root@localhost gitcode]#
四、修改文件
Git版本控制器的本質:跟蹤并管理文件的修改,而非文件本身。
# 對file1文件內(nèi)容進行修改,此時工作區(qū)和暫存區(qū)的文件版本不一致
# 用 git status 查看在你上次提交之后是否有對文件進行再次修改(base) [root@localhost gitcode]# vim file1
(base) [root@localhost gitcode]# cat file1
hello git
hello world
(base) [root@localhost gitcode]# git status
# 位于分支 master
# 尚未暫存以備提交的變更:
# (使用 "git add <file>..." 更新要提交的內(nèi)容)
# (使用 "git checkout -- <file>..." 丟棄工作區(qū)的改動)
#
# 修改: file1
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
(base) [root@localhost gitcode]#
# git diff [file] 查看暫存區(qū)和工作區(qū)文件的具體差異
# git diff HEAD -- [file] 查看版本庫和工作區(qū)文件的具體差異(base) [root@localhost gitcode]# git diff file1
diff --git a/file1 b/file1
index 7f112b1..05fe86c 100644
--- a/file1
+++ b/file1
@@ -1,2 +1,2 @@hello git
-
+hello world
(base) [root@localhost gitcode]# git diff HEAD -- file1
diff --git a/file1 b/file1
index 7f112b1..05fe86c 100644
--- a/file1
+++ b/file1
@@ -1,2 +1,2 @@hello git
-
+hello world
(base) [root@localhost gitcode]#
(base) [root@localhost gitcode]# git add file1
(base) [root@localhost gitcode]# git status
# 位于分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區(qū))
#
# 修改: file1
#
(base) [root@localhost gitcode]# git commit -m "modify: file1"
[master 7df1e32] modify: file11 file changed, 1 insertion(+), 1 deletion(-)
(base) [root@localhost gitcode]# git status
# 位于分支 master
無文件要提交,干凈的工作區(qū)
(base) [root@localhost gitcode]#
五、版本回退
Git將所有提交過的版本串成一條時間線,若只有一條時間線,則這個分支就是主分支,即master分支。
對于master分支,每一次提交master分?都會向前移動?步,這樣,隨著你不斷提交,master分?的線也越來越?,?HEAD只要?直指向master分?即可指向當前分?。
當進行版本回退的時候,只需要改變master指針的指向,就完成了版本回退,非常高效。
# 給 file1 新增 vertion1 和 vertion2 兩個版本并分別提交(base) [root@localhost gitcode]# vim file1
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
(base) [root@localhost gitcode]# git add file1
(base) [root@localhost gitcode]# git commit -m "modify: add vertion1"
[master 167def0] modify: add vertion11 file changed, 1 insertion(+)
(base) [root@localhost gitcode]# vim file1
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
add vertion2
(base) [root@localhost gitcode]# git add file1
(base) [root@localhost gitcode]# git commit -m "modigy: add vertion2"
[master c31b56a] modigy: add vertion21 file changed, 1 insertion(+)
(base) [root@localhost gitcode]#
# 通過 git reset 回退版本
# 在進行版本回退之前,通常先用 git log 查看歷史版本(base) [root@localhost gitcode]# git log --pretty=oneline
c31b56a87a6387873d7db9a16f7d1c81b4b2339e modigy: add vertion2
167def04692b8f6fa68cc835f41a81584ca31b7e modify: add vertion1
7df1e322e9d267964f51e91dac900bb1a77f171d modify: file1
f2e92108d0fe7ec01a6c49d1372e4907cac6d96b Add three files
fc3a3507b30d2f4374a71245a034e00f94ea8363 Add first file
(base) [root@localhost gitcode]# git reset 167def04692b8f6fa68cc835f41a81584ca31b7e
重置后撤出暫存區(qū)的變更:
M file1
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
add vertion2
(base) [root@localhost gitcode]# git reset --hard 167def04692b8f6fa68cc835f41a81584ca31b7e
HEAD 現(xiàn)在位于 167def0 modify: add vertion1
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
(base) [root@localhost gitcode]# # 由上可見,直接使用 git reset 回退版本,工作區(qū)的文件內(nèi)容并未修改
# 因為回退版本的完整命令格式為:git reset [--soft | --mixed | --hard] [HEAD]# 不同的參數(shù)代表不同的回退方式:
# --soft 對于工作區(qū)和暫存區(qū)的內(nèi)容都不變,只將版本庫回退到指定版本
# --mixed 對于工作區(qū)的內(nèi)容不變,暫存區(qū)和版本庫回退到指定版本,默認參數(shù)
# --hard 對于工作區(qū)、暫存區(qū)和版本庫都回退到指定版本# [HEAD] 說明:
# 1. 可直接寫成 commid id,表示特定的版本
# 2. HEAD 表示當前版本
# 3. HEAD^ 表示上一個版本
# 4. HEAD^^ 表示上上個版本
# 5. ... 依此類推
# 當我回退到 vertion1 之后后悔,我想再回到 vertion2 怎么辦?
# 1. 直接用 vertion2 的 commit id 進行回退
# 2. 如果找不到 vertion2 的 commit id 了,通過 git reflog 查看本地的歷史命令,
# 可以得到 vertion2 的部分 commit id,也可直接回退(base) [root@localhost gitcode]# git reflog
167def0 HEAD@{0}: reset: moving to 167def04692b8f6fa68cc835f41a81584ca31b7e
c31b56a HEAD@{1}: commit: modigy: add vertion2
167def0 HEAD@{2}: commit: modify: add vertion1
7df1e32 HEAD@{3}: reset: moving to 7df1e322e9d267964f51e91dac900bb1a77f171d
3d8c2fe HEAD@{4}: commit: modify: add vertion1
7df1e32 HEAD@{5}: commit: modify: file1
f2e9210 HEAD@{6}: commit: Add three files
fc3a350 HEAD@{7}: commit (initial): Add first file
(base) [root@localhost gitcode]# git reset --hard c31b56a
HEAD 現(xiàn)在位于 c31b56a modigy: add vertion2
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
add vertion2
(base) [root@localhost gitcode]#
六、撤銷修改
版本回退是我們已經(jīng)提交了版本,更新了版本庫之后再進行回退操作,但是如果我們在開發(fā)過程中,新寫了很多代碼但是沒有 add ,這時候忽然發(fā)現(xiàn)自己寫的代碼全是屎山代碼,想要回到新增代碼前的版本,該怎么做呢?是先提交更新版本再版本回退嗎?
上述方式雖然也行,但是太麻煩了,可以直接用 git checkout -- [file] 命令讓工作區(qū)的文件回到最近一次 add 和 commit 時的狀態(tài)。
(base) [root@localhost gitcode]# vim file1
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
add vertion2
too much shit code!!!
(base) [root@localhost gitcode]# git status
# 位于分支 master
# 尚未暫存以備提交的變更:
# (使用 "git add <file>..." 更新要提交的內(nèi)容)
# (使用 "git checkout -- <file>..." 丟棄工作區(qū)的改動)
#
# 修改: file1
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
(base) [root@localhost gitcode]# git checkout -- file1
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
add vertion2
(base) [root@localhost gitcode]## 如果代碼已經(jīng) add 但還沒 commit 呢?怎么處理?
# 方法1:先 git reset [file] 再 git checkout --[file]
# 方法2:直接 git reset --hard [file](base) [root@localhost gitcode]# vim file1
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
add vertion2
too much shit code
(base) [root@localhost gitcode]# git add file1
(base) [root@localhost gitcode]# git status
# 位于分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區(qū))
#
# 修改: file1
#
(base) [root@localhost gitcode]# git log --pretty=oneline
c31b56a87a6387873d7db9a16f7d1c81b4b2339e modigy: add vertion2
167def04692b8f6fa68cc835f41a81584ca31b7e modify: add vertion1
7df1e322e9d267964f51e91dac900bb1a77f171d modify: file1
f2e92108d0fe7ec01a6c49d1372e4907cac6d96b Add three files
fc3a3507b30d2f4374a71245a034e00f94ea8363 Add first file
(base) [root@localhost gitcode]# git reset --hard c31b56a87a6387873d7db9a16f7d1c81b4b2339e
HEAD 現(xiàn)在位于 c31b56a modigy: add vertion2
(base) [root@localhost gitcode]# cat file1
hello git
hello world
add vertion1
add vertion2
(base) [root@localhost gitcode]#
七、刪除文件
刪除的本質也是修改,如果我們在工作區(qū)用 rm 命令刪除了一個文件,那么可能是兩種原因:
- 誤刪
- 確定要刪除該文件
# 如果是誤刪,那么我們需要取消刪除操作,也就是撤銷修改操作。
# 如果是確認要刪除該文件,那么通過 rm 刪除之后,工作區(qū)和暫存區(qū)、版本庫就不一致了。
# 我們該如何處理呢?
# 我們需要先將刪除后的工作區(qū)更新到暫存區(qū),再將暫存區(qū)提交到版本庫。(base) [root@localhost gitcode]# ls
file1 file2 file3 file4
(base) [root@localhost gitcode]# rm -rf file4
(base) [root@localhost gitcode]# ls
file1 file2 file3
(base) [root@localhost gitcode]# git status
# 位于分支 master
# 尚未暫存以備提交的變更:
# (使用 "git add/rm <file>..." 更新要提交的內(nèi)容)
# (使用 "git checkout -- <file>..." 丟棄工作區(qū)的改動)
#
# 刪除: file4
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
(base) [root@localhost gitcode]# git add .
warning: 您在運行 'git add' 時沒有指定 '-A (--all)' 或 '--ignore-removal',
針對其中本地移除路徑的行為將在 Git 2.0 版本庫發(fā)生變化。
像本地工作區(qū)移除的路徑 'file4'
在此版本的 Git 中被忽略。* 'git add --ignore-removal <pathspec>',是當前版本的默認操作,忽略您本地工作區(qū)中移除的文件。* 'git add --all <pathspec>' 將讓您同時對刪除操作進行記錄。運行 'git status' 來檢查您本地工作區(qū)中移除的路徑。(base) [root@localhost gitcode]# git add --all file4
(base) [root@localhost gitcode]# git commit -m "delete file4"
[master 0f28717] delete file41 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file4
(base) [root@localhost gitcode]# git status
# 位于分支 master
無文件要提交,干凈的工作區(qū)
(base) [root@localhost gitcode]# ls
file1 file2 file3
(base) [root@localhost gitcode]# # 由上可得,我們需要處理不一致問題,也就是將刪除后的新版本再次提交到版本庫
# 我們可以也通過 git rm 進行文件刪除,這樣直接就刪除了工作區(qū)和暫存區(qū)的文件
# 我們只需要接著 git commit -m 更新刪除后的版本就行了(base) [root@localhost gitcode]# ls
file1 file2 file3
(base) [root@localhost gitcode]# git rm file3
rm 'file3'
(base) [root@localhost gitcode]# ls
file1 file2
(base) [root@localhost gitcode]# git status
# 位于分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區(qū))
#
# 刪除: file3
#
(base) [root@localhost gitcode]# git commit -m "delete file3"
[master 84b615b] delete file31 file changed, 0 insertions(+), 0 deletions(-)delete mode 100644 file3
(base) [root@localhost gitcode]#