GIT版本管理-創(chuàng)新互聯(lián)

本文為個(gè)人整理筆記,參考與廖雪峰老師的官方GIT教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比永善網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式永善網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋永善地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。

在之前的GIT入門介紹中,我們了解了本地一些GIT操作的命令和原理,在這一節(jié)將一起學(xué)習(xí)一下GIT遠(yuǎn)程管理和版本管理的相關(guān)內(nèi)容。

遠(yuǎn)程倉庫

如果要在github上管理我們的代碼,并在本地進(jìn)行修改提交就需要了解git 遠(yuǎn)程倉庫的相關(guān)內(nèi)容。github是git官方提供的一個(gè)代碼托管平臺(tái),個(gè)人可以申請(qǐng)免費(fèi)的,但是所上傳的內(nèi)容可以方便大家共享,如果要使用私密倉庫,需要付費(fèi)。

在github上添加SSH KEY:

在github上注冊好賬戶之后,添加本機(jī)的SSH公鑰,這樣在本地在對(duì)遠(yuǎn)程倉庫操作時(shí)就不用頻繁的使用賬號(hào)密碼。

ssh-keygen -t rsa -C "youremail@example.com"

一路默認(rèn)回車之后,cat .ssh/id_rsa.pub ,然后將cat的內(nèi)容添加到賬戶的SSH Key中。

創(chuàng)建第一個(gè)個(gè)人倉庫

首先在github上注冊一個(gè)個(gè)人賬戶,在New repository中直接輸入新建倉庫的名稱即可。根據(jù)提示使用SSH的方法在本地添加一個(gè)個(gè)人倉庫:

git remote add origin git@github.com:YourName/gitrepo.git
git push -u origin master  # -u更新所有分支 origin默認(rèn)遠(yuǎn)程倉庫名稱

當(dāng)?shù)谝淮瓮搅怂械谋镜匚募?,在?duì)文件做了commit,就可以直接使用:

git push origin master

這樣就直接將本地更新的文件推到了遠(yuǎn)程管理庫。

從遠(yuǎn)程倉庫克隆

github上有很多很優(yōu)秀的代碼和應(yīng)用,如果我們要是用別人的代碼,直接可以用git clone命令將代碼拉到我們本地,如果是自己的代碼庫可以直接拉?。?/p>

 git clone git@github.com:YourName/gitrepo.git

分支管理

在進(jìn)行開發(fā)的過程中,很多情況下我們需要對(duì)分支進(jìn)行管理。例如,如果是本地對(duì)一個(gè)文件進(jìn)行修改,那么這個(gè)文件就是線性的修改方式,如果要對(duì)多個(gè)文件進(jìn)行多次修改,而且不同的修改最終會(huì)確認(rèn)一個(gè)最終的版本,最終合并的這個(gè)分支就是這個(gè)文件的最終版本,需要注意的是,只有當(dāng)執(zhí)行了commit 命令之后,本地的master分支才會(huì)建立。

創(chuàng)建并切換分支:

git branche dev    #創(chuàng)建dev分支
git checkout dev   #切換分支到dev
===============
git checkout -b dev    # 創(chuàng)建并切換分支, 一條命令搞定

查看分支:

git branch

對(duì)分支修改后git add , git commit 之后就可切換到master分支上合并。

合并dev到master分支:

git merge dev

刪除分支:

git branch -d dev

當(dāng)在兩個(gè)分支上同時(shí)修改了文件并且提交后,在git合并時(shí)就會(huì)出現(xiàn)沖突的報(bào)錯(cuò)。這是因?yàn)楫?dāng)我們在合并的時(shí)候程序也不知道我們到底需要更新哪一個(gè),這就需要我們手動(dòng)去更新文件,解決沖突。然后再合并。

[root@work gitrepo]# git merge test # 在master和test分支上都commit后,再merge會(huì)報(bào)錯(cuò)
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
[root@work gitrepo]# cat readme.txt 
test
<<<<<<< HEAD              #查看編輯的文件,系統(tǒng)對(duì)我們手動(dòng)要修改的地方做了標(biāo)注
this is master 
=======
this is test 
>>>>>>> test

修改文件統(tǒng)一后,再次執(zhí)行g(shù)it add 和git commit就可以解決沖突了:

[root@work gitrepo]# git log --graph  --pretty=oneline --abbrev-commit
*   52ed4df confilt
|\  
| * 310f7e7 IT        # 顯示了一個(gè)分支的修改
* | a8fa78b master
|/  
* b040742 test

用git log --graph命令可以看到分支合并圖。

合并分支時(shí),默認(rèn)使用的是fast forward模式,但是使用這種模式是不記錄其它分支的修改日志的,在實(shí)際應(yīng)用中,為了更加清楚分支上的修改時(shí)間,需要加上--no-ff參數(shù),可以用普通模式合并,合并后的歷史有分支,能看出來曾經(jīng)做過合并,而fast forward合并就看不出來曾經(jīng)做過合并。

在一般的開發(fā)過程中大豆會(huì)有如下流程:

1、leader在遠(yuǎn)程倉庫創(chuàng)建2個(gè)分支:master和dev

