git安装使用

http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/


(1)安装git
[root@node1 ~]# yum install git


(2)查看帮助
[root@node1 ~]# git config
[root@node1 ~]# git help

(3)设置添加用户,最好用户+主机名和邮箱地址
[root@node1 ~]# git config --global user.name "xiaozhiqi2016"
[root@node1 ~]# git config --global user.email "329275108@qq.com"

(4)设置编辑器
[root@node1 ~]# git config --global core.editor vim

(5)设置颜色
[root@node1 ~]# git config --global color.ui true

(6)查看已经设置的列表
[root@node1 ~]# git config --list
user.name=xiaozhiqi2016
user.email=329275108@qq.com
core.editor=vim
color.ui=true


***********************************************************************************************

建立一个目录用作版本仓库
[root@node1 ~]# mkdir gitlab
[root@node1 ~]# cd gitlab

初始化,记住不要用记事本可以用nodepad++,字符集会不一样,只能管理文本
[root@node1 gitlab]# git init
Initialized empty Git repository in /root/gitlab/.git/

[root@node1 gitlab]# ll -a
total 12
drwxr-xr-x. 3 root root 4096 Oct 21 17:16 .
dr-xr-x---. 5 root root 4096 Oct 21 17:15 ..
drwxr-xr-x. 7 root root 4096 Oct 21 17:16 .git

 

例子:
1、添加一个文本文件
[root@node1 gitlab]# vi readme.txt
[root@node1 gitlab]# cat readme.txt
hehe

2、查看git的状态
[root@node1 gitlab]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# readme.txt
nothing added to commit but untracked files present (use "git add" to track)

3、标记这个将要提交的文件
[root@node1 gitlab]# git add readme.txt

[root@node1 gitlab]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: readme.txt
#

4、提交到本地仓库,并添加注释
[root@node1 gitlab]# git commit -m "the first commit"
[master (root-commit) 0caf800] the first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 readme.txt

再次查看状态已经提交
[root@node1 gitlab]# git status
# On branch master
nothing to commit (working directory clean)

 

***********************************************************************************************

例子:通过git log查看版本提交日志
二、继续管理一个文本
[root@node1 gitlab]# vi deploy.sh
[root@node1 gitlab]# cat deploy.sh
#!/bin/bash
echo hehe

[root@node1 gitlab]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# deploy.sh
nothing added to commit but untracked files present (use "git add" to track)

[root@node1 gitlab]# git add deploy.sh
[root@node1 gitlab]# git commit -m "the second commit"
[master (root-commit) 0caf800] the second commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 deploy.sh


查看git log日志
[root@node1 gitlab]# git log
commit 74a771a9213697cc776394c4d827295dfe4a96bf
Author: xiaozhiqi2016@live.com <xiaozhiqi2016@live.com>
Date: Wed Oct 21 17:30:10 2015 +0800

second commit

commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: xiaozhiqi2016@live.com <xiaozhiqi2016@live.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit

查看git log日志,只显示注释
[root@node1 gitlab]# git log --oneline


***********************************************************************************************
例子:比较两次提交的文件的差别,比较有3个空间的问题
三、对第一个版本进行更新
[root@node1 gitlab]# vi readme.txt
[root@node1 gitlab]# cat readme.txt
hehe
hehe

(1)缓冲区的和原来的文本进行对比
[root@node1 gitlab]# git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 91ca0fa..197cac2 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
hehe
+hehe

(2)添加标记到缓冲区
[root@node1 gitlab]# git add readme.txt

(3)缓冲区和本地仓库的比较
[root@node1 gitlab]# git diff --cached readme.txt

(4)提交本地仓库,并添加注释
[root@node1 gitlab]# git commit -m "add 2hehe"
[master 7c10652] add 2hehe
1 files changed, 1 insertions(+), 0 deletions(-)

(5)查看git log
[root@node1 gitlab]# git log
commit 7c10652f853a28e323ef71f3d51934a55c9e08c6
Author: xiaozhiqi2016@live.com <329275108@qq.com>
Date: Wed Oct 21 17:48:28 2015 +0800

add 2hehe

commit 74a771a9213697cc776394c4d827295dfe4a96bf
Author: xiaozhiqi2016@live.com <xiaozhiqi2016@live.com>
Date: Wed Oct 21 17:30:10 2015 +0800

second commit

commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: xiaozhiqi2016@live.com <xiaozhiqi2016@live.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit



***********************************************************************************************
例子:版本回滚
(1)版本回滚上一个版本,HEAD^表示上一个版本
[root@node1 gitlab]# git reset --hard HEAD^
HEAD is now at 74a771a second commit

[root@node1 gitlab]# cat readme.txt
hehe

[root@node1 gitlab]# git log
commit 74a771a9213697cc776394c4d827295dfe4a96bf
Author: xiaozhiqi2016@live.com <xiaozhiqi2016@live.com>
Date: Wed Oct 21 17:30:10 2015 +0800

second commit

commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: xiaozhiqi2016@gmail.com <xiaozhiqi2016@live.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit


