git是一種開源的分佈式版本控制系統,最初由linus爲linux內核開發編寫。
我將git使用方式分爲三種:linux
本地單一工做區。使用git管理一個本地文件夾,全部的版本信息都存儲在該文件夾下的.git文件夾中。工做區能夠稍後被推送到本地版本倉庫或遠程服務器倉庫。git
本地版本倉庫。在本地建立一個版本倉庫文件夾,只含版本信息,不含工做區。另行建立工做區文件夾,從版本倉庫中克隆版本庫或其子文件夾,在工做區中修改和提交代碼,而後推送到本地版本倉庫中。本地倉庫和git服務器上的倉庫同樣。vim
遠程版本倉庫。經過遠程git服務器與合做者共享版本信息,全部版本信息都可以被克隆到本地工做區,服務器主要做用是方便共享。將須要的文件夾克隆到本地,在本地修改、提交後,推送到遠程版本倉庫,合做者在以後訪問遠程服務器時也會看到你提交的代碼。緩存
【一】本地單一工做區
初始化工做區,工做區文件夾是否爲空均可以。 服務器
$ cd ~/workspace
$ git init
查看工做區的狀態,下面例子中,有一個沒有被git管理的文件app
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
將該文件交給git管理,從新看一下狀態。此時test.txt文件已經被加入git的管理列表,而且被複制到緩存區(stage)。分佈式
$ git add test.txt
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt
修改~/workspace/test.txt文件後,緩存區中的該文件沒有變化。this
$ vim test.txt
$ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: test.txt Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: test.txt
還本來次修改到上次更新到緩存區前spa
$ git checkout test.txt
修改文件後將文件更新到緩存區(沒錯是git add).版本控制
$ vim test.txt
$ git add test.txt
從緩衝區中刪除test.txt文件
$ git rm --cache test.txt rm 'test.txt' $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) test.txt nothing added to commit but untracked files present (use "git add" to track)
提交緩衝區中的文件到版本庫,產生一個新的版本,該版本的註釋爲add test.txt。-a選項表示將緩存區中的全部文件提交,不然可能會給你一個臨時的提交列表文件讓你修改。
$ git add test.txt $ git commit -a -m "add test.txt" [master (root-commit) eff9984] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt
查看版本記錄,剛纔提交的版本特徵碼爲eff99847eaa887b8f6b125307b2b22087a3f8f38
$ git log commit eff99847eaa887b8f6b125307b2b22087a3f8f38 Author: chenjunhua <chenjh@pegoe.com> Date: Wed Sep 2 14:18:43 2015 +0800 add test.txt
修改test.txt,查看修改內容,不加文件路徑則顯示當前路徑下的全部更改。
git diff test.txt
查看已經add的文件與上一次提交版本的差別
git diff --cached path_to_file
配置使用meld代替diff比較文本文件差別
$ git config --global diff.external "/opt/meld_git.sh" $ sudo echo "meld $2 $5" > /opt/meld_git.sh
$ sudo chmod a+x /opt/meld_git.sh
【二】本地版本倉庫
建立一個空文件夾,並初始化一個不包含工做區的版本倉庫。
$ sudo mkdir -p /home/git/aaa.git $ sudo chmod a+w /home/git/aaa.git $ cd /home/git/aaa.git $ git init --bare
從版本倉庫克隆工做區,修改、提交到本地倉庫。
$ cd ~/workspace $ git clone /home/git/aaa.git . //cp ..... $ git add * $ git commit -a -m "first commit"
第一次提交到本地倉庫後查看狀態會以下提示
$ git status
On branch masterYour branch is based on 'origin/master', but the upstream is gone. (use "git branch --unset-upstream" to fixup) nothing to commit, working directory clean
不用擔憂,能夠放心的推送。這裏的origin表明咱們clone的來源/home/git/aaa.git,是git自動生成的,master是分支的名字,這裏是主線分支。
$ git push origin master
Counting objects: 63, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (61/61), done.
Writing objects: 100% (63/63), 280.52 KiB | 0 bytes/s, done.
Total 63 (delta 8), reused 0 (delta 0)
第一次推送可能會提示
warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple
按照提示,執行 git config --global push.default matching 後從新推送便可。
再次修改,提交到本地倉庫以後,查看狀態
$ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working directory clean
【三】遠程版本倉庫
從遠程倉庫克隆到本地
$cd ~/src_git
$git clone jacob@192.168.183.50:/opt/git_repo/acc.git . Cloning into '.'... jacob@192.168.183.50's password: warning: You appear to have cloned an empty repository. Checking connectivity... done.