git 簡明使用手冊

1. git 簡明使用手冊

1.1. 簡介

Linux 內核開源項目有着爲數衆多的參與者。 絕大多數的 Linux 內核維護工作都花在了提交補丁和保存歸檔的繁瑣事務上(1991-2002年間)。 到 2002 年,整個項目組開始啓用一個專有的分佈式版本控制系統 BitKeeper 來管理和維護代碼。

到了 2005 年,開發 BitKeeper 的商業公司同 Linux 內核開源社區的合作關係結束,他們收回了 Linux 內核社區免費使用 BitKeeper 的權力。 這就迫使 Linux 開源社區(特別是 Linux 的締造者 Linus Torvalds)基於使用 BitKeeper 時的經驗教訓,開發出自己的版本系統Git.

git在當時的背景下開發出來,擁有以下特性:

  1. 對非線性開發模式的強力支持(允許成千上萬個並行開發的分支)
  2. 完全分佈式
  3. 有能力高效管理類似 Linux 內核一樣的超大規模項目(速度和數據量)

1.2. git 工作流

avatar

1.3. 配置文件

1.4. 獲取git倉庫

1.4.1. 從現有項目或目錄下導入所有文件到 Git 中

cd /home/zhangrj/git_experiment/clone
mkdir -p clone.init && cd clone.init
git init && git remote add origin /home/zhangrj/git_experiment/server/bare.repo
git pull origin master

1.4.2. 從一個服務器克隆一個現有的 Git 倉庫

  • 格式:
    • git clone [url]
    • git clone -b [branch] [url] 克隆指定分支
    • git clone -b [branch] [url] -o [name] 自定義倉庫名稱
cd /home/zhangrj/git_experiment/clone
git clone -b master /home/zhangrj/git_experiment/server/bare.repo bare.clone.init

1.5. 提交,暫存,狀態,移除,移動,提交歷史

avatar

1.5.1. 檢查當前文件狀態

使用 git status 命令

1.5.2. 跟蹤新文件

使用命令git add開始跟蹤一個文件,會將文件添加到下一次的提交中,此時文件處於暫存狀態.

[root@localhost bare]# echo "CONTRIBUTING" > CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	CONTRIBUTING.md
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost bare]# git add CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   CONTRIBUTING.md
#

1.5.3. 暫存已修改文件

如果你修改了一個名爲 CONTRIBUTING.md 的已被跟蹤的文件,然後運行 git status 命令,會看到下面內容

[root@localhost bare]# cat CONTRIBUTING.md 
CONTRIBUTING
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   CONTRIBUTING.md
#
[root@localhost bare]# echo "modify" >> CONTRIBUTING.md 
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   CONTRIBUTING.md
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   CONTRIBUTING.md
#

Changes not staged for commit 這行下面,說明已跟蹤文件的內容發生了變化,但還沒有放到暫存區,要暫存這次更新,需要運行 git add 命令.

[root@localhost bare]# git add CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   CONTRIBUTING.md
#
[root@localhost bare]# 

1.5.4. 提交更新

  • 使用命令 git commit -m 提交處於暫存狀態的文件
  • 使用命令 git commit -a -m 跳過使用暫存區域的方式,自動把所有已經跟蹤過的文件暫存起來一併提交

1.5.5. 移除

  • git rm 文件名稱 : 該文件就不再納入版本管理了.
  • git rm -f 文件名稱 : 如果刪除之前修改過並且已經放到暫存區域的話,則必須要用強制刪除選項 -f,該選項會連帶將本地文件一起刪除.
  • git rm --cached 文件名稱 : 只是將文件從倉庫刪除,本地仍然還存在該文件.
[root@localhost bare]# ls
CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost bare]# git rm CONTRIBUTING.md
rm 'CONTRIBUTING.md'
[root@localhost bare]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    CONTRIBUTING.md
#
[root@localhost bare]# ls
[root@localhost bare]# git commit -m "remove file CONTRIBUTING.md"
[master 279f415] remove file CONTRIBUTING.md
 Committer: root <[email protected]>

