GIT和SVN之间的主要区别
1、GIT是分布式的,SVN不是,由于这个特征,即使你在没有网络的地方,仍然能够提交文件,查看历史版本记录,创建项目分支等。
2、GIT把内容按元数据方式存储,而SVN是按文件。
3、GIT分支和SVN的分支不同,SVN的分支就是版本库中的另外的一个目录。如果你想知道是否合并了一个分支,你需要手工运行像这样的命令svn propget,svn mergeinfo来确认代码是否被合并。GIT的分支却相当的简单。你可以从同一个工作目录下快速的在几个分支间切换,并且很容易发现未被合并的分支。
4、GIT没有版本号的概念,而SVN有,但GIT有用SHA-1算法来唯一标识一的代码快照。基本可以代替SVN的数字版本号。
安装git
[root@git gittest]# yum install git
创建git版本库
[root@git gittest]# git initInitialized empty Git repository in/home/gittest/.git/[root@git gittest]# ls -a. .. .git[root@git gittest]# ls .git/branches config description HEAD hooks info objects refs
提交一个文件到刚创建的仓库
[root@git gittest]# echo "my firstfile!" > file1.txt-bash: !": event not found[root@git gittest]#[root@git gittest]# echo "my firstfile!"-bash: !": event not found[root@git gittest]# echo "my firstfile" > file1.txt[root@git gittest]# git add file1.txt #这里把file1.txt提交到暂存区[root@git gittest]# git status #查看git当前信息# On branch master## Initial commit## Changes to be committed:# (use "git rm --cached..." to unstage)## newfile: file1.txt #这里能看到新增了一个文件#[root@git gittest]# git commit -m"my first file to git" #这里才是真正的提交到git仓库,-m指定描述字符串[master (root-commit) 97e5eca] my firstfile to git Committer: root Your name and email address were configuredautomatically basedon your username and hostname. Please checkthat they are accurate.You can suppress this message by settingthem explicitly: git config --global user.name "Your Name" #这里提示我们配置自己的用户名和邮箱 git config --global user.email you@example.com If the identity used for this commit iswrong, you can fix it with: git commit --amend --author='Your Name ' 1files changed, 1 insertions(+), 0 deletions(-) #这里能看出有一个文件被提交了 create mode 100644 file1.txt [root@git gittest]# git status# On branch masternothing to commit (working directory clean) #再次查看,暂存区已经没有要提交的文件了,除非我们再次add文件
配置自己的用户信息
[root@git gittest]# git config --globaluser.name "juchangfei"[root@git gittest]# git config --globaluser.email juchangfei@qq.com[root@git gittest]# git config --globalcolor.ui true #配置颜色,这样在显示信息的时候就有颜色了[root@git gittest]# git config –list #查看已经配置成功user.name=juchangfeiuser.email=juchangfei@qq.comcolor.ui=truecore.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=true
查看add和没有add的区别
[root@git gittest]# echo "mysecond file" > file2.txt[root@git gittest]# echo "my thirdfile" > file3.txt[root@git gittest]# git status# On branch master# Changes to be committed:# (use "git reset HEAD..." to unstage)## newfile: file2.txt## Untracked files:# (use "git add ..." to include in what will becommitted)## file3.txt[root@git gittest]# git commit -m"file2 add, file 3 no add"[master add3223] file2 add, file 3 no add 1files changed, 1 insertions(+), 0 deletions(-) create mode 100644 file2.txt[root@git gittest]# git status #从这里能看到没有file3在没有add之前是提交不到仓库的# On branch master# Untracked files:# (use "git add ..." to include in what will becommitted)## file3.txtnothing added to commit but untracked filespresent (use "git add" to track)[root@git gittest]#
查看git log
[root@git gittest]# git log #能看到提交了两次,有两个文件file1和file2commitadd32239fbffb84e74167344f26f4a3cd4db58caAuthor: juchangfeiDate: Sun Jul 12 08:18:31 2015 +0800 file2 add, file 3 no add commit97e5ecadf5cf798479711030e05c476793c32be0Author: root Date: Sun Jul 12 08:01:35 2015 +0800 my first file to git
修改文件
[root@git gittest]# echo "something" >> file1.txt[root@git gittest]# git status# On branch master# Changed but notupdated:# (use "git add..." toupdate what will be committed)# (use "git checkout -- ..." to discard changes in working directory)## modified: file1.txt #看到file1被修改## Untracked files:# (use "git add ..." toinclude in what will be committed)## file3.txtno changes addedto commit (use "git add" and/or "git commit -a") [root@git gittest]# git diff file1.txt #可以用这个命令查看修改之前与修改之后的区别diff --gita/file1.txt b/file1.txtindex86ac065..0dc6077 100644--- a/file1.txt+++ b/file1.txt@@ -1 +1,2 @@ my first file+something #增加的内容在这里[root@git gittest]# git add file1.txt file3.txt[root@git gittest]# git status# On branch master# Changes to becommitted:# (use "git reset HEAD ..." to unstage)## modified: file1.txt# new file: file3.txt#[root@git gittest]# git commit -m "file1 and file3"[master 071fdf3]file1 and file3 2 files changed, 2 insertions(+), 0deletions(-) create mode 100644 file3.txt
回退到上一个版本
[root@git gittest]# cat file1.txtmy first filesomething[root@git gittest]# git reset --hard HEAD^ #^表示回退到上一个版本,^^表示回退到上上个版本,以此类推HEAD is now atadd3223 file2 add, file 3 no add #看这里[root@git gittest]# cat file1.txt #看到file后加的一行没有了my first file [root@git gittest]# ls #file3文件也没有了file1.txt file2.txt
查看版本唯一标识
[root@git gittest]# git reflogadd3223 HEAD@{0}:HEAD^: updating HEAD071fdf3 HEAD@{1}:commit: file1 and file3add3223 HEAD@{2}:commit: file2 add, file 3 no add97e5eca HEAD@{3}:commit (initial): my first file to git
根据版本唯一标识符回退
[root@git gittest]# git reset --hard 97e5ecaHEAD is now at97e5eca my first file to git[root@git gittest]# ls #查看已经回退第一个版本,只有file1了file1.txt
检出操作
[root@git gittest]# cat file1.txtmy first file[root@git gittest]# echo "the test something" >> file1.txt #增加一行[root@git gittest]# cat file1.txtmy first filethe test something[root@git gittest]# git checkout -- file1.txt #检出操作[root@git gittest]# cat file1.txt #刚才添加的又没有了my first file[root@git gittest]# echo "the test something222" >> file1.txt #重新添加一行[root@git gittest]# git add file1.txt #我先添加到暂存区[root@git gittest]# git status# On branch master# Changes to becommitted:# (use "git reset HEAD..." to unstage)## modified: file1.txt#[root@git gittest]# git checkout -- file1.txt #再做检出操作[root@git gittest]# git status# On branch master# Changes to becommitted:# (use "git reset HEAD ..." to unstage)## modified: file1.txt#[root@git gittest]# cat file1.txt #看这里,file1并没有被覆盖my first filethe test something222[root@git gittest]#
添加一个github版本库
首先得在github上注册一个帐号,然后新一个版本库,并且把本地的公钥添加github上,这个过程就不细说了,然后做以下操作。为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的。
[root@git gittest]# cat .git/config[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true[root@git gittest]# git remote add origin https://github.com/juchangfei/mygit.git[root@git gittest]#cat .git/config[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true[remote"origin"] #这是新增的 url =https://github.com/juchangfei/mygit.git fetch =+refs/heads/*:refs/remotes/origin/*[root@git gittest]# git push -u origin mastererror: Therequested URL returned error: 403 Forbidden while accessinghttps://github.com/juchangfei/mygit.git/info/refs fatal: HTTPrequest failed #我们看到这里报错了,解决方法见下文[root@git gittest]# git remote set-url origin ssh://git@github.com/juchangfei/mygit.git #把url修改成这样,在github上创建好项目后,会生成一个项目路径,按照github上的说法,http和ssh均有读写的权限Read+Write access。但是有时候http方式貌似不行。[root@git gittest]# cat .git/config[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true[remote"origin"] url =ssh://git@github.com/juchangfei/mygit.git #已经改过来了 fetch =+refs/heads/*:refs/remotes/origin/*[root@git gittest]# git pull origin masterFromssh://github.com/juchangfei/mygit * branch master -> FETCH_HEADAlreadyup-to-date.[root@git gittest]# git push -u origin master #push完之后在github上就能看到file1了,如下图Counting objects:9, done.Delta compressionusing up to 4 threads.Compressingobjects: 100% (4/4), done.Writing objects:100% (8/8), 747 bytes, done.Total 8 (delta 0),reused 0 (delta 0)To ssh://git@github.com/juchangfei/mygit.git 90419da..51fcdf1 master -> masterBranch master setup to track remote branch master from origin.
创建新的分支
[root@git gittest]# git branch product #创建一个product分支[root@git gittest]# git branch #现在在master分支上* master product[root@git gittest]#git checkout product #切换到product分支Switched to branch'product'[root@git gittest]# git branch #查看已经切换完成 master* product
在分支上提交文件
[root@git gittest]# echo "pro file hahaha" > profile1.txt[root@git gittest]# git add profile1.txt[root@git gittest]# git commit -m "pro file test" #在product上提交了一个文件profile1[product 76d88ad]pro file test 1 files changed, 1 insertions(+), 0deletions(-) create mode 100644 profile1.txt[root@git gittest]# git status# On branchproductnothing to commit(working directory clean)[root@git gittest]# lsfile1.txt profile1.txt README.md[root@git gittest]# git branch master #切换到master分支后发现profile1没有了fatal: A branchnamed 'master' already exists.[root@git gittest]# git checkout master Switched to branch'master'[root@git gittest]# lsfile1.txt README.md
合并分支
[root@git gittest]# git branch* master product[root@git gittest]# git merge product #合并product分支到master上Updating51fcdf1..76d88adFast-forward profile1.txt | 1 + 1 files changed, 1 insertions(+), 0deletions(-) create mode 100644 profile1.txt[root@git gittest]# lsfile1.txt profile1.txt README.md
解决冲突
[root@git gittest]# git branch master* product[root@git gittest]# echo "product_write" >> file1.txt #在product上给file1新增一行并提交[root@git gittest]# git add file1.txt[root@git gittest]# git commit -m "product test"[product 95d6308]product test 1 files changed, 1 insertions(+), 0deletions(-)[root@git gittest]# git checkout masterSwitched to branch'master'Your branch isahead of 'origin/master' by 1 commit.[root@git gittest]# cat file1.txtmy first filethe test something[root@git gittest]# echo "master_ write" >> file1.txt #在master上也给file1新增一行[root@git gittest]# git merge productUpdating76d88ad..95d6308error: Your localchanges to 'file1.txt' would be overwritten by merge. Aborting. #这时候合并就会提示报错了,会让你先提交代码Please, commityour changes or stash them before you can merge.[root@git gittest]# cat file1.txtmy first filethe test somethingmaster_ write[root@git gittest]# git add file1.txt[root@git gittest]# git commit -m "master test" #master提交[master f801103]master test 1 files changed, 1 insertions(+), 0deletions(-)[root@git gittest]# git merge product #提交完之后再合并发现有冲突了Auto-mergingfile1.txtCONFLICT(content): Merge conflict in file1.txtAutomatic mergefailed; fix conflicts and then commit the result.[root@git gittest]# vim file1.txt[root@git gittest]# cat file1.txt #再查看file1,文件内容如下,product和master提交的内容都在,并且有特殊符号表名了有冲突的地方,这时候只能手动解决冲突my first filethe test something<<<<<<>>>>>>product[root@git gittest]# vim file1.txt[root@git gittest]# cat file1.txtmy first filethe test somethingmaster_ writeproduct_write[root@git gittest]# git add file1.txt[root@git gittest]# git commit -m "master and product"[master bc03504]master and product[root@git gittest]# git status# On branch master# Your branch isahead of 'origin/master' by 4 commits.#nothing to commit(working directory clean)[root@git gittest]# git checkout productSwitched to branch'product'[root@git gittest]# cat file1.txtmy first filethe test somethingproduct_write[root@git gittest]# git merge masterUpdating95d6308..bc03504Fast-forward file1.txt | 1 + 1 files changed, 1 insertions(+), 0deletions(-)[root@git gittest]# cat file1.txt #至此冲突解决my first filethe test somethingmaster_ writeproduct_write
打标签
[root@git gittest]# git tag V1.0[root@git gittest]# git tagV1.0[root@git gittest]# git show V1.0 #查看标签状态commitbc0350471b73941ca3f26aebb52c97bee94e141cMerge: f80110395d6308Author: juchangfeiDate: Sun Jul 12 10:42:56 2015 +0800 master and product diff --ccfile1.txtindex2b84b37,db989a8..5108ce6--- a/file1.txt+++ b/file1.txt@@@ -1,3 -1,3 +1,4@@@ my first file the test something +master_ write+ product_write[root@git gittest]# git push origin V1.0 #把标签推送到github上Counting objects:14, done.Delta compressionusing up to 4 threads.Compressingobjects: 100% (9/9), done.Writing objects:100% (12/12), 1.20 KiB, done.Total 12 (delta1), reused 0 (delta 0)Tossh://git@github.com/juchangfei/mygit.git * [new tag] V1.0 -> V1.0[root@git gittest]# git checkout V1.0 #可以直接checkoutV1.0
好了,今天就讲到这里吧~