Genkins git遠程分支代碼拉取 (四)

1.遠程拉取gitlab 工程分支,並在本地建立分支

具體過程

  • 新建一個空文件
  • 初始化 git init
  • 自己要與origin master建立連接(下劃線遠程倉庫鏈接)
    git remote add origin http://192.168.9.10:8888/root/game-of-life.git
  • 把遠程分支拉到本地(game-of-live-first_branch爲遠程倉庫的分支名)
    git fetch origin game-of-live-first_branch
  • 在本地創建分支game-of-live-first_branch並切換到該分支
    git checkout -b game-of-live-first_branch origin/game-of-live-first_branch
  • 把game-of-live-first_branch遠程分支上的內容都拉取到本地
    git pull origin game-of-live-first_branch

Genkins git遠程分支代碼拉取 (四)

Genkins git遠程分支代碼拉取 (四)

2.修改分支的內容並上傳給遠程分支

Genkins git遠程分支代碼拉取 (四)

3.如果想在linux中拉取遠程分支代碼

[root@localhost rollBack]# git init
Initialized empty Git repository in /test/rollBack/.git/
[root@localhost rollBack]# git remote add origin [email protected]:root/game-of-life.git
[root@localhost rollBack]# git fetch origin game-of-live-first_branch
remote: Enumerating objects: 1770, done.
remote: Counting objects: 100% (1770/1770), done.
remote: Compressing objects: 100% (582/582), done.
remote: Total 1770 (delta 1112), reused 1770 (delta 1112)
Receiving objects: 100% (1770/1770), 15.00 MiB | 28.20 MiB/s, done.
Resolving deltas: 100% (1112/1112), done.
From 192.168.9.10:root/game-of-life

  • branch game-of-live-first_branch -> FETCH_HEAD
    [root@localhost rollBack]# git pull origin game-of-live-first_branch
    From 192.168.9.10:root/game-of-life
  • branch game-of-live-first_branch -> FETCH_HEAD

4.git使用

4.1.源碼安裝git
[root@localhost ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@localhost ~]# yum -y install perl-ExtUtils-MakeMaker package
[root@localhost ~]# tar xvf git-2.9.5 -C /usr/src/
[root@localhost ~]# cd /usr/src/git-2.9.5
[root@localhost git-2.9.5]# make configure
GIT_VERSION = 2.9.5
GEN configure
[root@localhost git-2.9.5]# ./configure --prefix=/usr/local/
[root@localhost git-2.9.5]# make && make install


4.2.配置git

[root@localhost git-2.9.5]# git config --global user.name "yunjisuan" #配置git使用用戶
[root@localhost git-2.9.5]# git config --global user.email "[email protected]" #配置git使用郵箱
[root@localhost git-2.9.5]# git config --global color.ui true #語法高亮
[root@localhost git-2.9.5]# git config --list 查看全局配置br/>user.name=yunjisuan
[email protected]
color.ui=true

查看生成的配置文件並添加新模塊 #在root下

[root@localhost ~] # cat .gitconfig
[user]
name = clsn
email = [email protected]
[color]
ui = true

[root@localhost ~]# cat .gitconfig
[user]
name = git
email = [email protected]
[color]
ui = true
[recevice] #添加此模塊
denyCurrentBranch = ignore

獲得配置命令的手冊 #三種方法

git help <verb>
git <verb> --help
man git-<verb>


4.3.配置ssh,增加rsa認證

修改配置文件,增加下面內容
[root@localhost ~]# vi /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes

重啓
[root@localhost ~]# systemctl restart sshd


4.4.獲取git倉庫(初始化倉庫)

[root@localhost ~]# mkdir /git_data #創建目錄
[root@localhost ~]# cd /git_data/
[root@localhost git_data]# git init #初始化目錄
Initialized empty Git repository in /git_data/.git/
[root@localhost git_data]# git status #查看工作區狀態
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)

添加並提交文件
[root@localhost git]# touch README
[root@localhost git]# echo "please read first" >> README
[root@localhost git]# git add *
[root@localhost git]# git commit -m "first commit"


5.git倉庫的管理

5.1向git內增加文件:

創建文件
[root@localhost git_data]# touch README
[root@localhost git_data]# ls
README
[root@localhost git_data]# git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
README
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost git_data]# git add ./ #添加文件跟蹤
[root@localhost git_data]# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README
[root@localhost git_data]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── objects
│ ├── e6
│ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 #查看隱藏目錄下增加了一條文件
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
10 directories, 15 files
[root@localhost git_data]# git commit -m 'first commit' #由工作區提交到本地倉庫
[master (root-commit) 621be41] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
[root@localhost git_data]# git status #查看git的狀態
On branch master
nothing to commit, working tree clean
[root@localhost git_data]# tree .git/ #提交後的git目錄狀態
.git/
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── logs
│ ├── HEAD
│ └── refs
│ └── heads
│ └── master
├── objects
│ ├── 54
│ │ └── 3b9bebdc6bd5c4b22136034a95dd097a57d3dd
│ ├── 62
│ │ └── 1be41481cd4dd65b58d1400173228125cdeb5b
│ ├── e6
│ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│ ├── info
│ └── pack
└── refs
├── heads
│ └── master
└── tags
15 directories, 21 files
總結說:
git add
添加到暫存區域
git commit 提交git倉庫 -m 後面接上註釋信息,內容關於本次提交的說明,方便自己或他人查看