1.5.6. 提交歷史

  • git log 查看提交歷史
[root@localhost init]# git log --pretty=format:'%cd %cr %s' --graph
* Fri Sep 21 16:18:16 2018 +0800 4 months ago 修改下發指令的topic錯誤的問題
* Wed Sep 12 16:32:13 2018 +0800 4 months ago 修改反向控制,p2p報文格式,去掉轉義的""
* Wed Jun 20 11:01:48 2018 +0800 7 months ago 更新CHANGELOG.md
* Wed Jun 20 10:36:29 2018 +0800 7 months ago add some fixes
* Sat May 19 10:13:55 2018 +0800 8 months ago modify default in config file and add fault judge in business/business.go
* Fri May 18 21:28:14 2018 +0800 8 months ago 修改讀取配置文件的方式
* Fri May 18 13:01:36 2018 +0800 8 months ago 添加消費redis p2p消息的log
* Fri May 18 11:35:06 2018 +0800 8 months ago 修改反向控制指令邏輯
* Thu May 17 20:00:33 2018 +0800 8 months ago 修改business包的導入路徑
* Thu May 17 16:34:05 2018 +0800 8 months ago 修改配置文件名稱爲fido.conf
* Thu May 17 16:18:16 2018 +0800 8 months ago 添加fido倉庫代碼

  • git show shaID 查看某次提交的詳細信息
[root@localhost init]#  git log --abbrev-commit --pretty=oneline 
424e897 修改下發指令的topic錯誤的問題
1cdc804 修改反向控制,p2p報文格式,去掉轉義的""
1f9670a 更新CHANGELOG.md
2739a0f add some fixes
9419328 modify default in config file and add fault judge in business/business.go
57f160d 修改讀取配置文件的方式
f067f2f 添加消費redis p2p消息的log
a2ceb00 修改反向控制指令邏輯
cd7a9e7 修改business包的導入路徑
3a8871e 修改配置文件名稱爲fido.conf
a95b488 添加fido倉庫代碼
[root@localhost init]# git show 424e897
commit 424e897eb56eab01ddd6900d560bd0bd2487bc73
Author: zhangrj <[email protected]>
Date:   Fri Sep 21 16:18:16 2018 +0800

    修改下發指令的topic錯誤的問題

diff --git a/business/business.go b/business/business.go
index 3aa6fca..f2e4206 100755
--- a/business/business.go
+++ b/business/business.go
@@ -71,7 +71,7 @@ func MQttPublisher() {
                Log.Error("get mqtt null publisher")
                return
        }
-       topic := publisher.GetTopic()
+       //topic := publisher.GetTopic()
 
        for {
                select {
@@ -87,6 +87,7 @@ func MQttPublisher() {
                                continue
                        }
                        payload := a["payload"].(string)
+                       topic := a["topic"].(string)
                        publisher.Publish(topic, []byte(payload))
                        subdebugenable, _ := Conf.Bool("redis::redis.sub.debug.enable")
                        if subdebugenable {
[root@localhost init]#

1.6. 撤銷操作

1.6.1. 撤銷操作

有時候我們提交完了才發現漏掉了幾個文件沒有添加,或者提交信息寫錯了,可以運行帶有 --amend 選項的提交命令嘗試重新提交

[root@localhost remote]# cat remote_test.txt 
remote_test
append
[root@localhost remote]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost remote]# git log --pretty=oneline
825a4bf8c6a3d431405bc8a982ba02ea10ffb6cb append
[root@localhost remote]# echo "append and amend" >> remote_test.txt 
[root@localhost remote]# cat remote_test.txt 
remote_test
append
append and amend
[root@localhost remote]# git add remote_test.txt
[root@localhost remote]# git commit --amend --allow-empty -m "append and amend"
[root@localhost remote]# git log --pretty=oneline
8ba306c51bcf29ce9e806b06a65b1a459ea0344e append and amend
825a4bf8c6a3d431405bc8a982ba02ea10ffb6cb append 

1.6.2. 取消暫存的文件

使用 git reset 命令 取消暫存的文件,將處於暫存狀態的文件回到未追蹤狀態

[root@localhost remote]# touch reset_test.txt
[root@localhost remote]# 
[root@localhost remote]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	reset_test.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost remote]# git add reset_test.txt
[root@localhost remote]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   reset_test.txt
#
[root@localhost remote]# git reset reset_test.txt
[root@localhost remote]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	reset_test.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost remote]# 

