Git简~介

Git是一个分布式版本控制系统,其他的版本控制系统我只用过SVN,但用的时间不长。大家都知道,分布式的好处多多,而且分布式已经包含了集中式的几乎所有功能。Linus创造Git的传奇经历就不再赘述,直接记录git命令吧! 文章会尽量按照使用git的顺序来记录,不定时的更新,版面可能会较为杂乱。

你的计算机上是否有Git?

windows版本的安装: Git下载 ,下载之后双击安装即可。

仓库怎么创建?

仓库(repository),即是一个项目,你要对这个项目进行版本管理。使用如下命令来初始化一个仓库。

creatint@wds MINGW64 /e/Weixin
$ git init;
Initialized empty Git repository in E:/Weixin/.git/
creatint@wds MINGW64 /e/Weixin (master)

文件还需要添加?

是的,此时虽然建立了仓库,但是其实文件是没有添加进去的,它是空的。

下面的代码是经常用到的,可以查看当前的文件状态!

creatint@wds MINGW64 /e/Weixin (master)
$ git status;
On branch master

Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

        Common/
        Home/
        index.php
        templates/

nothing added to commit but untracked files present (use "git add" to track)
creatint@wds MINGW64 /e/Weixin (master)

可以看到没有追踪的文件/文件夹,没有track意味着git没有对它进行管理。你需要添加这些文件(其实是添加到了暂存区,等待下一个命令)。

怎么提交修改?

添加文件之后就是提交文件(提交暂存区的文件到工作区)。

creatint@wds MINGW64 /e/Weixin (master)
$ git add Home;

查看状态:

creatint@wds MINGW64 /e/Weixin (master)
$ git status;
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        new file:   .idea/vcs.xml
        modified:   Home/index.html

creatint@wds MINGW64 /e/Weixin (master)

从上面可以看到,文件Home/index.html已经被修改(追踪,加入到了暂存区),最后再执行提交命令,把此状态添加到当前版本中。

creatint@wds MINGW64 /e/Weixin (master)
$ git commit -m "修改了标题";
On branch master
nothing to commit, working directory clean
creatint@wds MINGW64 /e/Weixin (master)

版本怎样穿梭?

先看看git状态,好像修改了文件?

creatint@wds MINGW64 /e/Weixin (master)
$ git status;
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   test.php

creatint@wds MINGW64 /e/Weixin (master)

查看一下有什么变化

$ git diff test.php
diff --git a/test.php b/test.php
index 0a02553..790878e 100644
--- a/test.php
+++ b/test.php
@@ -5,4 +5,5 @@
  * Date: 2016/4/12
  * Time: 10:51
  */
-echo 'this is a test.';
\ No newline at end of file
+echo 'this is a test.';
+echo 'hello';
\ No newline at end of file

creatint@wds MINGW64 /e/Weixin (master)

可以看到,文件增加了一句:echo 'hello';。我们执行add、commit命令后,得到了新的版本。

版本回退

我们查看一下此时test.php的代码

$ cat test.php
<?php
/**
 * Created by PhpStorm.
 * User: creatint
 * Date: 2016/4/12
 * Time: 10:51
 */
echo 'this is a test.';
echo 'hello';
creatint@wds MINGW64 /e/Weixin (master)

提交日志是什么?

提交日志清楚地记录了提交命令,查看他们:

$ git log;
commit 52fc500fde16f4b9911e87e42d314a98af718a57
Author: creatint <creatint@163.com>
Date:   Tue Apr 12 10:54:13 2016 +0800
    修改了test.php文件
commit 578ebf64a3e451a89f144eff37727d5569834158
Author: creatint <creatint@163.com>
Date:   Tue Apr 12 10:43:44 2016 +0800
    增加了test.php文件
creatint@wds MINGW64 /e/Weixin (master)

可以使之显示在一行

$ git log --pretty=oneline
52fc500fde16f4b9911e87e42d314a98af718a57 修改了test.php文件
578ebf64a3e451a89f144eff37727d5569834158 增加了test.php文件
creatint@wds MINGW64 /e/Weixin (master)