5.2刪除git內文件

1) 沒有添加到暫存區的數據直接rm刪除即可。
2) 已經添加到暫存區數據:
git rm --cached database
#→將文件從git暫存區域的追蹤列表移除(並不會刪除當前工作目錄內的數據文件)
git rm -f database
#→將文件數據從git暫存區和工作目錄一起刪除


5.3改(重命名數據)

1)沒有添加到暫存區的數據直接mv/rename改名即可。
2)已經添加到暫存區數據:
git mv README NOTICE #這個改的時候工作目錄裏的文件名字也改了


5.4查 (查看歷史記錄)
• git log #→查看提交歷史記錄
• git log -2 #→查看最近幾條記錄
• git log -p -1 #→-p顯示每次提交的內容差異,例如僅查看最近一次差異
• git log --stat -2 #→--stat簡要顯示最近兩次,數據增改行數,這樣能夠看到提交中修改過的內容,對文件添加或移動的行數,並在最後列出所有增減行的概要信息
• git log --pretty=oneline #→--pretty根據不同的格式展示提交的歷史信息
• git log --pretty=fuller -2 #→以更詳細的模式輸出提交的歷史記錄
• git log --pretty=fomat:"%h %cn" #→查看當前所有提交記錄的簡短SHA-1哈希字串與提交着的姓名。
使用format參數來指定具體的輸出格式
格式 說明
%s 提交說明
%cd 提交日期
%an 作者的名字
%cn 提交者的姓名
%ce 提交者的電子郵件
%H 提交對象的完整SHA-1哈希字串
%h 提交對象的簡短SHA-1哈希字串
%T 樹對象的完整SHA-1哈希字串
%t 樹對象的簡短SHA-1哈希字串
%P 父對象的完整SHA-1哈希字串
%p 父對象的簡短SHA-1哈希字串
%ad 作者的修訂時間


5.5還原歷史數據

Git服務程序中有一個叫做HEAD的版本指針,當用戶申請還原數據時,其實就是將HEAD指針指向到某個特定的提交版本,但是因爲Git是分佈式版本控制系統,爲了避免歷史記錄衝突,故使用了SHA-1計算出十六進制的哈希字串來區分每個提交版本,另外默認的HEAD版本指針會指向到最近的一次提交版本記錄,而上一個提交版本會叫HEAD^,上上一個版本則會叫做HEAD^^,當然一般會用HEAD~5來表示往上數第五個提交版本。
git reset --hard hash :
• git reset --hard HEAD^ #→還原歷史提交版本上一次
• git reset --hard 3de15d4 #→找到歷史還原點的SHA-1值後,就可以還原(值不寫全,系統會自動匹配)


5.6還原未來數據

什麼是未來數據?就是你還原到歷史數據了,但是你後悔了,想撤銷更改,但是git log已經找不到這個版本了。
git reflog #→查看未來歷史更新點
[root@localhost git_data]# git reflog
b6a6e89 HEAD@{0}: reset: moving to HEAD^
519bf0d HEAD@{1}: commit: third commit
b6a6e89 HEAD@{2}: commit: second commit
621be41 HEAD@{3}: commit (initial): first commit
[root@localhost git_data]# git reset --hard 519b #在刪除third commit情況下返回third commit
HEAD is now at 519bf0d third commit


5.7標籤使用

前面回滾使用的是一串字符串,又長又難記。
git tag v1.0 #→當前提交內容打一個標籤(方便快速回滾),每次提交都可以打個tag。
git tag #→查看當前所有的標籤
git show v1.0 #→查看當前1.0版本的詳細信息
git tag v1.2 -m "version 1.2 release is test" #→創建帶有說明的標籤,-a指定標籤名字,-m指定說明文字
git tag -d v1.0 #→我們爲同一個提交版本設置了兩次標籤,刪除之前的v1.0
[root@localhost git_data]# git reset --hard HEAD^ #還原歷史數據
HEAD is now at b6a6e89 second commit
[root@localhost git_data]# git reset --hard v1.0 #利用標籤回滾
HEAD is now at 519bf0d third commit


5.8對比數據

git diff可以對比當前文件與倉庫已保存文件的區別,知道了對README作了什麼修改後,再把它提交到倉庫就放⼼多了。
git diff README
[root@localhost git_data]# echo 222 >> README
[root@localhost git_data]# git diff README
diff --git a/README b/README
index 2bbe845..56c43c1 100644
--- a/README
+++ b/README
@@ -1,2 +1,3 @@
111
111
+222


