代碼管理平臺SVN(資源)

你要了解的常識php

版本控制,記錄若干文件內容變化,以便未來查閱特定版本修訂狀況
版本管理工具發展簡史,cvs svn  git 參考http://luckypoem14.github.io/test/2012/04/24/scm-history/
svn全稱subversion,是一個開源版本控制系統,始於2000年
git是linux創始人linus發起的,2005年發佈,最初目的是更好管理linux內核代碼
git和svn不一樣在於git不須要依賴服務端就能夠工做,即git是分佈式的
關於git和svn的比較你們參考http://blog.lishiming.net/?p=305
github是基於git的在線web頁面代碼託管平臺,能夠選擇付費服務
gitlab能夠認爲是一個開源的github,二者沒有直接關係html

SVN 簡介

Subversion(SVN) 是一個開源的版本控制系統, 也就是說 Subversion 管理着隨時間改變的數據。 這些數據放置在一箇中央資料檔案庫(repository) 中。 這個檔案庫很像一個普通的文件服務器, 不過它會記住每一次文件的變更。 這樣你就能夠把檔案恢復到舊的版本, 或是瀏覽文件的變更歷史。mysql

SVN 的一些概念

  • repository(源代碼庫):源代碼統一存放的地方
  • Checkout(提取):當你手上沒有源代碼的時候,你須要從repository checkout一份
  • Commit(提交):當你已經修改了代碼,你就須要Commit到repository
  • Update (更新):當你已經Checkout了一份源代碼, Update一下你就能夠和Repository上的源代碼同步,你手上的代碼就會有最新的變動

更多介紹:http://www.runoob.com/svn/svn-intro.htmllinux

安裝svn

yum install -y subversion
建立版本庫 
mkdir -p /data/svnroot/myproject
svnadmin create /data/svnroot/myproject
cd !$/conf #authz爲權限配置文件,passwd爲密碼文件
vim authz//配置文件改成以下
[groups]
admins = aming,user1
[/]
@admins = rw
*= r
[myproject:/]
user1 = rwnginx

