天天看點

GIT伺服器搭建(轉載)一、GIT伺服器的搭建On branch masterInitial commitChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: readme.txt

一、GIT伺服器的搭建

1. 安裝Git

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">yum -y install git </pre>

2. 建立git使用者

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">adduser git</pre>

3. 建立證書登陸

收集所有用戶端需要登入的使用者的公鑰,就是他們自己的

id_rsa.pub

檔案,把所有公鑰導入到

/home/git/.ssh/authorized_keys

檔案裡,一行一個。

保證ssh不輸入密碼能連接配接到git使用者

4. 初始化

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@app-01 opt]# git init --bare demo.git

Initialized empty Git repository in /opt/demo.git/</pre>

Git就會建立一個裸倉庫,裸倉庫沒有工作區,因為伺服器上的Git倉庫純粹是為了共享,是以不讓使用者直接登入到伺服器上去改工作區,并且伺服器上的Git倉庫通常都以

.git

結尾。

5. 把owner改為

git

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">chown -R git:git demo.git/</pre>

6. 禁用shell登陸

将/bin/bash改成
           

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@app-01 opt]# tail -1f /etc/passwd

git:x:1002:1006::/home/git:/usr/bin/git-shell</pre>

至此git伺服器就搭建完成了。

二、GIT的基本使用

1. 克隆遠端倉庫

在用戶端操作

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c DEV]$ git clone

ssh://[email protected]:2121/opt/demo.git

Initialized empty Git repository in /root/DEV/demo/.git/ warning: You appear to have cloned an empty repository.

[root@wls12c DEV]$ ls

demo</pre>

2. 初始化用戶端的工作環境

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ git config --global

user.name

"Scott Cho" [root@wls12c demo]$ git config --global

user.email

"

[email protected]

" [root@wls12c demo]$ git config --global core.editor vim</pre>

3. 向Git本地倉庫中送出一個新檔案

[

GIT伺服器搭建(轉載)一、GIT伺服器的搭建On branch masterInitial commitChanges to be committed:(use "git rm --cached &lt;file&gt;..." to unstage)new file: readme.txt

複制代碼

](javascript:void(0); "複制代碼")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ echo "I successfully cloned the Git repository" > readme.txt

[root@wls12c demo]$ git add readme.txt #增加資料到暫存區

[root@wls12c demo]$ git status        #檢視目前工作目錄的狀态

On branch master

Initial commit

Changes to be committed:

(use "git rm --cached <file>..." to unstage)

new file: readme.txt

[root@wls12c demo]$ git log

fatal: bad default revision 'HEAD' [root@wls12c demo]$ git commit -m "Clone the Git repository" [master (root-commit) b548d3b] Clone the Git repository 1 files changed, 1 insertions(+), 0 deletions(-)

create mode 100644 readme.txt

commit b548d3bd469cf5f183e9be9a3b2949f6361b5385

Author: Scott Cho

Date: Mon Feb 27 17:42:29 2017 +0800 Clone the Git repository</pre>

GIT伺服器搭建(轉載)一、GIT伺服器的搭建On branch masterInitial commitChanges to be committed:(use "git rm --cached &lt;file&gt;..." to unstage)new file: readme.txt

** 常見技巧:**

**  檢視目前檔案内容與Git版本資料庫中的差别:        **git diff readme.txt

**将目前工作目錄内的所有檔案都一起添加到暫存區域:   **git add .

建立忽略檔案清單:                   .gitignore

檔案被直接送出到Git資料庫:              git commit -a -m "Modified again”

4 定義遠端的Git伺服器

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ git remote add server

</pre>

5. 将檔案送出到遠端Git伺服器

GIT伺服器搭建(轉載)一、GIT伺服器的搭建On branch masterInitial commitChanges to be committed:(use "git rm --cached &lt;file&gt;..." to unstage)new file: readme.txt

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ git push -u server master

Counting objects: 3, done.

Writing objects: 100% (3/3), 262 bytes, done.

Total 3 (delta 0), reused 0 (delta 0)

To

  • [new branch] master -> master

    Branch master set up to track remote branch master from server.</pre>

GIT伺服器搭建(轉載)一、GIT伺服器的搭建On branch masterInitial commitChanges to be committed:(use "git rm --cached &lt;file&gt;..." to unstage)new file: readme.txt

6. 移除資料

** 保留工作區的檔案,删除暫存區的檔案: **git rm --cache test.java

從Git暫存區和工作目錄中一起删除:   git rm -f test.java

删除Git版本倉庫内的檔案快照:    git rm test.java

7. 重命名檔案

git mv 1.txt 2.txt

git commit -m "changed name"

8. 還原資料

GIT伺服器搭建(轉載)一、GIT伺服器的搭建On branch masterInitial commitChanges to be committed:(use "git rm --cached &lt;file&gt;..." to unstage)new file: readme.txt

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ echo "Git is a version control system" >> readme.txt

[root@wls12c demo]$ git add .

[root@wls12c demo]$ git commit -m "test rollback" [master e34168a] test rollback 1 files changed, 1 insertions(+), 0 deletions(-)

[root@wls12c demo]$ git log --pretty=oneline

e34168afeff0e1b1462eb6a52db4bd207d384332 test rollback

2947448a1d50b8a2613f93c54db31db797e65a5c changed name

458eacab758f72ecb4f63524caa301b4c440d702 ADD 1.txt agin

7c1464fa93af1eed88eaa76b15a663bae84cb78a ADD 1.txt agin

361b54468b1d9e4cbd271ce2be196d556ee84556 ADD 1.txt

b548d3bd469cf5f183e9be9a3b2949f6361b5385 Clone the Git repository

[root@wls12c demo]$ git reset --hard 29474 HEAD is now at 2947448 changed name [root@wls12c demo]$ cat readme.txt

I successfully cloned the Git repository</pre>

GIT伺服器搭建(轉載)一、GIT伺服器的搭建On branch masterInitial commitChanges to be committed:(use "git rm --cached &lt;file&gt;..." to unstage)new file: readme.txt

用git reflog指令來檢視所有的曆史記錄,這樣就可以看見還原以前的記錄了。

我們突然發現不應該寫一句話的,可以手工删除(當内容比較多的時候會很麻煩),還可以将檔案内容從暫存區中恢複或者版本庫中恢複:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">[root@wls12c demo]$ echo "xxxxx" >>readme.txt

[root@wls12c demo]$ git checkout -- readme.txt

[root@wls12c demo]$ cat readme.txt

I successfully cloned the Git repository

Git is a version control system</pre>

checkou規則是如果暫存區中有該檔案,則直接從暫存區恢複,如果暫存區沒有該檔案,則将還原成最近一次檔案送出時的快照。

** 9. 管理标簽**

打标簽 tag v1.0

git tag v1.1 -m "version 1.1 released" d316fb

檢視所有的已有标簽:

git tag

檢視此标簽的詳細資訊:

git show v1.0

10. 建立分支

  建立分支:   git branch dev1.0

** 檢視分支:   git branch**

切換分支:   git checkout dev1.0

  删除分支:   **git branch -d ** dev1.0

  合并分支:   git merge dev1.0