6..分支結構

6.1 增加分支
git branch 分支名稱


6.2分支切換
[root@localhost git_data]# git branch #查看分支
linux

  • master
    [root@localhost git_data]# git checkout linux #切換到分支 'linux'
    M README
    Switched to branch 'linux'
    [root@localhost git_data]# git branch
  • linux
    master

6.3在linux分支進行修改

例:就是切換到分支後進行普通操作

echo "clsn in linux" >> README


6.4合併代碼

合併Linux上的代碼到master上

git merge linux


6.5合併失敗解決

模擬衝突,在文件的同一行做不同修改

在master 分支進行修改
[root@gitlab git_data]# cat README
2017年11月30日
[root@gitlab git_data]# echo "clsn in master">> README
[root@gitlab git_data]# git commit -a -m "clsn 2017年11月30日 09點20分 "
[master 7ab71d4] clsn 2017年11月30日 09點20分
1 file changed, 1 insertion(+)

切換到linux分支
[root@gitlab git_data]# git checkout linux
[root@gitlab git_data]# cat README
2017年11月30日
[root@gitlab git_data]# echo "clsn in linux" >> README
[root@gitlab git_data]# git commit -a -m "2017年11月30日 03"
[linux 20f1a13] 2017年11月30日 03
1 file changed, 1 insertion(+)

回到master分區,進行合併,出現衝突
[root@gitlab git_data]# git checkout master
切換到分支 'master'
[root@gitlab git_data]# git merge linux

衝突(內容):合併衝突於 README
自動合併失敗,修正衝突然後提交修正的結果。

解決衝突
[root@gitlab git_data]# vim README
2017年11月30日
clsn in master
clsn in linux
#手工解決衝突
[root@gitlab git_data]# git commit -a -m "2017年11月30日 03"
[master b6a097f] 2017年11月30日 03


6.6刪除分支

因爲之前已經合併了linux分支,所以現在看到它在列表中。

在這個列表中分支名字前沒有 * 號的分支通常可以使用 git branch -d 刪除掉;你已經將它們的工作整合到了另一個分支,所以並不會失去任何東西。
git branch --no-merged #查看所有包含未合併工作的分支

git branch -d 分支name


7.服務器命令總結

服務器上的管理命令

   mkdir:                                  //XX (創建一個空目錄 XX指目錄名)

  pwd: // 顯示當前目錄的路徑。
  git init //把當前的目錄變成可以管理的git倉庫,生成隱藏.git文件。
  git add XX //把xx文件添加到暫存區去。
  git commit –m “XX” //提交文件 –m 後面的是註釋。
  git status //查看倉庫狀態
  git diff XX // 查看XX文件修改了那些內容
  git log //查看歷史記錄
  git reset --hard HEAD^ //或者 git reset --hard HEAD~ 回退到上一個版本(如果想回退到100個版本,使用git reset –hard HEAD~100 )
  cat XX //查看XX文件內容
  git reflog //查看歷史記錄的版本號id
  git checkout -- XX //把XX文件在工作區的修改全部撤銷。
  git rm XX //刪除XX文件
  git checkout –b dev //創建dev分支 並切換到dev分支上
  git branch //查看當前所有的分支
  git checkout master // 切換回master分支
  git merge dev //在當前的分支上合併dev分支
  git branch –d dev //刪除dev分支
  git branch name //創建分支
  git stash //把當前的工作隱藏起來 等以後恢復現場後繼續工作
  git stash list //查看所有被隱藏的文件列表
  git stash apply //恢復被隱藏的文件,但是內容不刪除
  git stash drop //刪除文件
  git stash pop //恢復文件的同時 也刪除文件
  git remote //查看遠程庫的信息
  git remote –v //查看遠程庫的詳細信息
  
git remote add origin https://github.com/-- //關聯一個遠程庫 --爲路徑
  git push –u origin master //(第一次要用-u 以後不需要)把當前master分支推送到遠程庫
  git push origin master //Git會把master分支推送到遠程庫對應的遠程分支上
  git clone https://github.com/--- // 從遠程庫中克隆 --爲路徑

   遠程倉庫相關命令
   檢出倉庫:       $ git clone 
   查看遠程倉庫:$ git remote -v
   添加遠程倉庫:$ git remote add 
   刪除遠程倉庫:$ git remote rm 
   修改遠程倉庫:$ git remote set-url --push 
   拉取遠程倉庫:$ git pull 
   推送遠程倉庫:$ git push 
  *如果想把本地的某個分支test提交到遠程倉庫,並作爲遠程倉庫的master分支,或者作爲另外一個名叫test的分支,如下:
   $git push origin test:master         // 提交本地test分支作爲遠程的master分支
   $git push origin test:test              // 提交本地test分支作爲遠程的test分支
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章