1.6.3. 撤消對文件的修改

如果你並不想保留對文件的修改怎麼辦?你可以使用 git checkout – [file] 方便地撤消修改 - 將它還原成上次提交時的樣子

[root@localhost remote]# cat remote_test.txt 
remote_test
append
append and amend
[root@localhost remote]# echo "checkout" >> remote_test.txt 
[root@localhost remote]# cat remote_test.txt 
remote_test
append
append and amend
checkout
[root@localhost remote]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   remote_test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost remote]# git checkout -- remote_test.txt
[root@localhost remote]# cat remote_test.txt 
remote_test
append
append and amend
[root@localhost remote]# 

1.7. 打標籤

1.7.1. 列出標籤

使用命令 git tag 列出標籤

cd /home/zhangrj/git_experiment/clone

1.7.2. 創建標籤

1.7.2.1. 輕量標籤/附註標籤

  1. 輕量標籤很像一個不會改變的分支 - 它只是一個特定提交的引用.
  2. 附註標籤是存儲在 Git 數據庫中的一個完整對象.它們是可以被校驗的;其中包含打標籤者的名字,電子郵件地址,日期時間;還有一個標籤信息.
cd /home/zhangrj/git_experiment/clone
git clone [email protected]:/home/zhangrj/git_experiment/server/bare.repo repo
cd repo
# 輕量標籤
git tag light-tag
# 賦值標籤
# -m 選項指定了一條將會存儲在標籤中的信息,沒有指定-m,git會要求你輸入信息。
git tag -a v1.4 -m 'my version 1.4'

1.7.2.2. 後期打標籤

就是針對過去的提交shaid打標籤

[root@localhost bare]# git tag
[root@localhost bare]# git log --pretty=oneline
d66518f7faaad8bebb8a164844cf01c1c0adcfae 12
825a4bf8c6a3d431405bc8a982ba02ea10ffb6cb append and amend
5cf375122164eb4482ca6bf36a68b37f857b635f 添加amend_test.txt文件
f507912b82acd9b2279960b46084ce07643eae77 添加新文件remote_test.txt
279f415fee6e3ee76cc91ae864ba62ac2bdfeee8 remove file CONTRIBUTING.md
cbeafac429b62acf951263e7d70faa23ea83e190 添加新文件
[root@localhost bare]# git tag -a v0.1 cbeafac429b62acf951263e7d70faa23ea83e190
[root@localhost bare]# git tag
v0.1

1.7.3. 共享標籤

  • 將本地tag push到遠程倉庫上
  • git push [remote-url] [tag-name]
[root@localhost bare]# git tag
v0.1
[root@localhost bare]# git remote -v
origin	[email protected]:/home/zhangrj/git_experiment/server/bare (fetch)
origin	[email protected]:/home/zhangrj/git_experiment/server/bare (push)
[root@localhost bare]# git status
[root@localhost bare]# git push origin v0.1

1.7.4. 刪除標籤

  • git tag -d [tag-name]
[root@localhost bare]# git tag
v0.1
[root@localhost bare]# git tag -d v0.1
Deleted tag 'v0.1' (was cd7aa76)
[root@localhost bare]# git tag

1.7.5. 獲取遠程標籤

  • git fetch [remote-url] tag tagname