2、張三和李四克隆遠(yuǎn)程倉庫到本地

3、張三和李四都切換到dev分支

4、張三創(chuàng)建分支z3,李四創(chuàng)建分支l4

5、開發(fā)完成后,張三合并z3到dev,李四合并l4到dev

6、張三和李四把本地庫的dev分支推送到遠(yuǎn)程dev

7、leader拉取遠(yuǎn)程庫里的dev和masterd,在本地將dev合并到master 并推送到遠(yuǎn)程master

BUG分支管理

如果當(dāng)前正在dev分支上開發(fā),突然線上出了一個(gè)BUG,需要立即修復(fù),這時(shí)候我需要暫時(shí)隱藏當(dāng)前的工作,也就是保存當(dāng)前dev分支上的進(jìn)度 git statsh:

[root@work gitrepo]# git stash
Saved working directory and index state WIP on dev: f241242 test
HEAD is now at f241242 test

[root@work gitrepo]# git status  #隱藏工作區(qū)之后,顯示的工作目錄為空了
# On branch dev
nothing to commit, working directory clean

保存了當(dāng)前的工作后,我們就要去修復(fù)線上的bug了,切換到master分支,并創(chuàng)建一個(gè)修復(fù)的issue分支:

[root@work gitrepo]# git checkout master
Switched to branch 'master'
[root@work gitrepo]# git checkout -b issue
Switched to a new branch 'issue'
[root@work gitrepo]# git branch
  dev
* issue
  master

在issue上完成修復(fù)工作后,執(zhí)行g(shù)it add ,git commit 提交代碼,然后在master上合并issue分支上的代碼刪除issue分支:

[root@work gitrepo]# git branch
  dev
* issue
  master
[root@work gitrepo]# git add readme.txt 
[root@work gitrepo]# git commit -m "fix issue"
[issue 8b29da7] fix issue
 1 file changed, 1 insertion(+)
 
[root@work gitrepo]# git checkout master # 回到master上合并issue分支
Switched to branch 'master'
[root@work gitrepo]# git merge --no-ff -m "fix bug issue" issue # 記錄分支日志信息
Merge made by the 'recursive' strategy.
 readme.txt | 1 +
 1 file changed, 1 insertion(+)
 
[root@work gitrepo]# git branch -d issue  #刪除issue分支
Deleted branch issue (was 8b29da7).

bug修復(fù)完成之后,我們要回到自己的dev分支繼續(xù)我們的工作了:

[root@work gitrepo]# git checkout dev
Switched to branch 'dev'

[root@work gitrepo]# git status # 原來的dev分支是空的
# On branch dev
nothing to commit, working directory clean

[root@work gitrepo]# git stash list # 查看我們隱藏的分支
stash@{0}: WIP on dev: f241242 test

[root@work gitrepo]# git stash pop # 顯示出隱藏的分支,并將隱藏的分支刪除
# On branch dev
# 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:   file.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (5a7f46b8a24f1a557a37b0378ee75c65387e024a)

[root@work gitrepo]# git status
# On branch dev
# 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:   file.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

恢復(fù)隱藏的分支有兩種方法:

git stash apply  # 恢復(fù)隱藏的分支

git stash drop   # 刪除隱藏的分支記錄

============================

git statsh pop  # 恢復(fù)隱藏的分支,并將隱藏的分支刪除,用一條命令做上面兩條命令的事情。

tips:

如果要開發(fā)一個(gè)新的特性,最好新建一個(gè)分支,如果開發(fā)的新分支完成一半需要?jiǎng)h除(此時(shí)還沒有提交)刪除一個(gè)未提交的分支,可以使用git branch -D BranchName 強(qiáng)制刪除。

多人協(xié)作分支管理

在進(jìn)行多人協(xié)作開發(fā)的團(tuán)隊(duì)中,由于每個(gè)人都會(huì)去不斷的修改文件,合并文件,就會(huì)出現(xiàn)當(dāng)你想遠(yuǎn)程提交自己的代碼時(shí),碰巧別人也修改了相同的文件,這樣你本地的文件和遠(yuǎn)程的文件內(nèi)容就不一樣了,需要手動(dòng)解決沖突再進(jìn)行提交。

[root@work gitrepo]# git remote  # 查看遠(yuǎn)程分支
origin
[root@work gitrepo]# git remote -v # 查看遠(yuǎn)程分支詳細(xì)信息
origin  git@github.com:AndySkyL/gitrepo.git (fetch)
origin  git@github.com:AndySkyL/gitrepo.git (push)

提交是出現(xiàn)沖突:

[root@work gitrepo]# git add readme.txt 
[root@work gitrepo]# git commit -m "dev2"
[dev 41ad4f8] dev2
 1 file changed, 1 insertion(+), 3 deletions(-)
 
[root@work gitrepo]# git push origin dev   # 推送dev分支沖突

▽! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:AndySkyL/gitrepo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

[root@work gitrepo]# git pull   # 根據(jù)提示使用git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:AndySkyL/gitrepo
 * [new branch]      dev        -> origin/dev
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
    git pull <remote> <branch>     
