1. 程式人生 > >GIT分布式版本控制系統使用教程

GIT分布式版本控制系統使用教程

國內 日誌 更改 ignore img local .com 遠程服務 pos

版本控制工具大概有:

RCS單機版

CVS、SVN集中式版本控制系統

GIT分布式版本控制系統


這裏介紹GIT,它四大位置:本地代碼工作區---待提交列表staging area---本地倉庫local repo---遠程倉庫remote repo(git服務器)。從左往右是上傳代碼,從右往左是下載代碼。

技術分享圖片技術分享圖片

備註1:git比svn多了待提交列表。

備註2:最好本地一個分支、遠程一個分支,沒有必要搞多個分支,只有每個人合並的時候就要花很多時間。

備註3:幫git當作高級svn來用。

備註4:更改服務器上的commit時間點這個不要做。

備註5:github公開的是免費的,私有的是收費的。國有的有coding.net等。


一、安裝git

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


二、git基礎設置

設置用戶名(可以針對單個倉庫設置,更改.git/config文件):
[root@test ~]# git config --global user.name "gxm"

設置用戶郵箱(可以針對單個倉庫設置,更改.git/config文件):
[root@test ~]# git config --global user.email "[email protected]"

查看設置:
[root@test ~]# git config --list


三、git命令幫助

[root@test ~]# git help
[root@test ~]# git help 指定指令----比如git help add


四、初始化一個新的git倉庫

root@test ~]# mkdir demo
[root@test ~]# cd demo/
[root@test demo]# git init
Initialized empty Git repository in /root/demo/.git/
[root@test demo]# ls -ah
.  ..  .git


五、向倉庫中添加新的文件,並提交(先add增加的暫存區,然後再提交到倉庫。勤使用git status和git log)

[root@test demo]# touch readme
[root@test demo]# vi hello.py
[root@test demo]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       hello.py
#       readme
nothing added to commit but untracked files present (use "git add" to track)
[root@test demo]# git add readme
[root@test demo]# git add hello.py
[root@test demo]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   hello.py
#       new file:   readme
#
[root@test demo]# git commit -m "add readme hello"
[master (root-commit) 542d06d] add readme hello
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 hello.py
create mode 100644 readme
[root@test demo]# git status
# On branch master
nothing to commit (working directory clean)
[root@test demo]# git log
commit 542d06da8b9dd3354d6307f1d78016671df249f1  這個是校驗碼
Author: gxm <[email protected]>
Date:   Wed Jan 31 17:27:57 2018 +0800
    add readme hello

備註:可以跳過暫存階段,直接git commit -a -m "test",不建議這種方法,如果是新創建的文件,這個命令不會提交上去。


六、刪除文件

文件系統方式刪除文件

[root@test demo]# rm readme
rm:是否刪除普通空文件 "readme"?y
[root@test demo]# git rm readme
rm 'readme'
[root@test demo]# git commit -m "delete readme"
[master dbb390a] delete readme
1 files changed, 1 deletions(-)
delete mode 100644 readme
[root@test demo]# git log
commit dbb390aadb864796c827b49504969d334d7466ba
Author: gxm <[email protected]>
Date:   Wed Jan 31 17:34:38 2018 +0800
    delete readme
commit 542d06da8b9dd3354d6307f1d78016671df249f1
Author: gxm <[email protected]>
Date:   Wed Jan 31 17:27:57 2018 +0800
    add readme hello


七、重命名文件

[root@test demo]# git mv hello.py hellogxm.py
[root@test demo]# git commit -m "rename hello.py"
[root@test demo]# git log
commit b89ea41790aebc686a2a48973d02b14b989c26ea
Author: gxm <[email protected]>
Date:   Wed Jan 31 17:38:39 2018 +0800
    rename hello.py
commit dbb390aadb864796c827b49504969d334d7466ba
Author: gxm <[email protected]>
Date:   Wed Jan 31 17:34:38 2018 +0800
    delete readme
commit 542d06da8b9dd3354d6307f1d78016671df249f1
Author: gxm <[email protected]>
Date:   Wed Jan 31 17:27:57 2018 +0800
    add readme hello

實際上執行了這3條命令:

[root@test demo]# mv hello.py hellogxm.py
[root@test demo]# git rm hellogxm.py
[root@test demo]# git add hellogxm.py


八、查看歷史詳細日誌

[root@test demo]# git show dbb390aadb864796c827b49504969d334d7466ba
commit dbb390aadb864796c827b49504969d334d7466ba
Author: gxm <[email protected]>
Date:   Wed Jan 31 17:34:38 2018 +0800
    delete readme
diff --git a/readme b/readme
deleted file mode 100644
index e69de29..0000000


九、撤銷本地剛才的commit

[root@test demo]# git reset --soft HEAD~1

比較柔和的撤銷,如果不用--soft,就會直接放在本地代碼中,如果用--soft就放在提交區。


十、如果撤銷本地沒有提交的改動

比如改了一個文件後悔了,在暫停區的時候,用chechout撤銷

[root@test demo]# git checkout -- 文件


十一、忽略提交一些文件

很多文件步應該放在git版本控制裏面的,創建一個vi .gitignore文件(這個文件在git項目的根目錄)

比如忽略,比如

[root@test demo]# vi .gitignore
*yml
log/*.log


十二、本地分支的創建、刪除和切換

查看分支:git branch
創建分支:git branch 分支名
切換分支:git checkout 分支名
刪除分支:git branch -d 分支名


十三、本地分支的合並

git checkout master  先切換master,然後把下面的分支合並過來
git merge 分支名


十四、在遠程創建倉庫,往遠程上傳或者交推代碼(github方式、國內的coding.net是同樣的方式,選擇一種就可以

全世界最大的git遠程倉庫github

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片


git remote add origin [email protected]:gxm555/test_git_20170208.git

git push -u origin master origin這個相當於遠程服務器的別名,在.git/config文件中定義,其實master後面還有個:master,即把本地的master推送到遠程的master。


技術分享圖片


技術分享圖片

十五、下載和拉取代碼到本地、上傳會推代碼到遠程(多push和pull,要不多人協同開發的時候容易產生合並沖突)

1、第一次下載。

git clone github提供的ssh或http鏈接
cd 目錄
git log查看歷史提交記錄
vi .git/config更改這個文件加
[user]
   name = test
   email = [email protected]


2、更改文件並重新提交。

更改文件並提交到遠程庫
git add 文件
git commit -m "描述"
git push origin master


3、再次下載

git pull origin master


十六、合並沖突

就是2個人同事改了一個文件或相同的行,只有後者提交的時候會提示沖突。會提示merge failed失敗,提示手動操作。用git status查看是哪個文件沖突,然後修改下。

<<<<<<<和>>>>>>之前的內容需要我們手動合並的,合並後add、commint後再push。

GIT分布式版本控制系統使用教程