一、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://git@120.77.85.77: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 "root@wls12c.com" [root@wls12c demo]$ git config --global core.editor vim</pre>
3. 向Git本地仓库中提交一个新文件
[](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
[root@wls12c demo]$ git log
commit b548d3bd469cf5f183e9be9a3b2949f6361b5385
Author: Scott Cho root@wls12c.com Date: Mon Feb 27 17:42:29 2017 +0800 Clone the Git repository</pre>
](javascript:void(0); "复制代码")
** 常见技巧:**
** 查看当前文件内容与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 ssh://git@120.77.85.77:2121/opt/demo.git</pre>
5. 将文件提交到远程Git服务器
[](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]$ 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 ssh://git@120.77.85.77:2121/opt/demo.git
- [new branch] master -> master
Branch master set up to track remote branch master from server.</pre>
](javascript:void(0); "复制代码")
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. 还原数据
[](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 "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>
](javascript:void(0); "复制代码")
用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