git命令
git介紹
分布式版本控制系統(tǒng)。
git版本庫操作
- 創(chuàng)建git倉庫
在C:\github\創(chuàng)建目錄helloworld,進(jìn)入該目錄C:\github\helloworld,打開git bash,執(zhí)行以下命令
$ git init
Initialized empty Git repository in C:/github/helloworld/.git/
會發(fā)現(xiàn)C:\github\helloworld目錄下多了一個(gè).git文件夾
- git倉庫添加文件
$ touch README.md
$ vim README.md
$ git add README.md
- git提交文件到倉庫
git commit -m "wrote a readme file"
- 修改文件,查看git倉庫狀態(tài)以及文件修改狀態(tài)
更改README.md文件內(nèi)容,查看git倉庫狀態(tài)
$ git status
On branch master
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: README.md
git status告訴我們將要提交的倉庫在工作區(qū)和暫存區(qū)(工作區(qū)和暫存區(qū)下面介紹)做了哪些修改
在git提交文件之前,我們忘記了文件做了什么修改,可以先查看下文件的修改情況.
$ git diff README.md
diff --git a/README.md b/README.md
index b80c463..8589d53 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,7 @@
學(xué)習(xí)git的Java 例子
+1、初始化git 倉庫
+git init
+2、添加文件到git倉庫
+git add file1 file2 ...
+3、提交修改到git倉庫
+git commit -m "提交變更到git"
- 再次提交文件
提交新增的文件和修改過的文件,都需要先使用git add指令把文件添加到git倉庫
$ git add README.md
$ git commit -m "修改README.md"
//向倉庫添加所有改動(dòng)
git add -A
//向倉庫添加所有新增和更改的文件
git add .
git add *
//添加修改和刪除,但是不包括新建文件
git add -u .
小結(jié)
- [x] git init初始化git倉庫
- [x] git add -A 添加文件(新增/變更/刪除)
- [x] git commit提交文件到倉庫
git版本控制
git版本回退
- git log查看倉庫提交歷史
$ git log
commit 4fcf873881ab2b8b5eb03cbfef1d641b632eb289
Author: jjqiubj@cn.ibm.com <jjqiubj@cn.ibm.com>
Date: Tue May 9 13:22:10 2017 +0800
test delete
commit 40033016fc902101e4ce27ee7eb19709bee3f016
Author: jjqiubj@cn.ibm.com <jjqiubj@cn.ibm.com>
Date: Tue May 9 13:16:11 2017 +0800
add test.txt
commit 8010ee0ba5f6f130bcc3dee1956b58eb1617f674
Author: jjqiubj@cn.ibm.com <jjqiubj@cn.ibm.com>
Date: Mon May 8 15:59:46 2017 +0800
修改README.md
commit bae79353290525e4fa3607e33666b5fbb09eff60
Author: jjqiubj@cn.ibm.com <jjqiubj@cn.ibm.com>
Date: Mon May 8 15:42:37 2017 +0800
wrote a readme file
git log命令顯示從最近到最遠(yuǎn)的提交日志。Git的commit id不是1,2,3……遞增的數(shù)字,而是一個(gè)SHA1計(jì)算出來的一個(gè)非常大的數(shù)字,用十六進(jìn)制表示
2.git reset版本回退
回退到README.md第一次修改提交的地方
git reset --hard bae79353290525e4fa3607e33666b5fbb09eff60
HEAD is now at bae7935 wrote a readme file
$ git log
commit bae79353290525e4fa3607e33666b5fbb09eff60
Author: jjqiubj@cn.ibm.com <jjqiubj@cn.ibm.com>
Date: Mon May 8 15:42:37 2017 +0800
wrote a readme file
回退后在使用git log查看記錄,只會看到"第一提交"到"回退版本"之間的log
3.git reflog查看命令歷史,幫助版本回退到“未來”
如果先想回退到“回退版本”到“最新提交”之間的版本,可以使用reflog獲取到所有的commit id
$ git reflog
8010ee0 HEAD@{0}: reset: moving to 8010ee0ba5f6f130bcc3dee1956b58eb1617f674
bae7935 HEAD@{1}: reset: moving to bae79353290525e4fa3607e33666b5fbb09eff60
17f7439 HEAD@{2}: commit: delete
43b1130 HEAD@{3}: commit: add
4fcf873 HEAD@{4}: commit: test delete
4003301 HEAD@{5}: commit: add test.txt
8010ee0 HEAD@{6}: commit: 修改README.md
bae7935 HEAD@{7}: commit (initial): wrote a readme file
$ git reset --hard 8010ee0
總結(jié)
- [x] git log查看提交歷史
- [x] git reset --hard commit-id 版本回退
- [x] git reflog查看git命令執(zhí)行歷史
git工作區(qū)和暫存區(qū)
- 工作區(qū)
工作區(qū)就是git項(xiàng)目的目錄,比如C:\github\helloworld -
版本庫
工作區(qū)有個(gè)目錄.git,這個(gè)不算是工作區(qū),是Git的版本庫。
版本庫中存放的有暫存區(qū)stage,git為我們創(chuàng)建的第一個(gè)分支master,以及指向master的指針HEAD
git初始化項(xiàng)目 - 暫存區(qū)和工作區(qū)
git代碼提交分為兩步:第一步,git add,這個(gè)步驟就是把文件提交到暫存區(qū);第二步,git commit,這個(gè)步驟就是把暫存區(qū)的所有內(nèi)容提交到當(dāng)前分支。
eg:
- git項(xiàng)目中修改README.md,添加一個(gè)新的文件
- 查看倉庫當(dāng)前狀態(tài)
$ git status
On branch master
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: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
- 執(zhí)行g(shù)it add
$ git add .
qiujiaojiao@ADMINIB-39ARTF6 MINGW64 /c/github/helloworld (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: LICENSE
modified: README.md
執(zhí)行完git add,變更被保存在git 暫存區(qū),這個(gè)時(shí)候git項(xiàng)目的文件結(jié)構(gòu)如下:
執(zhí)行完git commit,暫存區(qū)的內(nèi)容就被提交到當(dāng)前分支,這個(gè)時(shí)候的git項(xiàng)目的文件結(jié)構(gòu)如下:
小結(jié)
- [x] git先將代碼添加到暫存區(qū),然后才提交到當(dāng)前分支。git的有個(gè)HEAD指針指向當(dāng)前分支
git變更管理
- 撤銷修改
- 工作區(qū)修改了,沒有add到暫存區(qū):git checkout --
$ git status
On branch master
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
qiujiaojiao@ADMINIB-39ARTF6 MINGW64 /c/github/helloworld (master)
$ git checkout -- README.md
- 工作區(qū)修改了,已經(jīng)添加到暫存區(qū)
eg: 修改已經(jīng)在暫存區(qū)了
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md
撤銷暫存區(qū)變更
$ git reset HEAD README.md
Unstaged changes after reset:
M README.md
$ git status
On branch master
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
撤銷工作區(qū)修改
$ git checkout -- README.md
qiujiaojiao@ADMINIB-39ARTF6 MINGW64 /c/github/helloworld (master)
$ git status
On branch master
nothing to commit, working directory clean
2.刪除
- 工作區(qū)刪除了,希望版本庫也刪除
eg: 工作區(qū)當(dāng)前已經(jīng)處于刪除狀態(tài)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
更新刪除到版本庫
$ git rm test.txt
rm 'test.txt'
$ git commit -m "delete modify"
[master 4bc4820] delete modify
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test.txt
qiujiaojiao@ADMINIB-39ARTF6 MINGW64 /c/github/helloworld (master)
$ git status
On branch master
nothing to commit, working directory clean
- 工作區(qū)刪除了,但是不希望版本庫刪除
$ git checkout -- test.txt
qiujiaojiao@ADMINIB-39ARTF6 MINGW64 /c/github/helloworld (master)
$ git status
On branch master
nothing to commit, working directory clean
總結(jié)
- [x] 工作區(qū)刪除,希望版本庫刪除:(1)git rm ${file} (2) git commit -m
- [x] 工作區(qū)誤刪除,恢復(fù)工作區(qū): git checkout -- ${file}
git遠(yuǎn)程倉庫
可以自己搭建一個(gè)git服務(wù)器,做為git遠(yuǎn)程倉庫的服務(wù)器。也可以使用官網(wǎng)github。本文以官網(wǎng)GitHub為例
添加遠(yuǎn)程倉庫
- 配置GitHub的SSH Key
$ ssh-keygen -t rsa -C "henu_qjj@sina.cn"
#根據(jù)提示輸入回車鍵即可
- 把生成的公鑰文拷貝到GitHub上
GitHub官網(wǎng)注冊->登錄GitHub->Settings->SSH and GPG keys->New SSH Key
id_rsa.pub的內(nèi)容拷貝上去,這樣你的這臺pc就可以向GitHub推送內(nèi)容了。 - GitHub上創(chuàng)建遠(yuǎn)程倉庫
登錄GitHub->New repository,創(chuàng)建一個(gè)新的遠(yuǎn)程倉庫 - 本地倉庫跟遠(yuǎn)程倉庫關(guān)聯(lián)
根據(jù)GitHub創(chuàng)建倉庫后的提示,把本地倉庫和遠(yuǎn)程關(guān)聯(lián)起來。
$ git remote add origin https://github.com/${GitHub-username}/helloworld.git
提交本地庫內(nèi)容到遠(yuǎn)程庫
git push -u origin master
總結(jié)
- [x] 本地倉庫跟遠(yuǎn)程倉庫關(guān)聯(lián):
git remote add origin https://github.com/${GitHub-username}/helloworld.git - [x] 第一次推送本地所有內(nèi)容到遠(yuǎn)程倉庫
git push -u origin master
從遠(yuǎn)程倉庫克隆
- 在GitHub創(chuàng)建遠(yuǎn)程倉庫
- 下載遠(yuǎn)程倉庫
GitHub支持兩種方式的下載
//使用git協(xié)議,使用ssh key校驗(yàn),git push不需要輸入用戶名和密碼
$ git clone git@github.com:qiuyu2014/gitrepository.git
或者
//使用https協(xié)議,git push 代碼的時(shí)候需要輸入用戶名和密碼
$ git clone https://github.com/qiuyu2014/gitrepository.git
git 支持多種協(xié)議:地傳輸,SSH 協(xié)議,Git 協(xié)議和 HTTP 協(xié)議等,Reference Git協(xié)議
總結(jié)
- [x] git關(guān)聯(lián)遠(yuǎn)程倉庫:(1)git remote add origin https://github.com/${GitHub-username}/${git-project}.git (2)git push -u origin master
- [x] git從遠(yuǎn)程倉庫下載代碼:git clone
git分支管理
原理
master:指向master分支最新提交的指針
dev:指向dev分支的最新提交的指針
HEAD:當(dāng)前分支最新提交的指針,例如:如果當(dāng)前分支是master,則指向master指針
-
剛開始git項(xiàng)目,是一條直線,master指向最新提交HEAD指向當(dāng)前分支的最新提交
-
master分支每次提交,master和HEAD都會向前移動(dòng)
-
創(chuàng)建dev分支
- 現(xiàn)在切換到dev分支
在dev分支下,修改項(xiàng)目,提交變更
-
dev分支合并到master分支
直接修改master指針,指向dev的最新提交就可以了。
分支基本命令使用例子
- 創(chuàng)建新的分支dev
$ git branch dev
- 切換到dev分支
$ git checkout dev
Switched to branch 'dev'
- 查看當(dāng)前分支
$ git branch
* dev
master
4.修改dev分支內(nèi)容
$ 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: README.md
$ git add README.md
$ git commit -m "分支管理"
[dev b182941] 分支管理
1 file changed, 1 insertion(+)
5.切換master
$ git checkout master
發(fā)現(xiàn)master分支上,看不到dev修改提交的內(nèi)容
6.合并分支
git merge 合并指定分支到當(dāng)前分支
$ git merge dev
Updating 28e754e..b182941
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
Fast-forward信息,是指這次合并是“快進(jìn)模式”,也就是直接把master指向dev的當(dāng)前提交,所以合并速度非常快。
git merge 合并指定分支到當(dāng)前分支
7.刪除dev分支
$ git branch -d dev
Deleted branch dev (was b182941).
總結(jié)
- [x] 查看分支:git branch
- [x] 創(chuàng)建分支:git branch ${branch-name}
- [x] 切換分支:git checkout ${branch-name}
- [x] 創(chuàng)建并切換分支:git checkout -b ${branch-name}
- [x] 合并某分支到當(dāng)前分支:git merge ${branch-name}
- [x] 刪除分支:git branch -d ${branch-name}
沖突解決
- 創(chuàng)建并切換到dev分支
$ git checkout -b dev
//修改README.md,添加以下內(nèi)容
四 git沖突解決:dev分支內(nèi)容
- 提交dev修改
$ git add README.md
$ git commit -m "沖突解決:dev"
- master分支修改
$ git checkout master
//修改README.md,添加以下內(nèi)容
四 git沖突解決:master分支內(nèi)容
$ git add README.md
$ git commit -m "沖突解決:master"
當(dāng)前dev,master以及HEAD指針如下圖所示:
- 合并dev到master分支
$ git merge dev
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
5.解決沖突,重新提交
查看master分支的README.md文件,沖突內(nèi)容如下
<<<<<<< HEAD
四 git分支解決:master
=======
四 git沖突解決:dev分支內(nèi)容
>>>>>>> dev
Git用<<<<<<<,=======,>>>>>>>標(biāo)記出不同分支的內(nèi)容,我們修改如下后保存:
四 git分支解決:master
四 git沖突解決:dev分支內(nèi)容
提交master解決沖突后的修改
$ git add README.md
$ git commit -m "沖突解決"
現(xiàn)在master和dev的情況如下圖:
查看master分支合并情況
git log --graph --pretty=oneline --abbrev-commit
總結(jié)
- [x] git merge 會把要合并的分支和當(dāng)前代碼合并到一起,會列出沖突
- [x] 合并后的沖突需手動(dòng)解決,然后提交代碼
- [x] git log --graph可以查看合并圖
gitlab安裝配置
Reference:
https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/
https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/
- 安裝gitlab
本文使用的是ubuntu14.04,其他操作系統(tǒng)請參考Reference給出的鏈接。
// 首先信任 GitLab 的 GPG 公鑰:
root@kvm-009864:~# curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null
//配置軟件源
vim /etc/apt/sources.list.d/gitlab-ce.list
deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu trusty main
//安裝gitlab-ce
sudo apt-get update
sudo apt-get install gitlab-ce
- 啟動(dòng)gitlab
//配置并啟動(dòng)
sudo gitlab-ctl reconfigure
在瀏覽器輸入http://${ip},第一次登陸會讓更改密碼,用戶名是root。
gitlab的配置文件/etc/gitlab/gitlab.rb,如果要修改gitlab的配置,修改該文件相應(yīng)的配置即可,然后執(zhí)行sudo gitlab-ctl reconfigure 就可以重新加載配置,并啟動(dòng)gitlab