If you wish to set tracking information for this branch you can do so with:
    git branch --set-upstream-to=origin/<branch> dev   # 這里已經(jīng)給出提示
    
[root@work gitrepo]# git branch --set-upstream-to=origin/dev dev 
Branch dev set up to track remote branch dev from origin.

[root@work gitrepo]# git pull  #執(zhí)行此命令之后再按照之前的方式修改文件,解決沖突
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

解決沖突后提交:

[root@work gitrepo]# git add readme.txt 
[root@work gitrepo]# git commit -m "fix m"
[dev 3267ad5] fix m

[root@work gitrepo]# git push origin dev
Counting objects: 10, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 570 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
   17a9b60..3267ad5  dev -> dev

從本地推送分支,使用git push origin branch-name,如果推送失敗,先用git pull抓取遠(yuǎn)程的新提交;

在本地創(chuàng)建和遠(yuǎn)程分支對(duì)應(yīng)的分支,使用

git checkout -b branch-name origin/branch-name 
# 本地和遠(yuǎn)程分支的名稱最好一致;

建立本地分支和遠(yuǎn)程分支的關(guān)聯(lián),使用:

git branch --set-upstream-to=origin/dev dev

標(biāo)簽管理

  • 標(biāo)簽可以是每一次commit 的標(biāo)識(shí),類似于給每次commit添加一個(gè)別名:

[root@work gitrepo]# git branch
  dev
* master
[root@work gitrepo]# git tag v1.0
[root@work gitrepo]# git tag
v1.0

默認(rèn)標(biāo)簽是打在最新提交的commit上的。

  • 如果要對(duì)之前的某一次commit打標(biāo)簽后面直接接上commit ID即可:

[root@work gitrepo]# git log --pretty=oneline --abbrev-commit
e2ce160 fix bug issue
8b29da7 fix issue
881136a fix bug example
[root@work gitrepo]# git tag v0.9 881136a  #根據(jù)log 對(duì)對(duì)應(yīng)的commit打標(biāo)簽,可以使用-m
[root@work gitrepo]# git tag               #添加說明
v0.9                                       # git標(biāo)簽的排列默認(rèn)是以標(biāo)簽名的字母順序排列的
v1.0
  • 使用tag查看具體信息:

[root@work gitrepo]# git show v1.0
commit e2ce160ad30d5433c033d9be7dc5dfe23ddbfd6d
Merge: 881136a 8b29da7
Author: trying <trying@example.com>
Date:   Wed Dec 14 16:16:32 2016 +0800
    fix bug issue
  • 也可以在新建tag時(shí)添加說明:

 git tag -a v2.0 -m "version 2.0 released"   # -a 指定tag名稱 -m 添加說明
  • 刪除標(biāo)簽

# git tag -d v2.0
Deleted tag 'v2.0' (was 959f8b1)
  • 推送標(biāo)簽到遠(yuǎn)程

    由于在本地添加的標(biāo)簽不會(huì)自動(dòng)推送到遠(yuǎn)程,如果需要推送本地的標(biāo)簽到遠(yuǎn)程,使用git push origin tagname:

# git push origin v1.0    # 推送指定的tag
# git push origin --tags  # 一次推送所有的tag
  • 刪除遠(yuǎn)程標(biāo)簽

刪除遠(yuǎn)程標(biāo)簽需要先刪除本地標(biāo)簽:

# git tag
v0.9
v1.0

# git tag -d v0.9    # 刪除本地tag
Deleted tag 'v0.9' (was 881136a)

# git push origin :refs/tags/v0.9   # 刪除遠(yuǎn)程標(biāo)簽
 - [deleted]         v0.9

git push origin :refs/tags/tagname

GIT高亮顯示字體顏色:

# git config --global color.ui true

搭建GIT服務(wù)器

如果自己不想使用github的付費(fèi)倉庫,可以自己搭建一個(gè)私有的git 服務(wù)器供企業(yè)內(nèi)部使用。

安裝git:

yum install git -y

創(chuàng)建一個(gè)git用戶:

useradd git

禁止git 用戶登錄shell,編輯/etc/passwd文件,將git用戶默認(rèn)的shell改為:

git:x:823:823::/home/git:/usr/bin/git-shell

創(chuàng)建一個(gè)用于GIT倉庫的目錄:

mkdir /gitrepo
cd /gitrepo

初始化git倉庫:

git init --bare  test.git

在/home/git用戶的目錄下創(chuàng)建密鑰認(rèn)證文件authorized_keys ,將本地的公鑰導(dǎo)入服務(wù)端的authorized_keys 文件中:

cat id_rsa.pub  > /home/git/.ssh/authorized_keys

然回到本地,就可以獲取git服務(wù)器上的工作目錄了:

[root@work ~]# git clone git@172.16.1.10:/gitrepo/test.git
Cloning into 'test'...
warning: You appear to have cloned an empty repository.

這就可以和在github上一樣操作了。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章標(biāo)題:GIT版本管理-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article46/pcphg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站建設(shè)用戶體驗(yàn)、ChatGPT微信公眾號(hào)網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營