Git的基本概念與操作
=============================基本操作=================================
一、linux安裝git
yum install git
二、拉取專案
git clone https://github.com/sdwmbyy/test.git
三、檢視狀態
git status
四、進入目錄
cd test
五、新建“日記”檔案,新增一行“我今天吃了一個蘋果“ ,新增完後退出
vi 日記
六、檢視狀態:說明專案還沒提交
git status
[
# 位於分支 master
# 未跟蹤的檔案:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# "\346\227\245\350\256\260"
提交為空,但是存在尚未跟蹤的檔案(使用 "git add" 建立跟蹤)
七、為新新增的檔案建立跟蹤,並檢視狀態
git add 日記
[
[[email protected] test]# git status
# 位於分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 新檔案: "\346\227\245\350\256\260"
#
七、提交,並新增描述,“我新增了一個日記檔案”。
git commit
我新增了一個日記檔案
# 請為您的變更輸入提交說明。以 '#' 開始的行將被忽略,而一個空的提交
# 說明將會終止提交。
#
# 提交者: root <
#
# 位於分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 新檔案: "\346\227\245\350\256\260"
#
八、檢視狀態:說明專案已經得到提交
git status
我新增了一個日記檔案
# 請為您的變更輸入提交說明。以 '#' 開始的行將被忽略,而一個空的提交
# 說明將會終止提交。
#
# 提交者: root <[email protected]>
#
# 位於分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 新檔案: "\346\227\245\350\256\260"
#
~
~
".git/COMMIT_EDITMSG" 12L, 369C written
[master a944d48] 我新增了一個日記檔案
Committer: root <[email protected]>
您的姓名和郵件地址基於登入名和主機名進行了自動設定。請檢查它們正確
與否。您可以通過下面的命令對其進行明確地設定以免再出現本提示資訊:
git config --global user.name "Your Name"
git config --global user.email [email protected]
設定完畢後,您可以用下面的命令來修正本次提交所使用的使用者身份:
git commit --amend --reset-author
1 file changed, 1 insertion(+)
create mode 100644 "\346\227\245\350\256\260"
[[email protected] test]# git status
# 位於分支 master
# 您的分支領先 'origin/master' 共 1 個提交。
# (使用 "git push" 來發布您的本地提交)
#
無檔案要提交,乾淨的工作區
九、同步到GitHub
git push
Username for 'https://github.com': sdwmbyy
Password for 'https://[email protected]': 輸入自己的密碼
=============================基本操作end=================================
=============================處理衝突=================================
如果兩個人修改專案中某個檔案的同一個地方,第二個人push的時候
就會發生衝突。如下提示
[[email protected] test]# git push
warning: push.default 未設定,它的預設值將會在 Git 2.0 由 'matching'
修改為 'simple'。若要不再顯示本資訊並在其預設值改變後維持當前使用習慣,
進行如下設定:
git config --global push.default matching
若要不再顯示本資訊並從現在開始採用新的使用習慣,設定:
git config --global push.default simple
參見 'git help config' 並查詢 'push.default' 以獲取更多資訊。
('simple' 模式由 Git 1.7.11 版本引入。如果您有時要使用老版本的 Git,
為保持相容,請用 'current' 代替 'simple' 模式)
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com':
To https://github.com/sdwmbyy/test.git
! [rejected] master -> master (fetch first)
error: 無法推送一些引用到 'https://github.com/sdwmbyy/test.git'
提示:更新被拒絕,因為遠端版本庫包含您本地尚不存在的提交。這通常是因為另外
提示:一個版本庫已推送了相同的引用。再次推送前,您可能需要先合併遠端變更
提示:(如 'git pull')。
提示:詳見 'git push --help' 中的 'Note about fast-forwards' 小節。
1.下拉專案進行衝突合併
git pull
[[email protected] test]# git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
來自 https://github.com/sdwmbyy/test
7e802af..3eefd71 master -> origin/master
自動合併 我的大學
衝突(內容):合併衝突於 我的大學
自動合併失敗,修正衝突然後提交修正的結果。
2.檢視狀態
git status
[[email protected] test]# git status
# 位於分支 master
# 您的分支和 'origin/master' 出現了偏離,
# 並且分別有 1 和 1 處不同的提交。
# (使用 "git pull" 來合併遠端分支)
#
# 您有尚未合併的路徑。
# (解決衝突並執行 "git commit")
#
# 未合併的路徑:
# (使用 "git add <file>..." 標記解決方案)
#
# 雙方修改: "\346\210\221\347\232\204\345\244\247\345\255\246"
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
3.檢視差別
git diff
[[email protected] test]# git diff
diff --cc "\346\210\221\347\232\204\345\244\247\345\255\246"
index 2928822,5ef8c68..0000000
--- "a/\346\210\221\347\232\204\345\244\247\345\255\246"
+++ "b/\346\210\221\347\232\204\345\244\247\345\255\246"
@@@ -1,3 -1,3 +1,7 @@@
這是的高爾基的一本著名的小說
++<<<<<<< HEAD
+我初三的時候看過這本小說.
++=======
+ sdwmbyy:我沒看過,是啊,我根本沒有看過。
++>>>>>>> 3eefd71358ff0b9d3f092ab25b8569cf95dc5328
4.處理衝突,即選擇哪個提交作為最終版本
vi 我的大學
[[email protected] test]# vi 我的大學
這是的高爾基的一本著名的小說
<<<<<<< HEAD
我初三的時候看過這本小說.
=======
sdwmbyy:我沒看過,是啊,我根本沒有看過。
>>>>>>> 3eefd71358ff0b9d3f092ab25b8569cf95dc5328
5.將我的大學檔案的內容變為,即選擇第二次提交作為最終版本
這是的高爾基的一本著名小說
我初三的時候看過這本小說.
6.檢視狀態
git status
7.新增變更
git add 我的大學
8.提交
git commit
我push時,我的大學有衝突,我的選擇我的版本為最終版本
9.上傳:
git push
=============================處理衝突end=================================
=============================回到過去=================================
為什麼要這麼做:因為有些從Github上pull過來的專案可能被某些人改得面目全非。
因此,需要回到pull之前的版本上去。
1.檢視日誌,即之前進行了哪些提交
git log
commit 43a31f9c01833f807de3c899382ac3efec4f7536
Merge: 50ac9da 3eefd71
Author: root <[email protected]>
Date: Fri Aug 24 19:29:33 2018 +0800
我push時,我的大學有衝突,我的選擇我的版本為最終版本
Conflicts:
我的大學
commit 50ac9dab5087bf2ca6be9daecfc50593e81a7684
Author: root <[email protected]>
Date: Fri Aug 24 19:20:08 2018 +0800
我曾經看過這本書
commit 3eefd71358ff0b9d3f092ab25b8569cf95dc5328
Author: root <[email protected]>
Date: Fri Aug 24 11:18:33 2018 +0000
是啊,我確實沒有看過哦。
commit 7e802afd54390c2536a96790bf5131a207df7f3b
Merge: 0387f38 8406b11
Author: root <[email protected]>
Date: Fri Aug 24 11:09:27 2018 +0000
Merge branch 'master' of https://github.com/sdwmbyy/test
發生了衝突,我選擇第二次修改的版本為最終版本。
Conflicts:
我的大學
commit 0387f380aa7f959b67ad61535ee92a03c7395ebf
Author: root <[email protected]>
Date: Fri Aug 24 10:53:02 2018 +0000
HowardHub:這本小說還可以
commit 8406b117f1636ba5c45645bf5355ec0f21672756
Author: root <[email protected]>
Date: Fri Aug 24 18:51:16 2018 +0800
我對“我的大學”進行了修改,
commit 90bbf2566d175f9d186a15a3cd43bc115f96c351
Author: root <[email protected]>
Date: Fri Aug 24 18:11:41 2018 +0800
sdwmbyy:梨好吃。
2.根據某個時間點的coomit id,回到那個版本。hard代表了當前程式碼的版本的指標
git reset --hard 7e802afd54390c2536a96790bf5131a207df7f3b
3.回到未來
git reflog #列出當前版本號之前的版本
[[email protected] test]# git reflog
3eefd71 [email protected]{0}: reset: moving to 3eefd71358ff0b9d3f092ab25b8569cf95dc5328
43a31f9 [email protected]{1}: commit (merge): 我push時,我的大學有衝突,我的選擇我的版本為最終版本
50ac9da [email protected]{2}: commit: 我曾經看過這本書
7e802af [email protected]{3}: pull: Fast-forward
8406b11 [email protected]{4}: commit: 我對“我的大學”進行了修改,
90bbf25 [email protected]{5}: commit: sdwmbyy:梨好吃。
7100f7b [email protected]{6}: pull: Merge made by the 'recursive' strategy.
f7ee1f8 [email protected]{7}: commit: 新添加了一記檔案“我的大學”
a944d48 [email protected]{8}: commit: 我新增了一個日記檔案
e76129b [email protected]{9}: clone: from https://github.com/sdwmbyy/test.git
git reset --hard
[[email protected] test]# git reset --hard 3eefd71
HEAD 現在位於 3eefd71 是啊,我確實沒有看過哦。
=============================回到過去end=================================
=============================建立里程碑==================================
點選專案名-->點選release-->點選Draft a new release-->輸入里程碑相關資訊-->
-->完成釋出。
之後,如果這個版本有bug,就可以在此基礎上進行修改。
=============================建立里程碑end=================================
=============================分支開發與合併==================================
1.使用場景:昨天釋出的版本中,出現了一個bug,需要馬上進行修復,而master分
支已經推送了新的功能程式碼,所以不能在master分支上進行立刻釋出(如果釋出,就會
把沒有測試過的程式碼待到外面去,會有更多bug產生)。
2.解決辦法:用已經發布的程式碼為基礎,建立一個分支,對bug進行修改。修改完成後,把
它合併到正在開發的分支上。這樣在不影響現有程式碼的情況下,對bug進行處理。
3.操作流程:新建分支-->修改bug-->將分支合併到master
=============================分支開發與合併end==================================
=============================其它經驗==================================
1.多用網站和客戶端,少用命令列
2.每次提交前,diff自己的程式碼,對diff部分進行仔細檢查,避擴音交錯誤的程式碼。
3.下班前,整理好自己的工作區。把要提交的程式碼提交,把不需要的程式碼刪除。
4.並行專案,使用分支進行開發
5.遇到衝突時,要搞明白衝突的原因,不要隨意丟棄別人的程式碼。
6.釋出時,記得打tag,方便將來拉分支修bug
=============================其它經驗==================================