(2)指定版本回滚
[root@node1 gitlab]# git reflog
74a771a HEAD@{0}: HEAD^: updating HEAD
7c10652 HEAD@{1}: commit: add 2hehe
74a771a HEAD@{2}: commit: second commit
0caf800 HEAD@{3}: commit (initial): the first commit

[root@node1 gitlab]# git reset --hard 0caf800
HEAD is now at 0caf800 the first commit

[root@node1 gitlab]# git log
commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: xiaozhiqi2016@gmail.com <xiaozhiqi2016@live.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit
[root@node1 gitlab]# ls
readme.txt
[root@node1 gitlab]# cat readme.txt
hehe

看到只有readme.txt文件了deploy.sh这个是在第一次提交之后产生的,所以回滚到第一次所以deloy.sh文件没有了。


***********************************************************************************************

例子:撤销修改的代码或者删除的文件,只要没有提交都可以恢复的
五、再一次修改版本添加提交
[root@node1 gitlab]# vi readme.txt
[root@node1 gitlab]# cat readme.txt
hehe
wo shi hehe

[root@node1 gitlab]# git add readme.txt
[root@node1 gitlab]# git commit -m "add 2"
[master cbe0525] add 2
1 files changed, 1 insertions(+), 0 deletions(-)

[root@node1 gitlab]# git status
# On branch master
nothing to commit (working directory clean)

[root@node1 gitlab]# git log
commit cbe0525305bb22baccc19c837ebc8c39dd6228aa
Author: xiaozhiqi2016@live.com <329275108@qq.com>
Date: Wed Oct 21 18:02:24 2015 +0800

add 2

commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: xiaozhiqi2016@gmail.com <xiaozhiqi2016@live.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit

 

再一次修改版本添加未提交,撤销修改的代码
[root@node1 gitlab]# vi readme.txt
[root@node1 gitlab]# cat readme.txt
hehe
wo shi hehe
hehe hehe

[root@node1 gitlab]# git add readme.txt
[root@node1 gitlab]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#

[root@node1 gitlab]# git checkout -- readme.txt
[root@node1 gitlab]# cat readme.txt
hehe
wo shi hehe


删掉这个文件,然后撤销删除
[root@node1 gitlab]# git rm readme.txt

然后恢复这个文件
[root@node1 gitlab]# git checkout -- readme.txt

 

***********************************************************************************************

例子:要忽略某个文件不提交,在本地仓库下创建一个.gitignore文件
[root@node1 ~]# touch a.sh .gitignore

把要忽略的文件的名字写到这个文件里面
[root@node1 ~]# echo .gitignore >> .gitignore
[root@node1 ~]# echo a.sh >> .gitignore

[root@node1 ~]# vi readme.txt
hehe
wo shi hehe
wo shi heihei

[root@node1 ~]# git add .
[root@node1 ~]# git commit -m "xxoo"




***********************************************************************************************

例子:为GitHub上远程仓库使用密钥,一切是以本地为基准

1.本地生成密钥对
[root@node1 gitlab]# ssh-keygen -t rsa -P ""
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? n