其中那一长串的十六进制字符串是版本的特征字符串,也就是版本的ID [caption id="" align="aligncenter" width="791"] git提交历史时间线[/caption] 版本是通过指针指向的,更改版本,其实是修改了指针,而各个版本的记录仍然是存在的,只是看不到罢了。

$ git reset --hard HEAD^
#git reset --hard 578ebf6
HEAD is now at 578ebf6 增加了test.php文件
creatint@wds MINGW64 /e/Weixin (master)

查看文件内容,我们发现,已经回到修改文件之前的状态了。

$ cat test.php
<?php
/**
 * Created by PhpStorm.
 * User: creatint
 * Date: 2016/4/12
 * Time: 10:51
 */
echo 'this is a test.';
creatint@wds MINGW64 /e/Weixin (master)

撤销功能怎么实现?

$ git checkout test.php

如果你执行了addcommit,之后对文件进行了一些修改,过了一小时却想退回到修改之前的状态呢?使用上面的代码吧!

分支管理怎么弄?

分支(branch)就是树杈,创建分支时,相当于一次复制(其实只是多了个指针),之后对该分支的修改与之前的分支无关,你也可以把两个分支合并。一个分支就像一个版本,可以很好的对文档版本进行控制。 创建代码仓库时,系统默认的分支为master,在哪里查看呢?看下面:

creatint@wds MINGW64 /e/Weixin (master)

就句话出现在每一次命令输出行的上方,其中的master代表当前的分支名。

创建分支branch

$ git checkout -b dev
Switched to a new branch 'dev'
creatint@wds MINGW64 /e/Weixin (dev)

git checkout -b dev的等于:

$ git branch dev
$ git checkout dev

查看当前分支:

$ git branch
* dev
  master
creatint@wds MINGW64 /e/Weixin (master)

合并分支

在dev中做了工作,想要把dev的工作同步到master,就需要合并分支。合并分支时,要合并到哪个分支,就要先切换至哪个分支,然后执行合并命令

creatint@wds MINGW64 /e/Weixin <class="variable">(dev)
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
creatint@wds MINGW64 /e/Weixin <class="variable">(master)

可以看到已经切换至master。可以使用git diff dev查看devmaster的不同之处。

creatint@wds MINGW64 /e/Html5 (master)
$ git merge dev
Already up-to-date.
creatint@wds MINGW64 /e/Html5 (master)

这样,dev的工作便同步到了master中。

先有了远程仓库,在本地怎么管理呢?

这里以开源中国Git为例

我已经在开源中国Git上创建了一个项目 首先,在本地init一个空仓库

creatint@wds MINGW64 /e/Html5
$ git init;
Initialized empty Git repository in E:/Html5/.git/

然后,添加远程仓库

creatint@wds MINGW64 /e/Html5 (master)
$ git remote add origin https://git.oschina.net/yotaku/Html5.git;

没有反应?看看下面:

creatint@wds MINGW64 /e/Html5 (master)
$ git remote remote -v 
origin  https://git.oschina.net/yotaku/Html5.git (fetch)
origin  https://git.oschina.net/yotaku/Html5.git (push)

上面显示了连接的远程仓库

creatint@wds MINGW64 /e/Html5 (master)
$ git fetch origin master 
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (9/9), done.
From https://git.oschina.net/yotaku/Html5
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

获取远程仓库master分支的改变内容,但是没有加入索引。远程仓库分支用 origin master表示。 获取了之后呢?

creatint@wds MINGW64 /e/Html5 (master)
$ git merge origin/master
Already up-to-date.
creatint@wds MINGW64 /e/Html5 (master)

起始,可以运行一个命令即可达到相同的效果。

creatint@wds MINGW64 /e/Html5 (master)
$ git pull
...
creatint@wds MINGW64 /e/Html5 (master)

上面的命令是获取远程仓库改变内容,然后直接执行merge.

原文链接:Git小记

