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.