[root@localhost bare]# git remote -v
origin	[email protected]:/home/zhangrj/git_experiment/server/bare (fetch)
origin	[email protected]:/home/zhangrj/git_experiment/server/bare (push)
[root@localhost bare]# git tag
[root@localhost bare]# git fetch origin tag v0.1
[root@localhost bare]# git tag
v0.1

1.8. 分支

1.9. git協議

本地協議

  • 本地協議就是文件協議,通過文件地址來訪問倉庫
cd /home/zhangrj/git_experiment/clone
git clone /home/zhangrj/git_experiment/server/bare.repo bare.clone.local_protocol

ssh 協議

http/https協議

git協議

  1. 安裝 git damon 軟件
yum install git-daemon
  1. 命令行運行
git daemon --verbose --export-all --base-path=/home/repo/pub --reuseaddr --enable=receive-pack --detach
# --detach:daemon方式運行
# --base-path:指定倉庫地址
# --enable=receive-pack:允許client能夠push
  1. 客戶端可以使用 git協議 拉取代碼 git clone git://server-ip/XX.git
[root@localhost bare]# pwd
/home/zhangrj/git_experiment/server/bare
[root@localhost bare]# ls
branches  config  description  HEAD  hooks  info  objects  refs
[root@localhost bare]# cd .. && git daemon --verbose --export-all --base-path=`pwd` --reuseaddr  --enable=receive-pack --detach
[2] 31786
[root@localhost bare]# cd /home/zhangrj/git_experiment/clone/
[root@localhost clone]# pwd
/home/zhangrj/git_experiment/clone
[root@localhost clone]# ls
fido.repo  init  remote  tag
[root@localhost clone]# git clone git://127.0.0.1/bare
Cloning into 'bare'...
remote: Counting objects: 23, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 23 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (23/23), done.
Resolving deltas: 100% (2/2), done.
[root@localhost clone]# ls
bare  fido.repo  init  remote  tag

1.10. 遠程倉庫

1.10.1. 查看遠程倉庫

git remote -v -v會顯示需要讀寫遠程倉庫使用的Git保存的簡寫與其對應的URL

1.10.2. 添加遠程倉庫

git remote add fido http://192.168.88.4/fidis/FIDATA/fido.git --> 使用fido代替後面的url

1.10.3. 從遠程倉庫拉取

git fetch [remote-name]

1.10.4. 推送到遠程倉庫

git push [remote-name] [branch-name]

mkdir -p /home/zhangrj/git_experiment/{server,clone}
cd /home/zhangrj/git_experiment/clone
mkdir -p remote && cd remote
git init
git remote -v
git remote add bare [email protected]:/home/zhangrj/git_experiment/server/bare.repo
git remote -v
git pull bare master
或者
git fetch --all && git reset --hard bare

1.10.5. 遠程倉庫的重命名

git remote rename [old] [new]

1.10.6. 遠程倉庫的移除

git remote rm [remote-url](or remote-name)

1.11. 創建倉庫服務器

  • 遠程倉庫實際上和本地倉庫沒啥不同,純粹爲了7x24小時開機並交換大家的修改.
  • 使用git的倉庫大部分都是遵循開放協議的,源代碼開源的;如果不想公開源代碼,就只能自己搭建一臺Git服務器作爲私有倉庫使用。

1.11.1. 示例

  1. 創建一個沒有源代碼的裸倉庫
mkdir -p /home/zhangrj/git_experiment/{server,clone}
cd /home/zhangrj/git_experiment/server
git init --bare --shared bare.repo

cd /home/zhangrj/git_experiment/clone
git clone [email protected]:/home/zhangrj/git_experiment/server/bare.repo bare.clone

  1. 把現有倉庫導出爲裸倉庫
cd /home/zhangrj/git_experiment/server
git clone [email protected]:/home/zhangrj/git_experiment/server/bare.repo --bare --shared bare.repo.2

cd /home/zhangrj/git_experiment/clone
git clone [email protected]:/home/zhangrj/git_experiment/server/bare.repo.2 bare.clone.2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章