git其实是一种多人开发项目时候的版本控制系统,是由LINUX之父Linus开发的,与SVN最大的区别在于可以支持离线操作。
首先安装:我用的网易的yum源http://mirrors.163.com/centos/6/os/x86_64/
然后yum install git -y
一、Git初始化
[root@localhost ~]#git --version 查看git版本
使用Git的第一件事就是设置你的名字和email,这些就是你在提交commit时的签名,每次提交记录里都会包含这些信息。使用git config命令进行配置:
[root@localhost ~]#git config --global user.name dangwanqiang 当前用户姓名和邮箱
[root@localhost ~]#git config --global color.ui true 在git输出中开启颜色显示
[root@localhost ~]#git config --list 实际也是写入文件中去
user.name=dangwanqiang
user.email=goodang517@163.com
color.ui=true
执行了上面的命令后,会在家目录下建立一个叫.gitconfig 的文件(该文件问隐藏文件,需要使用ls -al查看到). 内容一般像下面这样,可以使用vim或cat查看文件内容:
[root@localhost ~]#cat ~/.gitconfig
[user]
name = dangwanqiang
email = goodang517@163.com
[color]
ui = true
二、获得一个Git仓库
既然我们现在把一切都设置好了,那么我们需要一个Git仓库。有两种方法可以得到它:一种是从已有的Git仓库中clone (克隆,复制);还有一种是新建一个仓库,把未进行版本控制的文件进行版本控制。
1.Clone一个仓库
为了得一个项目的拷贝(copy),我们需要知道这个项目仓库的地址(Git URL). Git能在许多协议下使用,所以Git URL可能以ssh://, http(s)://, git://. 有些仓库可以通过不只一种协议来访问。
$ git clone url clone操作完成后,会发现当前目录下多了一个xxx文件夹,这个文件夹里的内容就是我们刚刚clone下来的代码。由于当前`github仅是测试项目,里面仅有一个README.md文件。
$ cd xxxx/
(master)$ ls
README.md
2.初始化一个新的仓库
可以对一个已存在的文件夹用下面的命令让它置于Git的版本控制管理之下。
创建代码目录github:
$ cd /home/dangwanqiang/
$ mkdir github
进入到代码目录,创建并初始化Git仓库:
$ cd github
$ git init
Git会输出:
Initialized empty Git repository in /home/dangwanqiang/github.git/ 通过ls -la命令会发现project目录下会有一个名叫.git 的目录被创建,这意味着一个仓库被初始化了。可以进入到.git目录查看下有哪些内容。
三、在工作目录下的简单操作
git的基本流程如下:
创建或修改文件
使用git add命令添加新创建或修改的文件到本地的缓存区(Index)
使用git commit命令提交到本地代码库
(可选,有的时候并没有可以同步的远端代码库)使用git push命令将本地代码库同步到远端代码库
三部曲:1.init 2.add 3.commit
[root@localhost github]# touch READ.txt a.py //创建自己的工作文件
[root@localhost github]# git status
# On branch master
#
# Initial commit
#
# Untracked files: //未添加到版本库的文件
# (use "git add ..." to include in what will be committed)
#
#READ.txt
#a.py
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost github]# git add a.py //将a.py加到版本库
[root@localhost github]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
#new file: a.py
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
#READ.txt
[root@localhost github]# git add READ.txt //将READ.txt 加到版本库
[root@localhost github]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
#new file: READ.txt
#new file: a.py
#
[root@localhost github]# git commit -m 'init commit' //-m参数指定提交说明
[master (root-commit) 9f045e6] init commit
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 READ.txt
create mode 100644 a.py
[root@localhost github]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost github]# vi a.py //对a.py再次进行修改
[root@localhost github]# git status -s
M a.py
注: 工作区与暂缓区不同,即M标志位在第二位,也意味着需要执行add
[root@localhost github]# git status
# On branch master
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory
#
#modified: a.py
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost github]# git diff //查看工作区与暂缓区具体不同
diff --git a/a.py b/a.py
index 72943a1..dbee026 100644
--- a/a.py
+++ b/a.py
@@ -1 +1,2 @@
aaa
+bbb
[root@localhost github]# git add a.py
[root@localhost github]# git status -s
M a.py
注:暂缓区与最终版本库不同,即M标志位在第一位,也意味着需要执行commit
[root@localhost github]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
#modified: a.py
#
[root@localhost github]# git diff --staged //查看暂缓区与最终版本库的具体不同
diff --git a/a.py b/a.py
index 72943a1..dbee026 100644
--- a/a.py
+++ b/a.py
@@ -1 +1,2 @@
aaa
+bbb
[root@localhost github]# git rm a.py //删除缓存区和工作区的文件
rm 'a.py' [root@localhost github]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
#deleted: a.py
#
[root@localhost github]# git status -s
D a.py [root@localhost github]# ls
READ.txt
[root@localhost github]# git commit -m 'delete a.py' //将删除提交到最终版本库
[master 480eda1] delete a.py
1 file changed, 2 deletions(-)
delete mode 100644 a.py
[root@localhost github]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost github]# git rm --cached READ.txt //只删除暂存区的文件
rm 'READ.txt' [root@localhost github]# ls
READ.txt
[root@localhost github]# git reset READ.txt //从最终版本库恢复文件到暂存区
[root@localhost github]# ls
READ.txt
[root@localhost github]# git status
# On branch master [root@localhost github]# git mv READ.txt READ.LL
[root@localhost github]# git commit -m 'rename read.txt'
这个时候如果本地的仓库连接到了远程Git服务器,可以使用下面的命令将本地仓库同步到远端服务器:
$ git push origin master四、分支与合并
Git的分支可以让你在主线(master分支)之外进行代码提交,同时又不会影响代码库主线。分支的作用体现在多人协作开发中,比如一个团队开发软件,你负责独立的一个功能需要一个月的时间来完成,你就可以创建一个分支,只把该功能的代码提交到这个分支,而其他同事仍然可以继续使用主线开发,你每天的提交不会对他们造成任何影响。当你完成功能后,测试通过再把你的功能分支合并到主线。
1.分支
一个Git仓库可以维护很多开发分支。现在我们来创建一个新的叫 experimental的分支:
$ git branch experimental 运行git branch命令可以查看当前的分支列表,已经目前的开发环境处在哪个分支上:
$ git branch
experimental
* master
experimental 分支是你刚才创建的,master分支是Git系统默认创建的主分支。星号标识了你当工作在哪个分支下,输入git checkout 分支名可以切换到其他分支:
$ git checkout experimental
Switched to branch 'experimental'
切换到experimental分支,切换完成后,先编辑里面的一个文件,再提交(commit)改动,最后切换回 “master”分支:
# 修改文件file1
$ echo "update" >> file1
# 查看当前状态
$ git status
# 添加并提交file1的修改
$ git add file1$ git commit -m "update file1"
# 查看file1的内容
$ cat file1
test
update
# 切换到master分支
$ git checkout master
查看下file1中的内容会发现刚才做的修改已经看不到了。因为刚才的修改时在experimental分支下,现在切换回了master分支,目录下的文件都是master分支上的文件了。
现在可以在master分支下再作一些不同的修改:
# 修改文件file2
$ echo "update again" >> file2
# 查看当前状态
$ git status
# 添加并提交file2的修改
$ git add file2
$ git commit -m "update file2 on master"
# 查看file2的内容
$ cat file2
test
update again
这时,两个分支就有了各自不同的修改,分支的内容都已经不同,如何将多个分支进行合并呢?
可以通过下面的git merge命令来合并experimental到主线分支master:
# 切换到master分支
$ git checkout master
# 将experimental分支合并到master
$ git merge -m 'merge experimental branch' experimental
-m参数仍然是需要填写合并的注释信息。
由于两个branch修改了两个不同的文件,所以合并时不会有冲突,执行上面的命令后合并就完成了。
如果有冲突,比如两个分支都改了一个文件file3,则合并时会失败。首先我们在master分支上修改file3文件并提交:
# 切换到master分支
$ git checkout master
# 修改file3文件
$ echo "master: update file3" >> file3
# 提交到master分支
$ git commit -a -m 'update file3 on master'
然后切换到experimental,修改file3并提交:
# 切换到experimental分支
$ git checkout experimental
# 修改file3文件
$ echo "experimental: update file3" >> file3
# 提交到master分支
$ git commit -a -m 'update file3 on experimental'
切换到master进行合并:
$ git checkout master
$ git merge experimental
Auto-merging file3
CONFLICT (content): Merge conflict in file3
Automatic merge failed; fix conflicts and then commit the result.
合并失败后先用git status查看状态,会发现file3显示为both modified,查看file3内容会发现:
$ cat file3
test
> experimental
上面的内容也可以使用git diff查看,先前已经提到git diff不加参数可以显示未提交到缓存区中的修改内容。
可以看到冲突的内容都被添加到了file3中,我们使用vim编辑这个文件,去掉git自动产生标志冲突的> file1# 创建新的文件file2$ echo "new file2" >> file2# 提交所有修改$ git add *$ git commit -m 'update test branch' 然后,我们查看test分支和master之间的差别:
$ git diff master test
diff --git a/file1 b/file1
index fa49b07..17059cd 100644--- a/file1+++ b/file1@@ -1 +1,2 @@
new file+branch testdiff --git a/file2 b/file2
new file mode 100644
index 0000000..80e7991--- /dev/null+++ b/file2@@ -0,0 +1 @@+new file2
git diff 是一个难以置信的有用的工具,可以找出你项目上任意两个提交点间的差异。可以使用git help diff详细查看其他参数和功能。
3.更多的比较选项
如果你要查看当前的工作目录与另外一个分支的差别,你可以用下面的命令执行:
# 切换到master
$ git checkout master
# 查看与test分支的区别
$ git diff test
diff --git a/file1 b/file1
index 17059cd..fa49b07 100644--- a/file1+++ b/file1@@ -1,2 +1 @@
new file-branch testdiff --git a/file2 b/file2
deleted file mode 100644
index 80e7991..0000000--- a/file2+++ /dev/null@@ -1 +0,0 @@-new file2
你也以加上路径限定符,来只比较某一个文件或目录:
$ git diff test file1
diff --git a/file1 b/file1
index 17059cd..fa49b07 100644--- a/file1+++ b/file1@@ -1,2 +1 @@
new file-branch test
上面这条命令会显示你当前工作目录下的file1与test分支之间的差别。
--stat 参数可以统计一下有哪些文件被改动,有多少行被改动:
$ git diff test --stat
file1 | 1 -
file2 | 1 - 2 files changed, 2 deletions(-)
九、分布式的工作流程
1.分布式的工作流程
你目前的项目在/home/dangwanqiang/github目录下,这是我们的git 仓库(repository),另一个用户也想与你协作开发。他的工作目录在这台机器上,如何让他提交代码到你的git仓库呢?
首先,我们假设另一个用户也用dangwanqiang用户登录,只是工作在不同的目录下开发代码,实际工作中不太可能发生,大部分情况都是多个用户,这个假设只是为了让实验简化。
该用户需要从git仓库进行克隆:
# 进入到临时目录
$ cd /tmp# 克隆git仓库
$ git clone /home/dangwanqiang/github myrepo
$ ls -l myrepo
-rw-rw-r-- 1 dangwanqiang dangwanqiang 31 Dec 22 08:24 README.md
-rw-rw-r-- 1 dangwanqiang dangwanqiang 9 Dec 22 08:24 file1
这就建了一个新的叫"myrepo"的目录,这个目录里包含了一份github仓库的克隆。这份克隆和原始的项目一模一样,并且拥有原始项目的历史记录。
在myrepo做了一些修改并且提交:
$ cd myrepo# 添加新的文件newfile$ echo "newcontent" > newfile# 提交修改$ git add newfile$ git commit -m "add newfile" myrepo修改完成后,如果我们想合并这份修改到github的git仓库该如何做呢?
可以在仓库/home/dangwanqiang/github中把myrepo的修改给拉 (pull)下来。执行下面几条命令:
$ cd /home/dangwanqiang/github$ git pull /tmp/myrepo masterremote: Counting objects: 5, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From /tmp/myrepo
* branch master -> FETCH_HEADUpdating 8bb57aa..866c452Fast-forward
newfile | 1 + 1 file changed, 1 insertion(+)
create mode 100644 newfile# 查看当前目录文件$ ls [8:28:02]README.md file1 newfile
这就把myrepo的主分支合并到了github的当前分支里了。
如果github在myrepo修改文件内容的同时也做了修改的话,可能需要手工去修复冲突。
如果你要经常操作远程分支(remote branch),你可以定义它们的缩写:
$ git remote add myrepo /tmp/myrepo git pull命令执行两个操作: 它从远程分支(remote branch)抓取修改git fetch的内容,然后把它合并git merge进当前的分支。
github里可以用git fetch 来执行git pull前半部分的工作, 但是这条命令并不会把抓下来的修改合并到当前分支里:
$ git fetch myrepo
From /tmp/myrepo
* [new branch] master -> myrepo/master
获取后,我们可以通过git log查看远程分支做的所有修改,由于我们已经合并了所有修改,所以不会有任何输出:
$ git log -p master..myrepo/master 当检查完修改后,github可以把修改合并到它的主分支中:
$ git merge myrepo/masterAlready up-to-date. 如果我们在myrepo目录下执行git pull会发生什么呢?
myrepo会从克隆的位置拉取代码并更新本地仓库,就是把github上的修改同步到本地:
# 进入到github$ cd /home/dangwanqiang/github# 添加一行内容到newfile$ echo "github: new line" >> newfile# 提交修改$ git commit -a -m 'add newline to newfile'[master 8c31532] add newline to newfile 1 file changed, 1 insertion(+)# 进入myrepo目录$ cd /tmp/myrepo# 同步github的所有修改$ git pullremote: Counting objects: 6, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 1), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From /home/dangwanqiang/github 8bb57aa..8c31532 master -> origin/masterUpdating 866c452..8c31532Fast-forward
newfile | 1 + 1 file changed, 1 insertion(+)
因为myrepo是从github仓库克隆的,那么他就不需要指定github仓库的地 址。因为Git把github仓库的地址存储到myrepo的配置文件中,这个地址就是在git pull时默认使用的远程仓库:
$ git config --get remote.origin.url
/home/dangwanqiang/github
如果myrepo和github在不同的主机上,可以通过ssh协议来执行clone 和pull操作:
$ git clone localhost:/home/dangwanqiang/girproject test 这个命令会提示你输入dangwanqiang用户的密码,用户密码随机,可以点击屏幕上方的SSH按钮查看。
2.公共Git仓库
开发过程中,通常大家都会使用一个公共的仓库,并clone到自己的开发环境中,完成一个阶段的代码后可以告诉目标仓库的维护者来pull自己的代码。
如果你和维护者都在同一台机器上有帐号,那么你们可以互相从对 方的仓库目录里直接拉所作的修改,git命令里的仓库地址也可以是本地的某个目录名:
$ git clone /path/to/repository$ git pull /path/to/other/repository 也可以是一个ssh地址:
$ git clone ssh://yourhost/~you/repository3.将修改推到一个公共仓库
通过http或是git协议,其它维护者可以通过远程访问的方式抓取(fetch)你最近的修改,但是他们 没有写权限。如何将本地私有仓库的最近修改主动上传到公共仓库中呢?
最简单的办法就是用git push命令,推送本地的修改到远程Git仓库,执行下面的命令:
$ git push ssh://yourserver.com/~you/proj.git master:master 或者
$ git push ssh://yourserver.com/~you/proj.git master git push命令的目地仓库可以是ssh或http/https协议访问。
4.当推送代码失败时要怎么办
如果推送(push)结果不是快速向前fast forward,可能会报像下面一样的错误:
error: remote 'refs/heads/master' is not an ancestor oflocal 'refs/heads/master'.
Maybe you are not up-to-date and need to pull first?error: failed to push to 'ssh://yourserver.com/~you/proj.git'
这种情况通常是因为没有使用git pull获取远端仓库的最新更新,在本地修改的同时,远端仓库已经变化了(其他协作者提交了代码),此时应该先使用git pull合并最新的修改后再执行git push:
$ git pull$ git push ssh://yourserver.com/~you/proj.git master十、Git标签
1.轻量级标签
我们可以用 git tag不带任何参数创建一个标签(tag)指定某个提交(commit):
# 进入到github目录
$ cd /home/dangwanqiang/github
# 查看git提交记录
$ git log# 选择其中一个记录标志位stable-1的标签,注意需要将后面的8c315325替换成仓库下的真实提交内,commit的名称很长,通常我们只需要写前面8位即可
$ git tag stable-1 8c315325
# 查看当前所有tag
$ git tag
stable-1
这样,我们可以用stable-1 作为提交 8c315325 的代称。
前面这样创建的是一个“轻量级标签”。
如果你想为一个tag添加注释,或是为它添加一个签名, 那么我们就需要创建一个 "标签对象"。
标签对象
git tag中使用-a, -s 或是 -u三个参数中任意一个,都会创建一个标签对象,并且需要一个标签消息(tag message)来为tag添加注释。 如果没有-m 或是 -F 这些参数,命令执行时会启动一个编辑器来让用户输入标签消息。
当这样的一条命令执行后,一个新的对象被添加到Git对象库中,并且标签引用就指向了一个标签对象,而不是指向一个提交,这就是与轻量级标签的区别。
下面是一个创建标签对象的例子:
$ git tag -a stable-2 8c315325 -m "stable 2"
$ git tag
stable-1
stable-2
2.签名的标签
签名标签可以让提交和标签更加完整可信。如果你配有GPG key,那么你就很容易创建签名的标签。首先你要在你的 .git/config 或 ~/.gitconfig 里配好key。
下面是示例:
[user]
signingkey =
你也可以用命令行来配置:
$ git config (--global) user.signingkey 现在你可以在创建标签的时候使用-s 参数来创建“签名的标签”:
$ git tag -s stable-1 1b2e1d63ff 如果没有在配置文件中配GPG key,你可以用-u参数直接指定。
$ git tag -u stable-1 1b2e1d63ff
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com