Git小记的更多相关文章

  1. git --help出来的命令 + eclipse里用git小记

    用法:git [--version] [--help] [-C <path>] [-c name=value]           [--exec-path[=<path>]] ...

  2. Git 小记

    感觉用github管理自己平时的一些代码挺方便的,尤其还有各种统计,作为一个码农,就有一种每日签到.累计签到统计的感觉.用github,学习git自然是不可避免的,原先只是用几个 git clone  ...

  3. Git 的 WindowsXP安装

    文章1: http://blog.sina.com.cn/s/blog_5063e4c80100sqzq.html 一.安装必要客户端 1. TortoiseGit http://tortoisegi ...

  4. git stash错误小记

    git出错小记 想要push代码,我们经常这样做. 1.查看状态 git status 2.隐藏本地编辑的新内容 git stash 3.拉远程的代码 git pull 这一步操作有的时候会报错,没有 ...

  5. Git使用小记

    刚刚简答的完成了pureblog,想着先上传导Github上去,等着以后有时间了在完善其功能,所以使用Git上传导Github代码仓库上去,这里简答的记录以下使用小计. 我们首先下载Git,我们使用用 ...

  6. Mac git提交步骤小记

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 19.0px; font: 13.0px "PingFang SC"; c ...

  7. git基础使用小记

    一.安装步骤省略二.运行“Git Bash“在打开的窗口中输入:ssh-keygen -t rsa -C "my@gmail.com" 会提示SSH Public Keys存放的位 ...

  8. git仓库构建小记

    1.新建 .git 文件夹 约定的文件目录下,新建 .git 文件夹 mkdir test.git 2.初始化服务端仓库 git init --bare test.git 此时进入 test.git ...

  9. git学习笔记 看廖大神视频小记

    1.创建一个空目录 $ mkdir gittemp $cd gittemp $pwd //x显示当前目录 2.$ git init 把这个目录变成git可以管理的仓库 多的一个隐藏的.git 目录 可 ...

随机推荐

  1. Android——SharedPreferences

    SharedPreferences是一种轻型的Android数据存储方式,用来保存应用的一些常用配置. 实现SharedPreferences存储的步骤如下: 1.根据Context获取SharedP ...

  2. 使select文本框可编辑可选择(jQuery插件)

    最近做项目中用到了这个插件,正好分享下. 1.  需要用的js包点击下载,在项目中引入该js. <script src="${pageContext.request.contextPa ...

  3. python之路十五

    CSS position 属性 定义和用法position 属性规定元素的定位类型.说明这个属性定义建立元素布局所用的定位机制.任何元素都可以定位,不过绝对或固定元素会生成一个块级框,而不论该元素本身 ...

  4. 关于datepicker只显示年、月、日的设置

    关键时侯,还得看官方文档.花了半个多小时,找了网上一大堆答复,然后一一验证,90%没有能解决问题. 先给出官方文档的URL: http://bootstrap-datepicker.readthedo ...

  5. Windows Server 2008 双网卡同时上内外网 不能正常使用

    Windows server 2008 32位下,双网卡同时上内外网,并提供VPN服务,遇见的奇怪问题 1.服务器配置 2.网络配置 以太网适配器 内部连接: 连接特定的 DNS 后缀 . . . . ...

  6. mui项目中如何使用原生JavaScript代替jquery来操作dom 转自【B5教程网】:http://www.bcty365.com/content-146-3661-1.html

    最近在用mui写页面,当然了在移动App里引入jq或zepto这些框架,肯定是极不理性的.原生JS挺简单,为何需要jq?jq的成功当时是因为ie6.7.8.9.10.chrome.ff这些浏览器不兼容 ...

  7. SQL转换时间的时分

    SELECT WorkerNo, DutyTime, DATENAME(weekday, DutyTime) AS WeekDay, CycleType, CycleNumber, YnOnDuty, ...

  8. ubuntu14.04下直接修改apache2默认目录导致wordpress样式改变的解决办法

    一开始看到网上有各种各样的解决方法: 第一种是直接将 sites-available目录下的000-default.conf中的下列代码: DocumentRoot /var/www/html 修改为 ...

  9. Microsoft ACE OLEDB 12.0 数据库连接字符串

    Excel 97-2003 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;Extended ...

  10. XML带多属性解析为一个实体类(利用反射)

    最近在对接一个银行的项目,大概就是类似一个钱包的功能,在请求返回的数据时,发现返回的数据标准的XML格式的支付串,格式如下 <kColl id="inputOrig" app ...