vim passwd//加入以下內容
[users]
aming = aming!(*$123
user1 = user1
^^^123
user2 = user2-***123
vim svnserver.conf          //更改或增長以下內容
[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz
realm = /data/svnroot/myproject
svnserve -d -r /data/svnroot          //這樣就啓動了git

svnserv -d -r  後面指定的路徑,就是你svn的路徑。 舉個例子,-r 後面跟的路徑是/data/svnroot/,而/data/svnroot/下面有myproject目錄github

那你遠程訪問myproject時,應該是svn://ip/myprojectweb

實例:redis

下載sql

[root@localhsot ~]# yum install -y subversion

建立版本庫 

[root@localhsot ~]# mkdir -p /data/svnroot/myproject         ## 建立一個目錄來存放工程的版本庫       

[root@localhsot ~]#  svnadmin create /data/svnroot/myproject               ## 建立工程的版本庫,初始化

[root@localhsot ~]#   cd !$/conf               #authz爲權限配置文件,passwd爲密碼文件,svnserve.conf爲倉庫配置文件

 

[root@localhsot ~]#  vim authz             //配置文件改成以下

[groups]
admins = aming,user1                      # 定義用戶,能夠定義多個 
[/]                                           #指的是/data/svnroot/myproject目錄
@admins = rw
*= r                                            #*表示全部,除了aming,其餘的用戶都是隻讀
[myproject:/]                                 #指的是項目的名字,user1的項目是rw
user1 = rw

[root@localhsot ~]# vim passwd

[users]
aming = aming!(*$123
user1 = user1
^^^123
user2 = user2-***123

[root@localhsot ~]# vim svnserver.conf

[general]
anon-access = none                         #匿名用戶沒有權限
auth-access = write                           #被受權的用戶
password-db = passwd                     #用戶的密碼存放的位置passwd文件
authz-db = authz                       #須要指定文件
realm = /data/svnroot/myproject              #被哪個項目進行徵調

[root@localhsot ~]# svnserve -d -r /data/svnroot     //這樣就啓動了

[root@localhsot ~]# ps aux | grep svn          #查看進程,會出現/data/svnroot 

[root@localhsot ~]#  netstat -lntp              #查看 端口

當客戶端訪問服務端時,要看看是否有防火牆規則

[root@localhsot ~]#  iptables -nVL

 

客戶端上使用svn(linux)

yum install -y subversion
svn checkout svn://192.168.133.130/myproject --username=aming
cd myproject ; ls -la
cp /etc/fstab .
svn add .       //添加到版本控制中心
svn commit -m 「add file」             //把文件上傳到服務器
svn delete filename          //在本地刪除
svn commit -m 「delete filename」          //在服務器上刪除
svn update            //把當前目錄下的文件都更新到最新版
svn log           //查看變動日誌

實例:

[root@localhsot ~]# cd /home/

[root@localhsot home]#  mkdir svntest

[root@localhsot home]# svnme=aming checkout svn://192.168.226.130/myproject --username=root

[root@localhsot home]# cd myproject/

 

[root@localhsot myproject]#  cp /etc/fstab .

[root@localhsot myproject]#  ls

[root@localhsot myproject]# svnme=aming checkout svn://192.168.226.130/myproject --username=root

[root@localhsot myproject]# ls

myproject

[root@localhsot myproject]# ls -la myproject/

 

[root@localhsot myproject]# svn add ./fstab

A               fstab

上傳到服務端

[root@localhsot myproject]# svn commit -m "add fstab"

正在增長             fstab

傳輸文件數據

提交後的版本爲1。

 

切換到服務端(01)

[root@localhsot myproject]# cd  myproject/

[root@localhsot myproject]#  ls   

與客戶端同步

[root@localhsot myproject]#  svn up                         #先升級

正在升級 ‘.’:

 A   fstab

更新到版本 1。

fstab

[root@localhsot myproject]#  ls                         #查看是否增長了fstab文件

fstab    

[root@localhsot myproject ] #    cd

[root@localhsot ~]# vim /root/.subversion/servers             #查看

 

[root@localhsot ~]#  cd /root/.subversion/

[root@localhsot .subversion]#     ls

auth  config    README.txt servers

[root@localhsot .subversion]#  cd auth/

[root@localhsot auth]# ls

 

svn.simple  svn.ssl.client-passphrase  svn.ssl.server  svnusername

[root@localhsot auth]# cd svn.simple/

[root@localhsot svn.simple]#  ls

nw3kjkq3hjkhwj4khr5jkw

[root@localhsot svn.simple]]#  cat nw3kjkq3hjkhwj4khr5jkw             #查看

讓用戶不記錄系統裏面,刪除他就好了

[root@localhsot svn.simple]#  rm -f  nw3kjkq3hjkhwj4khr5jkw     

[root@localhsot svn.simple]# cd /home/svntest/ 

[root@localhsot svntest]#  ls

myproject

 

在客戶端(02)上面操做

[root@localhsot myproject]#  vi fstab             #添加一條內容

在UUID下面添加內容

kjasdhklhjsa

[root@localhsot myproject]#  svn add ./fstab              

[root@localhsot myproject]#  svn commit -m "ch fstab"      #推送到服務端

正在發送      fstab

傳輸文件數據。

提交後的版本爲2。

[root@localhsot myproject]# 

 

在服務端端(01)上面操做

[root@localhsot svntest]# cd myproject/

[root@localhsot myproject]# svn up

[root@localhsot myproject] cat fstab                 #查看fstab生成的文件

 

•在本地刪除

[root@localhsot myproject]# svn delete fstab               #刪除不完全

D           fstab

[root@localhsot myproject]#  svn commit -m "delete fstab"           #完全刪除

正在刪除                 fstab

 

提交後的 版本爲3.

[root@localhsot myproject]#  svn  up 

正在升級 ‘.’:

 D   fstab

更新到版本 3。

[root@localhsot myproject]#  svn log              #查看變動歷史

 

客戶端上使用svn(windows)

官網 https://tortoisesvn.net/index.zh.html
下載TortoiseSVN 並安裝
簡明教程 http://www.jianshu.com/p/6b3b7b915332

實例:

下載小烏龜

在桌面上面右擊,查看安裝

建立一個myproject,而後右擊選着SVN Checkou,     點擊OK,

建立一個文檔,並寫入數據,添加到服務端。選中文檔右擊,找到ADD

在客戶端(01)上面,執行,

[root@localhsot myproject]#  svn up

正在升級 ‘.’:

A    123.txt

更新到新頒佈 7。

[root@localhsot myproject]#  ls

123.txt

[root@localhsot myproject]#  cat 123.txt

aas
a
sd
ad

 

[root@localhsot myproject]#  vim 111.txt

oisddls

sd

sd

asd

[root@localhsot myproject]#  svn add 111.txt

[root@localhsot myproject]#  svn commit -m "add 111.txt"

正在增長        111.txt

傳輸文件數據。

提交後的版本爲8。

 

在windows上面更新

 

單機上使用git

• yum install -y git

• mkdir /data/gitroot

• cd /data/gitroot

• git init //初始化倉庫

• echo -e  「123\naaa\n456\nbbb」 > 1.txt //建立一個新文件

• git add 1.txt//把1.txt添加到倉庫

• git commit -m 「add new file 1.txt」   //add完了必需要commit纔算真正把文件提交到git倉庫裏

• 再次更改1.txt

• git status  //查看當前倉庫中的狀態,好比是否有改動的文件

• git diff 1.txt  //能夠對比1.txt本次修改了什麼內容,相比較倉庫裏面的版本

實例:

下載

[root@localhost ~]# yum install -y git

[root@localhost ~]# mkdir /data/gitroot                           #建立gitroot   目錄

[root@localhost ~]# cd /data/gitroot                                #切換到gitroot   目錄

[root@localhost gitroot]# ls

[root@localhost gitroot]#git init                                       # 初始化倉庫

[root@localhostgitroot]#ls -la

總用量 0
drwxr-xr-x. 3 root root  18 6月  30 16:13 .
drwxr-xr-x. 4 root root  36 6月  30 16:12 ..
drwxr-xr-x. 7 root root 119 6月  30 16:13 .git

 

[root@localhost gitroot]# ls .git/

branches  config  description  HEAD  hooks  info  objects  refs

[root@localhost gitroot]# vim 1.txt

sad

asd

asd

[root@localhost gitroot]#  git add 1.txt                                  #把文件添加到倉庫裏面

[root@localhost gitroot]# git commit -m "add 1.txt"
[maste  (根提交) c123] add 1.txt

1   file changed,  4  insertions(+)

create mode 10064 1.txt

[root@localhost gitroot]# vim 1.txt

sad

asd

asd

as

[root@localhost gitroot]# git add 1.txt

[root@localhost gitroot]# git commit -m 「add  1.txt agin」

 

 

[root@localhost gitroot]# git status

#   位於分支  master

無文件要提交,乾淨的工做區

[root@localhost gitroot]# git diff 1.txt                          #相比較倉庫裏面的版本

diff   --git a/1.txt b/1.txt

idnex  908809..789789 100644

--- a/1.txt

+++ b/1.txt

@@ -4,3 +4,4  @@ asdjk

sad

asd

asd

+as

 

版本回退
多更改幾回1.txt,而後add,commit
git log//查看全部提交記錄
git log --pretty=oneline//一行顯示
git reset --hard f7c8e9//回退版本,其中後面跟的字符串是簡寫
撤銷修改
rm -f 1.txt//不當心刪除了1.txt
git checkout -- 1.txt//恢復1.txt
若是1.txt文件修改,add後但沒有commit,再想回退到上一次提交的狀態,可使用git reset HEAD 1.txt,再執行git checkout -- 1.txt
git reflog //查看全部歷史版本

實例(機器同上):

[root@localhost gitroot]# git add 1.txt 

[root@localhost gitroot]# git commit -m "add 1.txt agin"

[master f83b2hj3j] add 1.txt agin

   1 file   changed,   1  insertion(+)

[root@localhost gitroot]# vim 1.txt

sad

asd

as

[root@localhost gitroot]# git add 1.txt

[root@localhost gitroot]# git commit -m "ch 1.txt agin"

[master f83b2hj3j] ch 1.txt agin

   1 file   changed,   1  deletion(-)

[root@localhost gitroot]#   git log     #查看記錄

[root@localhost gitroot]# ls

1.txt

[root@localhost gitroot]# cd .git/

[root@localhost .git]# ls

branches   COMMIT_EDITMSG   config description HEAD   hooks info    logs    objecks  refs

[root@localhost .git]# cat config

[core]

           repositoryformatversion = 0

           filemode  = true

           bare  =  false

           loggallrefupdates = true

[root@localhost .git]# cat /root/.

 

[root@localhost .git]#  cat /root/.gitconfig

[user]

        email = lisi@lishiming.net

        name = lisi

[push]

       default = matching

[root@localhost .git]# git log

[root@localhost .git]# git log --pretty=oneline                           #讓git log看起來更清楚

aw324jkh234kh23k4h3223k4bhkj2bj2k33    ch    1.txt agin

s879df7dfsjadfjkhsdjkfhkjshdfkjhbsbdjkfj     add 1.txt agin

87usd832jk4bjk23bh4jk23hn45jk2hn3jk       add 1.txt agin

c55342jhik23hj4ihj234hjjk23h4kj23h4kj        add 1.txt

 

版本回退

[root@localhost .git]# cd ..

[root@localhost gitroot]# ls

1.txt

[root@localhost gitroot]# git reset --hard 12ku3hjk12h3kh

HEAD   如今位於  f33uh add 1.txt agin

[root@localhost gitroot]# git log --pretty=oneline   

s879df7dfsjadfjkhsdjkfhkjshdfkjhbsbdjkfj     add 1.txt agin

87usd832jk4bjk23bh4jk23hn45jk2hn3jk       add 1.txt agin

c55342jhik23hj4ihj234hjjk23h4kj23h4kj        add 1.txt

 

恢復s879df7dfsjad

[root@localhost gitroot# git reset --hard s879df7dfsjad

[root@localhost gitroot]# git log --pretty=oneline                     #發現只有兩條日誌了

87usd832jk4bjk23bh4jk23hn45jk2hn3jk       add 1.txt agin

c55342jhik23hj4ihj234hjjk23h4kj23h4kj        add 1.txt

 

[root@localhost gitroot]#  git reflog                   #查看之前恢復的版本

 

恢復刪除的文件

[root@localhost gitroot]# rm -f 1.txt

[root@localhost gitroot]# ls

[root@localhost gitroot]# git checkout -- 1.txt 

[root@localhost gitroot]# ls

1.txt

若是1.txt文件修改,add後但沒有commit,再想回退到上一次提交的狀態,可使用git reset HEAD 1.txt,再執行git checkout -- 1.txt

[root@localhost gitroot]# vim 1.txt                    #修改文檔

asd

asd

sad

a

[root@localhost gitroot]# git add 1.txt

[root@localhost gitroot]# git reset HEAD 1.txt                    #去掉add的標記

重置後撤出暫存區的變動:

M        1.txt

[root@localhost gitroot]# git checkout -- 1.txt    #撤銷上一步

[root@localhost gitroot]# cat 1.txt

asd

asd

sad

a

 

刪除文件 
echo -e "11111111111\n2222222222" > 2.txt 
git rm 2.txt 
git commit -m "rm 2.txt"

實例:

[root@localhost gitroot]#  git rm 1.txt

rm '1.txt'

[root@localhost gitroot]# git commit -m "rm 2.txt"                      #完全刪除了

[master cab660a] delete 1.txt

1 file changed,6  deletions(.)

delete mode 100644 1.txt

[root@localhost gitroot]# ls

恢復

[root@localhost gitroot]# git log --pretty=online

[root@localhost gitroot]# git reset --hard a66026e30

[root@localhost gitroot]# ls                   #恢復成功了

1.txt

[root@localhost gitroot]# cat 1.txt

 

創建遠程倉庫

首先到 https://github.com 註冊一個帳號,建立本身的git,點repositories 再點new
名字自定義,好比叫studygit 選擇public 點 create repository
添加key:右上角點本身頭像,選擇settings,左側選擇SSH and GPG keys
左側點New SSH key,把linux機器上的~/.ssh/id_rsa.pub內容粘貼到這裏
把本地倉庫推送到遠程倉庫 git remote add origin git@github.com:aminglinux/studygit.git //這一步是在遠程建立一個新的倉庫studygit,名字儘可能和本地的一致
git push -u origin master //而後把本地的studygit倉庫推送到遠程的studygit
下一次再推送,就能夠直接 git push

實例:

註冊

建立一個項目

建立完成

 

添加祕鑰,驗證

在機器(01)上面操做

[root@localhost ~]# ssh-keygen                             #查看祕鑰

[root@localhost ~]# cat .ssh/id_rsa.pub                 #查看生產的祕鑰

 

在客戶端(01機器)上面建立倉庫,寫好東西以後,發送到遠程端上。

連接地址:https://github.com/wzqlinux/localhost

在客戶端(01機器)上

【root@localhost ~】# cd /tmp/

【root@localhost tmp】# mkdir localhost                      #若是已經存在了,刪除執行下一步

【root@localhost tmp】# rm -rf localhost/              #再次 mkdir localhost建立        

【root@localhost tmp】# cd localhost/

 

建立一個新的存儲庫。

【root@localhost localhost】# echo "# localhost" >> README.md
【root@localhost localhost】# git init

初始化空的Git版本庫於/tmp/localhost/.git/

【root@localhost localhost】# ls -la
總用量 8
drwx-xr-x   3  root  root    35   10月 18 08:80 .
drwxrwxret. 7  root  root    7868 10月 18 08:80 ..
drwxrwxret. 7  root  root    7868 10月 18 08:80 .git
【root@localhost localhost】# git add README.md
【root@localhost localhost】# git commit -m "first commit"
【root@localhost localhost】# git remote add origin https://github.com/wzqlinux/localhost.git                 #執行這一條,下面沒有顯示,說明執行錯成功了
【root@localhost localhost】# git push -u origin master

緣由:是以前已經建立了一個,從新建立一個就好了

【root@localhost localhost】# ls

README.md

【root@localhost localhost】# vim 2.txt              #編譯一個新的文件

asdfasasd

asdsd

【root@localhost localhost】# git add 2.txt

【root@localhost localhost】#git commit -m "add 2.txt"

[master 32hjb1hj2] add 2.txt

   1 file  changed ,  3 insertions(+)

   create mode 100644 2.txt

【root@localhost localhost】#git push                       #推到遠程上面

【root@localhost localhost】# git remote add origin https://github.com/wzqlinux/localhost.git

而後刷新gitHUB  ,下面中間顯示的註釋

 

 

 

克隆遠程倉庫(在工做中經常使用)

cd /home
git clone git@github.com:aminglinux/lanmp.git
它提示,會在當前目錄下初始化一個倉庫,並建立一個.git的目錄,以下
Initialized empty Git repository in /home/lanmp/.
git/完成後,ls能夠看到一個lanmp的目錄
cd lanmp
vi lanmp.sh 編輯一下文件,而後提交
git add lanmp.sh
git commit -m "sdlfasdf" 
而後再推送到遠程服務端
git push

實例:

在GitHub上面,找一個項目,複製要克隆的連接

 

在客戶端(01機器)上

【root@localhost localhost】# cd /home
[root@localroot home]# ls
【root@localhost home】# git clone git@github.com:aminglinux/lanmp.git    #沒有上傳公鑰,也能夠執行這一步
【root@localhost home】# cd lamp/
【root@localhost lamp】# ls
lamp.sh      README.md
【root@localhost lamp】# ls -l
總用量 20
-rw-r--r--  1  root root 17861 6月 12:23 lamp.sh
-rw-r--r-- 1   root root 17861 6月 12:23 README.md
【root@localhost lamp】# vim README.md
#lamp
lamp/lnmp  一鍵安裝腳本
author:aming
version :  0.2
sdssdsdf
asdf

把更改的文件上傳到服務端

【root@localhost lamp】# git add README.md

【root@localhost lamp】# git commit -m "change readme.md"

【root@localhost lamp】#git push                   #推到服務端

回到GitHub上面刷新

選中lamp.sh,而後修改


 

在客戶端(01)上面更新

【root@localhost lamp】# git pull                   #把上面更改的內容下拉下來
【root@localhost lamp】# cat README.MD     #查看README.MD是否更新了

 

 

分支管理

git branch    //查看分支
git branch aming      //建立分支
git checkout aming    //切換到了aming分支下
再用git branch查看,會看到有兩個分支master和aming,當前使用的分支前面會有一個*在aming分支下 ,編輯2.txt,並提交到新分支
echo "askdfjlksadjflk" > 2.txt
git add 2.txt
git commit -m "laksjdflksjdklfj" 
切換回master分支
git checkout master //此時cat 2.txt發現並無更改內容

注意:

兩個分支的的不一樣會在

<<<<<<< HEAD

>>>>>>> aming

之間展現。

而<<<以前或>>>以後的部分是兩個分支都相同的地方

實例:

在本地的倉庫裏面查操做

在客戶端(01機器)上

【root@localhost ~】# cd /data/gitroot
[root@localroot gitroot]# git branch                    #//查看倉庫裏面分支

* master                #星號表明當前的分支,是哪個。

[root@localroot gitroot]# git branch aming      //建立分支
[root@localroot gitroot]# git branch 

aming

* master                            #* master  說明還在master分支上面     

[root@localroot gitroot]# ls                           #查看master分支下的文件

 1.txt

[root@localroot gitroot]# git checkout aming           //切換到了aming分支下

切換到分支‘aming’

[root@localroot gitroot]#  git branch                   #查看分支

* aming

master     

[root@localroot gitroot]# ls                           #查看阿明分支下的文件

1.txt

[root@localroot gitroot]# vim 2.txt       #新建立一個文件,作測試

sdf

sd

ffsd

[root@localroot gitroot]# git add .
[root@localroot gitroot]# git -m "add 2.txt"
[root@localroot gitroot]# git push                       #推到

緣由:沒有配置遠程的窗口,git push 使用前提:有一個遠程的倉庫,本地和遠程同步的時候。

[root@localroot gitroot]# ls

1.txt    2.txt

[root@localroot gitroot]# git checkout master                     #切換到master分支下
[root@localroot gitroot]# ls                             #沒有2.txt。說明分支是隔離開的

1.txt 

 

分支的合併

git checkout master //合併分支以前,先切換到目標分支 
git merge aming //把aming分支合併到了master
若是master分支和aming分支都對2.txt進行了編輯,當合並時會提示衝突,須要先解決衝突才能夠繼續合併。
解決衝突的方法是在master分支下,編輯2.txt,改成aming分支裏面2.txt的內容。 而後提交2.txt,再合併aming分支。
可是這樣有一個問題,萬一master分支更改的內容是咱們想要的呢? 能夠編輯2.txt內容,改成想要的,而後提交。切換到aming分支,而後合併master分支到aming分支便可(倒着合併)。合併分支有一個原則,那就是要把最新的分支合併到舊的分支。也就是說merge後面跟的分支名字必定是最新的分支。
git branch -d aming //刪除分支
若是分支沒有合併,刪除以前會提示,那就不合並,強制刪除
git branch -D aming


 

實例:

在客戶端(01機器)上

[root@localroot gitroot]# git branch                       #切換到master分支上去

    aming

*  master

[root@localroot gitroot]# git merge aming         //把aming分支合併到了master
[root@localroot gitroot]# ls                           #合併完成以後,下面出現2。txt

1.txt    2.txt

 

作一些變動

[root@localroot gitroot]# vim 2.txt                   #在2.txt中添加一些內容

sdjkfh

sdf

sdfsdf

[root@localroot gitroot]# git add 2.txt
[root@localroot gitroot]# git commit -m "ch 2.txt"

[master 257sd] ch 2.txt

1 fiel changed, 2 inserts(+)

[root@localroot gitroot]# git checkout aming                      #切換到amingf分支

切換到amingf分支

[root@localroot gitroot]# vim 2.xtx

j32

23kj

23j4

更改阿明下的2.txt文件

[root@localroot gitroot]# git add 2.txt
[root@localroot gitroot]# git commit -m "ch 2.txt"

[master 81sdg7] ch 2.txt

1 fiel changed, 2 deletions(-)

 

分支管理

合併aming到master分支下

[root@localroot gitroot]# git checkout master
[root@localroot gitroot]# git merge aming
[root@localroot gitroot]# cat 2.txt                  #上面是master分支下的內容,地下是aming分支下的內容。

緣由:兩個文件的修改的內容不同。把這兩個文件內容,設置成同樣的

解決辦法:

[root@localroot gitroot]# vim 2.txt                       #修改2.txt文件

[root@localroot gitroot]# git checkout aming

[root@localroot gitroot]# git merge aming

[root@localroot gitroot]# git commit -a              #查看一些提示信息
[root@localroot gitroot]# git add 2.txt
[root@localroot gitroot]# git commit -m "ch 2.txt"

再一次合併

[root@localroot gitroot]# git merge aming

Already up-to-date.

 

切換到aming分支

[root@localroot gitroot]# git checkout aming

切換到‘aming分支’

查看2.txt文件

[root@localroot gitroot]# cat 2.txt

刪除分支aming

[root@localroot gitroot]# git branch -d aming

緣由:你在aming分支裏面

切換到aming分支,而後再刪除。

[root@localroot gitroot]# git checkout master
[root@localroot gitroot]# git branch -d aming

若是上面的刪除不了,執行這一步強制刪除aming

[root@localroot gitroot]# git branch -D aming

使用分支的原則

對於分支的應用,建議你們以這樣的原則來:
master分支是很是重要的,線上發佈代碼用這個分支,平時咱們開發代碼不要在這個分支上。
建立一個dev分支,專門用做開發,只有當發佈到線上以前,纔會把dev分支合併到master
開發人員應該在dev的基礎上再分支成我的分支,我的分支(在本身pc上)裏面開發代碼,而後合併到dev分支

dev分支合併bob分支的命令是:
git checkout dev //先切換到dev分支,而後
git merge bob

master的分支不動,master合併的時候纔會動,dev的分支用作開發,在dev的基礎上再分支成我的分支,我的分支(在本身pc上)裏面開發代碼,而後合併到dev分支,而後dev合併到master  分支的合併原則

遠程分支

本地新建的分支若是不推送到遠程,對其餘人就是不可見的
查看遠程分支 git ls-remote origin,能夠看到全部分支
對於git push分支分兩種狀況
當本地分支和遠程分支一致時


git push會把全部本地分支的變動一同推送到遠程,若是想只推送一個分支,使用git push origin branch-name


當本地分支比遠程分支多,默認git push 只推送本地和遠程一致的分支,想要把多出來的本地分支推送到遠程時,使用git push origin branch-name 若是推送失敗,先用git pull抓取遠程的新提交


git clone的時候默認只把master分支克隆下來,若是想把全部分支都克隆下來,須要手動建立,在本地建立和遠程分支對應的分支,使用git checkout -b branch-name origin/branch-name,本地和遠程分支的名稱要一致

 

實例:

建立新的項目名

格式化

 

另外一種克隆方法:在命令行下格式化:把這個項目,克隆島本地上,在本地操做,同步到遠程上面

建立分支

把倉庫克隆到本地

[root@localroot gitroot]# cd /tmp
[root@localroot tmp]# git clone git@github.com:aminglinux/apelearn.git          #克隆
[root@localroot tmp]# cd apelearn/
[root@localroot apelearn]# ls

linux.doc    README.md

查看分支

[root@localroot apelearn]# git branch

* master

查看遠程全部的分支

[root@localroot apelearn]# git ls-remote origin

23jkw3bjkb23kj4b2323j4n2332            HEAD

4jk43jkb23kjb4jk23b4jk23bkl4             refs/heads/dev

jk23jk4h23kjh4nkj23n4lk23jn4             refs/heads/master

 

把全部的分支都克隆下來,手動建立,在本地建立和遠程分支對應的分支

 

[root@localroot apelearn]# git checkout -b dev origin/dev                要遠程分支的名字dev

分支dev設置爲跟蹤來自origin的遠程分支dev.

切換到一個新的分支‘dev’

[root@localroot apelearn]# git branch

* dev

   master

[root@localroot apelearn]# ls

linux.doc         README.md

 

更改dev分支下的文件

[root@localroot apelearn]#  vim 2.txt                    #新建一個文件

aa

bb

cc

[root@localroot apelearn]# git  add 2.txt
[root@localroot apelearn]# git commit -m "ch 2.txt"

[dev 56757] ch 2.txt

1 file  changed ,2 insertions(+)

    create mode 100644  2.txt

把增長的內容,推送到遠程上面

[root@localroot apelearn]# git push                          #推送到遠程上面

        8c23hh23..3b434j    dev -> dev

 

推送分支的種類

git push會把全部本地分支的變動一同推送到遠程,若是想只推送一個分支,使用git push origin branch-name

[root@localroot apelearn]# git push origin dev            #只推送一個分支dev

 

• 當本地分支比遠程分支多,默認git push 只推送本地和遠程一致的分支,想要把多出來的本地分支推送到遠程時,使用git push origin branch-name  若是推送失敗,先用git pull抓取遠程的新提交

新建一個分支dev2

[root@localroot apelearn]# git branch dev2               新建一個分支dev2
[root@localroot apelearn]# git branch                     查看分支

* dev

  dev2

  master

[root@localroot apelearn]# git checkout dev2

切換到分支'dev2'

[root@localroot apelearn]# ls

2.txt    linux.doc  README.md

[root@localroot apelearn]# vim 3.txt

aaaaaaaaaa

bbbbb

[root@localroot apelearn]# git add 3.txt
[root@localroot apelearn]# git commit -m "add 3.txt"

[dev2 c67b32] add 3.txt

   1 file changed ,1 insertion(+)

   create mode 100644 3.txt

[root@localroot apelearn]# git push                 #查看新建立的分支

Everything up-to-date                       #最新的,不須要更新

 

把新建立的dev2推送到遠程上面

[root@localroot apelearn]# git push origin dev2      把新建立的dev2推送到遠程上面

      * [new branch ]      dev2 > dev2

 

刷新頁面,查看新建立的dev2

 

標籤管理
標籤相似於快照功能,能夠給版本庫打一個標籤,記錄某個時刻庫的狀態。也能夠隨時恢復到該狀態。
git checkout master 先切到master分支上
git tag v1.0 給master打一個標籤v1.0
git show v1.0 查看標籤信息
git tag 能夠查看全部的標籤
tag是針對commit來打標籤的,因此能夠針對歷史的commit來打tag
git log --pretty=oneline --abbrev-commit //先查看歷史的commit
git tag v0.9 46d3c1a //針對歷史commit打標籤
git tag -a v0.8 -m "tag just v1.1 and so on" 5aacaf4 //能夠對標籤進行描述
git tag -d v0.8 //刪除標籤
git push origin v1.0 //推送指定標籤到遠程
git push --tag origin //推送全部標籤
若是本地刪除了一個標籤,遠程也想要刪除須要這樣操做:
git tag v1.0 -d //刪除本地標籤
git push origin :refs/tags/v1.0 //刪除遠程標籤

實例:

給master打標籤

[root@localroot apelearn]# git checkout master         先切到master分支上

切換到分支 ‘master’

[root@localroot apelearn]# git tag v1.0         給master打一個標籤v1.0
[root@localroot apelearn]#  git show v1.0      查看標籤信息
[root@localroot apelearn]# git tag      能夠查看全部的標籤

v1.0

[root@localroot apelearn]# git show v1.0       查看標籤信息

tag是針對commit來打標籤的,因此能夠針對歷史的commit來打tag

[root@localroot apelearn]# git log --pretty=oneline  查看當前的commit

[root@localroot apelearn]# git log --pretty=oneline --abbrev-commit     //先查看歷史的commit,,,,,,顯示簡寫的commit

 

針對歷史commit打標籤

[root@localroot apelearn]# git tag v0.8 3cff3f23f          //針對歷史commit打標籤
[root@localroot apelearn]# git tag

v0.8

v0.9

[root@localroot apelearn]# git tag -a v0.8 -m "first ltag" 5aacaf4 //能夠對標籤進行描述
[root@localroot apelearn]#  git tag -d v0.8       //刪除標籤

推送指定標籤到遠程

[root@localroot apelearn]#  git push origin v1.0      //推送指定標籤到遠程

[root@localroot apelearn]#  git push --tag origin     //推送全部標籤

 若是本地刪除了一個標籤,遠程也想要刪除須要這樣操做

[root@localroot apelearn]# git tag v1.0 -d    //刪除本地標籤
[root@localroot apelearn]#•git push origin :refs/tags/v1.0   //刪除遠程標籤

 

git別名
git commit 這個命令是否是有點長? 用別名能夠提升咱們的工做效率
git config --global alias.ci commit
git config --global alias.co checkout
git config --global alias.br branch
查看git別名使用命令
git config --list |grep alias
查詢log小技巧:
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
取消別名
git config --global --unset alias.br

實例:

[root@localroot gitroot]# git config --global alias.ci commit

查看git別名使用命令

[root@localroot gitroot]# git config --list |grep alias

取消別名

[root@localroot gitroot]# git config --global --unset alias.br
[root@localroot gitroot]#
[root@localroot apelearn]#
[root@localroot apelearn]#
[root@localroot apelearn]#

 

 

搭建git服務器

     git服務器平時是不動的,咱們從服務器端克隆一個遠程倉庫到客戶端,平時寫什麼代碼就在本地克隆的倉庫去寫,而後把修改的內容push到遠程的git服務器倉庫實現修改。
      github畢竟是公開的,而私有倉庫又得花錢買。因此咱們能夠想辦法搭建一個私有的,只本身公司使用的。Gitlab是個不錯的選擇。在介紹它以前,先講述一下命令行的git服務器
找一臺服務器,首先要安裝git,yum install git 
添加git用戶,而且設置shell爲/usr/bin/git-shell,目的是爲了避免讓git用戶遠程登錄
useradd -s /usr/bin/git-shell git 
cd /home/git
建立authorized_keys文件,並更改屬主、屬組和權限,用來存客戶端機器上的公鑰
mkdir .ssh
touch .ssh/authorized_keys
chown -R git.git .ssh
chmod 600 .ssh/authorized_keys

定好存儲git倉庫的目錄,好比 /data/gitroot
mkdir /data/gitroot
cd /data/gitroot
git init --bare sample.git // 會建立一個裸倉庫,裸倉庫沒有工做區,由於服務器上的Git倉庫純粹是爲了共享,因此不讓用戶直接登陸到服務器上去改工做區,而且服務器上的Git倉庫一般都以.git結尾
chown -R git.git sample.git
以上操做是在git服務器上作的,平時git服務器是不須要開發人員登陸修改代碼的,它僅僅是充當着一個服務器的角色,就像github同樣,平時操做都是在咱們本身的pc上作的
首先要把客戶端上的公鑰放到git服務器上/home/git/.ssh/authorized_keys文件裏
在客戶端上(本身pc)克隆遠程倉庫
git clone git@ip:/data/gitroot/sample.git
此時就能夠在當前目錄下生成一個sample的目錄,這個就是咱們克隆的遠程倉庫了。進入到這裏面,能夠開發一些代碼,而後push到遠程。

實例:

在一臺新的虛擬機上面操做,搭建git服務器

[root@localroot ~]# yum install git 

添加git用戶,而且設置shell爲/usr/bin/git-shell,目的是爲了避免讓git用戶遠程登錄

[root@localroot ~]# useradd -s /usr/bin/git-shell git 
[root@localroot ~]# cd /home/git

建立authorized_keys文件,並更改屬主、屬組和權限,用來存客戶端機器上的公鑰

[root@localroot git]# mkdir .ssh
[root@localroot git]# touch .ssh/authorized_keys
[root@localroot git]# chown -R git.git .ssh
[root@localroot git]# chmod 600 .ssh/authorized_keys

在客戶端虛擬機(01)

[root@localroot ]# cat .ssh/id_rsa.pub                  #查看密鑰

而後把密鑰粘貼到02虛擬機.ssh/authorized_keys中,也就是新建的虛擬機

[root@localroot git]# vi .ssh/authorized_keys

在客戶端虛擬機(01)(192.168.133.132是新建的02虛擬機上面)

[root@localroot git]# ssh git@192.168.133.132

這樣的提示說明沒問題,說明驗證超成功了。不容許登錄

在新建的虛擬機(02服務端

初始化倉庫

[root@localroot git]# cd /data/
[root@localroot data]# mkdir /data/gitroot
[root@localroot gitroot]# cd  /data/gitroot
[root@localroot gitroot]# git init --bare sample.git

建立一個裸倉庫,裸倉庫沒有工做區,由於服務器上的Git倉庫純粹是爲了共享,因此不讓用戶直接登陸到服務器上去改工做區,而且服務器上的Git倉庫一般都以.git結尾

初始化k空的Git倉庫於  /data/gitroot/sample.git/

[root@localroot gitroot]# ls

sample.git

設置一個數組

[root@localroot git]# chown -R git.git sample.git

 

在客戶端(01)上(本身pc)克隆遠程倉庫
git clone git@ip:/data/gitroot/sample.git

[root@localroot git]# git clone git@192.168.133.132:/data/gitroot/sample.git
[root@localroot git]# cd sample/
[root@localroot sample]# ls -la

總用量

drwx-xr-x     3   root root  18 6月  18 15:33 .

dr-x-xr-x--   25   root root  18 6月  18 15:33 ..

drwx-xr-x     7    root root  18 6月  18 15:33 . git

[root@localroot sample]# cp /etc/init.d/

123  function mariadb  mysqld    netconsole   network   nginx  php-fpm   README

[root@localroot sample]# cp /etc/init.d/mysqld .
[root@localroot sample]# git add .
[root@localroot sample]# git commit -m "add new file"

[master (根提交) d1ddd2hjg]  add  new  file

  1  file  changed 100755 mysqld (+)

  create mode 100755 mysqld

[root@localroot sample]# git push

緣由:沒有選擇branch

解決版本:設置一個倉庫,push的時候加上分支

[root@localroot sample]#  git push origin master

再作一個變動

[root@localroot sample]# echo "sidhfksd" > 222.txt
[root@localroot sample]# git add 222.txt
[root@localroot sample]# git commit -m "add 22.txt"

[root@localroot sample]# git push

作一個測試

[root@localroot sample]#  cd ..
[root@localroot ~]# cd /tmp/

把剛纔的測試,項目倉庫克隆下來,把代碼推送到服務端

[root@localroot tmp]# git clone git@192.168.133.132:/data/gitroot/sample.git

[root@localroot tmp]# ls sample/

222.txt   mysqld

[root@localroot tmp]# cd sample/
[root@localroot sample]# vim 222.txt                #修改 222.txt

222

33

[root@localroot sample]# git commit -m "ch 222.txt"

[root@localroot sample]# git push

[root@localroot sample]# cd /root/sample/
[root@localroot sample]# ls

222.txt    mysqld

[root@localroot sample]# git pull                             #拉取222.txt 

[root@localroot sample]# cat 222.txt 

222

33

 

 

使用gitlab

      GitLab是利用 Ruby on Rails 一個開源的版本管理系統,實現一個自託管的Git項目倉庫,可經過Web界面進行訪問公開的或者私人項目。它擁有與Github相似的功能,可以瀏覽源代碼,管理缺陷和註釋。能夠管理團隊對倉庫的訪問,它很是易於瀏覽提交過的版本並提供一個文件歷史庫。團隊成員能夠利用內置的簡單聊天程序(Wall)進行交流。它還提供一個代碼片斷收集功能能夠輕鬆實現代碼複用,便於往後有須要的時候進行查找。
gitlab官網 https://about.gitlab.com/gitlab-com/
官方安裝文檔 https://about.gitlab.com/installation/?version=ce#centos-7 (ce/ee)
要求服務器內存很多於2g
vim /etc/yum.repos.d/gitlab.repo//加入以下內容
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
yum install -y gitlab-ce
gitlab-ctl reconfigure
netstat -lnpt //查看監聽端口
gitlab-ctl stop/restart/start/status
瀏覽器訪問gitlab,輸入ip便可
默認管理員root,無密碼,它會讓咱們去定義一個密碼
gitlab經常使用命令 https://www.cnyunwei.cc/archives/1204
gitlab備份 gitlab-rake gitlab:backup:create
備份目錄在/var/opt/gitlab/backups
gitlab 恢復 先停服務 gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq
gitlab-rake gitlab:backup:restore BACKUP=xxxxx (這裏是一個編號,即備份文件的前綴)
再啓動服務 gitlab-ctl start

實例:

在虛擬機(01)上面操做,配置鏡像

[root@localroot ~]# vim /etc/yum.repos.d/gitlab.repo//加入以下內容

[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

[root@localroot ~]# yum install -y gitlab-ce
[root@localroot ~]# gitlab-ctl reconfigure              #啓動服務
[root@localroot ~]# ps aux | grep git

[root@localroot g~]# netstat -lnpt //查看監聽端口

停掉系統中的nginx服務

[root@localroot ~]# /etc/init.d/nginx stop
[root@localroot ~]# chkconfig nginx off

 

[root@localroot ~]# gitlab-ctl stop/restart/start/status   #/中止/重啓/

若是有redis.server,停掉他

[root@localroot ~]# killall redis.server

啓動服務

[root@localroot ~]#gitlab-ctl start

查看是否IPTABLES規則,若是有就開放80端口

[root@localroot ~]# iptables -nvL
[root@localroot ~]# w                     #查看負載
[root@localroot ~]# free
[root@localroot ~]# vmstat 1
[root@localroot ~]# top -c

 

瀏覽器訪問gitlab,輸入ip便可,ip是客戶端的IP
默認管理員root,無密碼,它會讓咱們去定義一個密碼

 

使用gitlab

查看提供Web服務的nginx目錄

[root@localroot ~]# ps aux | grep nginx
[root@localroot ~]# ls /var/opt/gitlab/nginx/conf/

gitlab-http.conf     nginx.conf    nginx-status.conf

[root@localroot ~]# ls /var/opt/gitlab/nginx/conf/nginx.conf      主配置文件

/var/opt/gitlab/nginx/conf/nginx.conf 

[root@localroot ~]#ls /var/opt/gitlab/nginx/conf/gitlab-http.conf        gitlab配置文件

/var/opt/gitlab/nginx/conf/gitlab-http.conf

修改監聽的端口

[root@localroot ~]# vim /var/opt/gitlab/nginx/conf/gitlab-http.conf 

 

添加公鑰

進入管理員區域

 

 

[root@localroot ~]#
[root@localroot ~]#
[root@localroot ~]#

 

 

 

常見問題:

一、

  1. 是否是每建立一個項目,都須要用svnadmin create xxx 進行初始化操做? conf目錄下的三個文件是獨立仍是通用的?

  2. svnserve.conf中的auth-access不會跟authz中的設定產生衝突麼? 例如 authz文件中我設定@admins = r,svnserve.conf中設定auth-access = write .那麼admins組的用戶到底有沒有write的權限?

答:1 每一個項目都須要create一下 authz和passwd文件能夠共用,參考擴展裏面的多倉庫統一管理

 

2 svnserver.conf裏面設定的整個項目的讀寫權限,而authz是針對我的用戶的。 若svnserver.conf中設置爲寫,可是我的用戶卻爲只讀,那最終權限是隻讀。若svnserver.conf裏面是隻讀,即便authz設置爲寫,也不能寫。

三、安裝gitlab的時候自動安裝了gitlab

 

 

擴展部分

推送到服務器上的文件  能夠在服務器硬盤上找到源文件

http://blog.csdn.net/qshn2sky/article/details/77622016 

http://blog.csdn.net/aguda_king/article/details/71609475

 

svn的鉤子  http://coolnull.com/1716.html

gitlab修改端口  http://blog.csdn.net/arybd/article/details/54635295

修改主機名 http://www.mamicode.com/info-detail-1316828.html

第三方郵件 http://blog.csdn.net/liuruiqun/article/details/50000213

server ssh 端口並非22   http://www.cnblogs.com/limx/p/5709101.html   http://www.linuxidc.com/Linux/2017-02/141043.htm 

應該修改  /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

# If you use non-standard ssh port you need to specify it

ssh_port: xxxxx

 

 

有關svn方面的信息:https://bbs.csdn.net/topics/360112901?list=lz

 

連接:

SVN 教程 :http://www.runoob.com/svn/svn-tutorial.html

相關文章
相關標籤/搜索