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 init Initialized 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 <file>..." 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 <root@git.(none)> Your name and email address were configuredautomatically based on 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 [email protected] If the identity used for this commit iswrong, you can fix it with: git commit --amend --author='Your Name <[email protected]>' 1files changed, 1 insertions(+), 0 deletions(-) #這裏能看出有一個文件被提交了 create mode 100644 file1.txt [root@git gittest]# git status # On branch master nothing to commit (working directory clean) #再次查看,暫存區已經沒有要提交的文件了,除非我們再次add文件
配置自己的用戶信息
[root@git gittest]# git config --globaluser.name "juchangfei" [root@git gittest]# git config --globaluser.email [email protected] [root@git gittest]# git config --globalcolor.ui true #配置顏色,這樣在顯示信息的時候就有顏色了 [root@git gittest]# git config –list #查看已經配置成功 user.name=juchangfei [email protected] color.ui=true core.repositoryformatversion=0 core.filemode=true core.bare=false core.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 <file>..." to unstage) # # newfile: file2.txt # # Untracked files: # (use "git add <file>..." 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 <file>..." to include in what will becommitted) # # file3.txt nothing added to commit but untracked filespresent (use "git add" to track) [root@git gittest]#
查看git log
[root@git gittest]# git log #能看到提交了兩次,有兩個文件file1和file2 commitadd32239fbffb84e74167344f26f4a3cd4db58ca Author: juchangfei<[email protected]> Date: Sun Jul 12 08:18:31 2015 +0800 file2 add, file 3 no add commit97e5ecadf5cf798479711030e05c476793c32be0 Author: root<root@git.(none)> 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 <file>..." toupdate what will be committed) # (use "git checkout --<file>..." to discard changes in working directory) # # modified: file1.txt #看到file1被修改 # # Untracked files: # (use "git add <file>..." toinclude in what will be committed) # # file3.txt no changes addedto commit (use "git add" and/or "git commit -a") [root@git gittest]# git diff file1.txt #可以用這個命令查看修改之前與修改之後的區別 diff --gita/file1.txt b/file1.txt index86ac065..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<file>..." 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.txt my first file something [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 reflog add3223 HEAD@{0}:HEAD^: updating HEAD 071fdf3 HEAD@{1}:commit: file1 and file3 add3223 HEAD@{2}:commit: file2 add, file 3 no add 97e5eca HEAD@{3}:commit (initial): my first file to git
根據版本唯一標識符回退
[root@git gittest]# git reset --hard 97e5eca HEAD is now at97e5eca my first file to git [root@git gittest]# ls #查看已經回退第一個版本,只有file1了 file1.txt
檢出操作
[root@git gittest]# cat file1.txt my first file [root@git gittest]# echo "the test something" >> file1.txt #增加一行 [root@git gittest]# cat file1.txt my first file the 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<file>..." 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<file>..." to unstage) # # modified: file1.txt # [root@git gittest]# cat file1.txt #看這裏,file1並沒有被覆蓋 my first file the 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 master error: 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://[email protected]/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://[email protected]/juchangfei/mygit.git #已經改過來了 fetch =+refs/heads/*:refs/remotes/origin/* [root@git gittest]# git pull origin master Fromssh://github.com/juchangfei/mygit * branch master -> FETCH_HEAD Alreadyup-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://[email protected]/juchangfei/mygit.git 90419da..51fcdf1 master -> master Branch 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 branchproduct nothing to commit(working directory clean) [root@git gittest]# ls file1.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]# ls file1.txt README.md
合併分支
[root@git gittest]# git branch * master product [root@git gittest]# git merge product #合併product分支到master上 Updating51fcdf1..76d88ad Fast-forward profile1.txt | 1 + 1 files changed, 1 insertions(+), 0deletions(-) create mode 100644 profile1.txt [root@git gittest]# ls file1.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 master Switched to branch'master' Your branch isahead of 'origin/master' by 1 commit. [root@git gittest]# cat file1.txt my first file the test something [root@git gittest]# echo "master_ write" >> file1.txt #在master上也給file1新增一行 [root@git gittest]# git merge product Updating76d88ad..95d6308 error: 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.txt my first file the test something master_ 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.txt CONFLICT(content): Merge conflict in file1.txt Automatic 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 file the test something <<<<<<<HEAD master_ write ======= product_write >>>>>>>product [root@git gittest]# vim file1.txt [root@git gittest]# cat file1.txt my first file the test something master_ write product_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 product Switched to branch'product' [root@git gittest]# cat file1.txt my first file the test something product_write [root@git gittest]# git merge master Updating95d6308..bc03504 Fast-forward file1.txt | 1 + 1 files changed, 1 insertions(+), 0deletions(-) [root@git gittest]# cat file1.txt #至此衝突解決 my first file the test something master_ write product_write
打標籤
[root@git gittest]# git tag V1.0 [root@git gittest]# git tag V1.0 [root@git gittest]# git show V1.0 #查看標籤狀態 commitbc0350471b73941ca3f26aebb52c97bee94e141c Merge: f80110395d6308 Author: juchangfei<[email protected]> Date: Sun Jul 12 10:42:56 2015 +0800 master and product diff --ccfile1.txt index2b84b37,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://[email protected]/juchangfei/mygit.git * [new tag] V1.0 -> V1.0 [root@git gittest]# git checkout V1.0 #可以直接checkoutV1.0
好了,今天就講到這裏吧~