[root@node1 gitlab]# cd /root/.ssh
[root@node1 .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArP517Nu/bOJS/tL2UGHZCxy9keicjw3N+bv0lQMN3yX7zR5bnXlQWFZuLvphmTId+cR2xojDlgRJd17SrFM8upQRe9z+IkQpQ2Nem/uXx8uUfkxt++thNtDaV0/mG/ViR2Nf43Xow8ZdXopuwFeQydXGDI2zYERWr05h4QqxtGVmkQwLQwQcXBOXC8M5YfxpOyMmcyrQ9JOrOt6uf2K10K4yNm/ADRtK7TvTsE5wIsNao4xFyHgHJX8gJ5OAuTfzaxgTh+Qpv6Uo+zAPGT6lIFVQTGxgbJp7dP48PPnmdhx+iziR0dATpHycz69zmmbHqpRxw58nK4jo6+KuMhyeMQ== xiaozhiqi2016@live.com


2.将生成的密钥复制到github上,然后才能继续往下走

 

3.从本地仓库建立与远程的关联
[root@node1 ~]# cd gitlab/
[root@node1 gitlab]# git remote add origin git@github.com:xiaozhiqi2016/demo.git
[root@node1 gitlab]# cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:xiaozhiqi2016/demo.git
fetch = +refs/heads/*:refs/remotes/origin/*


(1)从github远程拉到本地
注意:如果不是从远程clone下来的仓库一定是pull,然后push.
[root@node1 gitlab]# git pull origin master
From github.com:xiaozhiqi2016/demo
* branch master -> FETCH_HEAD
Merge made by recursive.
.gitignore | 57 +++++++++++++++++
LICENSE | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 258 insertions(+), 0 deletions(-)
create mode 100644 .gitignore
create mode 100644 LICENSE

[root@node1 gitlab]# ll
total 16
-rw-r--r--. 1 root root 11357 Oct 21 21:34 LICENSE
-rw-r--r--. 1 root root 28 Oct 21 18:04 readme.txt

[root@node1 gitlab]# ls -a
. .. .git .gitignore LICENSE readme.txt


(2)从本地推送到github远程
[root@node1 gitlab]# git push -u origin master
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
Counting objects: 12, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (11/11), 1.01 KiB, done.
Total 11 (delta 0), reused 0 (delta 0)
To git@github.com:xiaozhiqi2016/demo.git
8a4592d..09150c1 master -> master
Branch master set up to track remote branch master from origin.



把远程仓库复制到本地
[root@node1 gitlab]# cd /tmp
[root@node1 tmp]# git clone git@github.com:xiaozhiqi2016/demo.git
Initialized empty Git repository in /tmp/demo/.git/
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 15 (delta 1), reused 11 (delta 0), pack-reused 0
Receiving objects: 100% (15/15), 5.42 KiB, done.
Resolving deltas: 100% (1/1), done.

[root@node1 tmp]# ls -a demo/
. .. .git .gitignore LICENSE readme.txt

***********************************************************************************************


(1)创建分支
[root@node1 gitlab]# git branch dev


(2)切换分支
[root@node1 gitlab]# git checkout dev
Switched to branch 'dev'


或者git checkout -b dev 相当于创建分支并切换分支

(3)查看当前分支
[root@node1 gitlab]# git branch
* dev
master

(4)在分支新建一个文件
[root@node1 gitlab]# vi dev.txt
hehehehehehehe


(5)标记这个文件
[root@node1 gitlab]# git add dev.txt

(6)提交修改
[root@node1 gitlab]# git commit -m "add dev.txt"
[dev b089e92] add dev.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 dev.txt


(7)切换到master分支
[root@node1 gitlab]# git checkout master
Switched to branch 'master'
[root@node1 gitlab]# ls
LICENSE readme.txt


(8)在master分支,将dev分支合并到master上
[root@node1 gitlab]# git merge dev
Updating 09150c1..b089e92
Fast-forward
dev.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 dev.txt

[root@node1 gitlab]# ls
dev.txt LICENSE readme.txt

[root@node1 gitlab]# git branch
dev
* master

(9)删除分支,暂时不删
[root@node1 gitlab]# git branch -d dev


***********************************************************************************************

冲突问题

(1)创建切换分支
[root@node1 gitlab]# git checkout -b test
M dev.txt
Switched to a new branch 'test'

[root@node1 gitlab]# git branch
dev
master
* test

(2)创建文件标记提交
[root@node1 gitlab]# vi dev.txt
hehehehehehehe123456

[root@node1 gitlab]# git add dev.txt

[root@node1 gitlab]# git commit -m "change dev.txt"
[test 9e1c6af] change dev.txt
1 files changed, 1 insertions(+), 1 deletions(-)

[root@node1 gitlab]# ls
dev.txt LICENSE readme.txt

(3)切换主分支
[root@node1 gitlab]# git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.

(4)修改和分支一样的文件并标记提交
[root@node1 gitlab]# vi dev.txt
[root@node1 gitlab]# git add dev.txt
[root@node1 gitlab]# git commit -m "master change"
[master 7e9f3e8] master change
1 files changed, 1 insertions(+), 1 deletions(-)

(5)合并分支的时候发生冲突
[root@node1 gitlab]# git merge test
Auto-merging dev.txt
CONFLICT (content): Merge conflict in dev.txt
Automatic merge failed; fix conflicts and then commit the result.


[root@node1 gitlab]# cat dev.txt
<<<<<<< HEAD
hehehehehehehe123
=======
hehehehehehehe123456
>>>>>>> test

(6)手动解决冲突并标记提交
[root@node1 gitlab]# vi dev.txt
[root@node1 gitlab]# cat dev.txt
hehehehehehehe123456
[root@node1 gitlab]# git add dev.txt
[root@node1 gitlab]# git commit -m "change"
[master 62f1628] change

(7)重新合并分支
[root@node1 gitlab]# git merge test
Already up-to-date.

(8)推送分支
[root@node1 gitlab]# git push origin dev
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 352 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:xiaozhiqi2016/demo.git
* [new branch] dev -> dev

[root@node1 gitlab]# git push origin test
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 279 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:xiaozhiqi2016/demo.git
* [new branch] test -> test


***********************************************************************************************

给分支打个标签
[root@node1 gitlab]# git checkout master
Already on 'master'
Your branch is ahead of 'origin/master' by 4 commits.

[root@node1 gitlab]# git branch
dev
* master
test


[root@node1 gitlab]# git tag v1.0
[root@node1 gitlab]# git tag
v1.0

查看标签
[root@node1 gitlab]# git show v1.0
commit 62f1628fae9993e61148b883a91f6f95dab80096
Merge: 7e9f3e8 9e1c6af
Author: xiaozhiqi2016@live.com <329275108@qq.com>
Date: Wed Oct 21 22:35:50 2015 +0800


推送标签
[root@node1 gitlab]# git push origin v1.0
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 463 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
To git@github.com:xiaozhiqi2016/demo.git
* [new tag] v1.0 -> v1.0



[root@node1 gitlab]# git checkout v1.0
Note: checking out 'v1.0'.

[root@node1 gitlab]# cat .gitignore

posted @ 2016-10-13 11:30  Python自动化运维之路  阅读(1007)  评论(0)    收藏  举报