基於Django快速開發可定制的辦公系統實戰(1):Git的使用
?為什麽在項目的開篇要介紹下git的使用呢?俗話說:“工欲善其事,必先利其器”,git工具就是項目開發的必備利器,尤其是在多人協作開發環境中。
使用git工具可實現分布式的版本控制,可在服務端和本地創建一個版本庫。
?腦圖是本文的“脊椎”,了解個大概後,再通讀本文,再加上實際的操作,效果會更好,那我們就開始吧!
1 Git工具安裝
Windows版本安裝:
- 安裝包下載地址:https://gitforwindows.org/
使用安裝包完成安裝後,git工具會自動安裝git bash 工具(打開開始菜單,找到"Git"->"Git Bash"),會彈出git命令窗口,在命令窗口下可以進行git操作,同時鼠標右鍵也可以快速打開git bash
其它平臺版本安裝:
- http://www.runoob.com/git/git-install-setup.html
2 用戶配置
配置個人用戶名稱和電子郵件
$ git config --global user.name "RobbieHan"
$ git confit --global user.email [email protected]
查看配置信息
$ git config --list
3 Git工作流程
- 克隆Git資源作為工作目錄(在項目中會介紹到使用git從github克隆項目文件來進行開發工作)
- 在克隆的資源上添加或修改文件
- 提交前查看修改
- 提交項目修改文件
- 撤回修改
4 Git創建倉庫
使用當前目錄作為倉庫
$ git init
使用指定目錄作為Git倉庫
$ git init myproject
初始化後,會在我們創建的倉庫目錄下生成一個.get的隱藏目錄,所有Git相關數據和資源都在這個目錄中。
5 Git基本操作
git clone:
使用git clone拷貝一個Git倉庫到本地,例如從github上克隆git-demo項目
$ git clone https://github.com/RobbieHan/git-demo.git
# ssh方式 git clone [email protected]:RobbieHan/git-demo.git
克隆完成後,會在當前目錄下生成一個git-demo的項目目錄,接下來我們就可以查看git-demo項目文件,修改代碼了.
git add :
使用git add 命令將文件添加到緩存,我們在git-demo項目中創建一個test.py文件,然後使用git add 將文件添加到緩存
$ touch test.py
$ ls
README.md test.py
$ git status -s
?? test.py
其中git status命令是用於查看項目當前狀態,接下來使用git add命令來添加文件:
$ git add test.py
$ git status -s
A test.py
這時候在使用git status命令可以看到 test.py文件已經成功添加到緩存。<br>
git diff: <br>
查看尚未緩存的改動(已經修改變更尚未執行git add的文件):git diff <br>
$ echo "# This is test.py" > test.py
$ git diff
diff --git a/test.py b/test.py
index e69de29..0c029d2 100644
--- a/test.py
+++ b/test.py
@@ -0,0 +1 @@
從上面輸出結果可以看出git diff 會一行行顯示還未提交到緩存的具體變動內容。<br>
查看已緩存成功的改變:git diff --cached
$ git add .
$ git diff --cached
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..0c029d2
--- /dev/null
+++ b/test.py
@@ -0,0 +1 @@
+# This is test.py
上面使用了 git add . 是提交所有項目文件到緩存,執行git diff --cached 可以看到已經緩存成功的改變,包括新建 test.py文件 ,將內容寫入 test.py
git commit:
使用git commit 將存入緩存的快照添加到倉庫中
$ git commit -m ‘這是一條描述信息:將緩存快照添加到倉庫‘
The file will have its original line endings in your working directory.
1 file changed, 1 insertion(+)
create mode 100644 test.py
$ git status
nothing to commit, working directory clean
將緩存中的快照提交到倉庫後,再來執行git status 可以看到已經沒有需要提交的改動信息了。
git checkout -- file:
使用git checkout -- file 用來撤銷修改,將文件恢復到上一個版本,比如將一些無用或錯誤的數據寫到項目文件了,這時可以使用git checkout來撤銷修改
$ echo "這是一條錯誤的數據" > test.py
cat test.py
$ cat test.py
這是一條錯誤的數據
$ git status
On branch master
Your branch is ahead of ‘origin/master‘ by 1 commit.
(use "git push" to publish your local commits)
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: test.py
no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout -- test.py
$ cat test.py
# This is test.py
可以看到使用 git checkout -- file 命令已經將 test.py恢復到之前版本
上面的操作是我們用來恢復工作區的錯誤操作,如果已經將工作區的錯誤操作通過 git add 添加到了緩存區怎麽辦呢?可以使用 git reset HEAD file 來撤銷緩存區的修改。
6、git分支管理
使用git分支可以讓你從開發主線上分離出來,然後在不影響主線的同時進行開發工作。
創建分支:
$ git checkout -b dev # -b 表示創建並切換到該分支
Switched to a new branch ‘dev‘
查看分支:
$ git branch
* dev # 當前分支前面會顯示一個 * 號
master
接下來我們對項目文件的修改都只會在dev分支上生效,例如給test.py文件添加一行內容
$ echo "dev test" >> test.py
$ git add test.py
$ git commit -m "branch dev test"
1 file changed, 1 insertion(+), 1 deletion(-)
以上的修改操作不會影響到master分支的文件,切換到master分支查看test.py
$ git checkout master
Switched to branch ‘master‘
$ cat test.py
# This is test.py
當然,我們在dev 分支完成的開發工作可以合並到master分支:
$ git merge dev
Updating 459d678..a4a069b
Fast-forward
test.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
合並分之後master分支的項目內容就和dev一樣了。
分支管理的命令小結:
創建分支:git branch <name>
查看分支:git branch
切換分支: git checkout <name>
合並分支到當前分支:git merge <name>
刪除分支:git branch -d <name>
合並沖突:
切換到dev分支,在test.py裏面追加一行內容:"This is branch dev"
$ echo "This is branch dev" >> test.py
$ git add test.py
$ git commit -m "dev"
切換到master分支, 在test.py裏面追加一行內容:"This is branch master"
$ git checkout master
$ echo "This is branch master" >> test.py
$ git commit -am test.py
現在 dev 和master兩個分支都對文件test.py做了改動,接下來合並分支就是出現沖突:
$ git merge devAuto-merging test.py
CONFLICT (content): Merge conflict in test.py
Automatic merge failed; fix conflicts and then commit the result.
使用git status查看存在沖突的文件:
$ git status
On branch master
Your branch is ahead of ‘origin/master‘ by 3 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: test.py
no changes added to commit (use "git add" and/or "git commit -a")
接下來我們需要手動去修改沖突內容:
$ vim test.py
dev test
<<<<<<< HEAD
This is branch master
=======
This is branch dev
>>>>>>> dev
可以看到裏面test.py裏面已經標記了兩個分支修改的內容,手動修改下沖突的內容,保存文件。
$ git add test.py
$ git commit -m "master"
沖突合並完成。
7 Git標簽:
在做程序開發時,經常會發布維護多個版本,這時就可以用到標簽(tag),需要用到某個版本時,根據標簽就可以獲取對應的版本。<br>
給當前分支打上一個標簽:
$ git tag v1.0
v1.0
如果在代碼提交到git倉庫的時候忘記打標簽了,可以追加標簽
$ git log --oneline --decorate --graph
* 97b7808 (HEAD -> master, tag: v1.0) Merge branch ‘dev‘
|| * 3bbe270 (dev) dev
* | 0bad6a6 test.py
|/
* a4a069b branch dev test
* 459d678 這是一條描描述信息‘
* a7968a0 (origin/master, origin/HEAD) git demo
$ git tag v0.1 a7968a0 # 通過日誌找到commit id 然後通過 commit id來追加標簽
使用指定的tag來生成分支
git checkout -b <branch_name> <tag_name>
git checkout -B <branch_name> <tag_name> # 如果分支已經存在使用 -B 可以強制創建分支,覆蓋原來的分支
git標簽命令:
查看標簽:git tag
查看標簽詳細信息:git show <tagname>
指定標簽信息:git tag -a < tagname> -m <tag_desc>
刪除一個標簽:git tag -d <tagname>
8 使用遠程倉庫
在git bash命令窗口生成密鑰文件
$ ssh-keygen -t rsa -C "[email protected]"
上面郵箱換成github註冊郵箱,然後一直回車,系統會在用戶目錄下生成.ssh文件夾,打開id_rsa.pub,復制裏面的key。
vim ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCx984lhTT0nlW3QWTZBiO4WT3BtdOq7OGfgbWlT2NhcW0Uqj7UkMexG4dseUxz4L2TMDjTJIhRdMX+n5Qq1qQwsoQoA3AXg1TxEEZZQ9JJNcanQG58X09235XsIYwAGZbnoixjNAMbV1aa+oLafKvL3InQav+P0Xj38tWVuuJqjtt+QQCiEx8W828N6etR/dcoDw4Isa1k9Hntn4qoY+GqRnkcyBY4aIXMcrakghFcDVH5XlPBndXXRLm06VGtIgpqX82+2gXo0Pp4+p0LMTRnDVxE1fjJy2FwnQf4LkNgeDr7o+DMbTEpPgSKogI9kFpwnbCZjjab [email protected]
登陸github網站進入-settings選擇 SSH and GPG keys ,然後選擇 New SSH key, 填寫 title, 在key選項裏面填入剛剛復制的key, 保存添加。
本地通過git bash 測試密鑰鏈接是否成功
$ ssh -T [email protected]
Hi RobbieHan! You‘ve successfully authenticated, but GitHub does not provide shell access.
在github上新建一個倉庫,倉庫名為:gitdemo, 將本地倉庫提交到github
$ git remote add origin [email protected]:RobbieHan/git-demo.git
$ git push -u origin master
上傳分支到github
$ git push -u origin dev
上傳標簽到github
$ git push origin v1.0 # 上傳一個tag
$ git push origin --tags # 上傳全部tag
我們從github上克隆了一個項目,如果這個項目更新了,可以使用兩條命令來獲取更新
$ git fetch origin master # 從遠程倉庫下載新的分支數據
$ git merge # 合並到本地當前分支
克隆某個分支到本地:git clone -b <branch name> [remote repository address]
查看當前遠程庫:git remote -v
刪除遠程倉庫: git remote rm [別名]
- 開源項目:
- 輕量級辦公管理系統(乙方流程版)
- https://github.com/RobbieHan/gistandard
- 輕量級辦公管理系統(甲方定制版,本記錄同步項目)
- https://github.com/RobbieHan/sandboxOA
安裝部署交流:83792608(QQ群)
需求定制:1595574341(QQ)
進階開發交流:t.zsxq.com/zFa6Ei2 (知識星球)
基於Django快速開發可定制的辦公系統實戰(1):Git的使用