首页 > 技术文章 > git的简单使用

jacob1934 2015-09-01 15:00 原文

git是一种开源的分布式版本控制系统,最初由linus为linux内核开发编写。
我将git使用方式分为三种:

  本地单一工作区。使用git管理一个本地文件夹,所有的版本信息都存储在该文件夹下的.git文件夹中。工作区可以稍后被推送到本地版本仓库或远程服务器仓库。

  本地版本仓库。在本地创建一个版本仓库文件夹,只含版本信息,不含工作区。另行创建工作区文件夹,从版本仓库中克隆版本库或其子文件夹,在工作区中修改和提交代码,然后推送到本地版本仓库中。本地仓库和git服务器上的仓库一样。

  远程版本仓库。通过远程git服务器与合作者共享版本信息,所有版本信息都能够被克隆到本地工作区,服务器主要作用是方便共享。将需要的文件夹克隆到本地,在本地修改、提交后,推送到远程版本仓库,合作者在之后访问远程服务器时也会看到你提交的代码。

【一】本地单一工作区
  初始化工作区,工作区文件夹是否为空都可以。  

$ cd ~/workspace
$ git init

  查看工作区的状态,下面例子中,有一个没有被git管理的文件

$ 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文件后,缓存区中的该文件没有变化。

$ 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

还原本次修改到上次更新到缓存区前

$ 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: jacob <jacob****@outlook.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.

 



 

推荐阅读