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. tar 解压bz2报错 Cannot exec: No such file or directory

    tar: bzip2: Cannot exec: No such file or directorytar: Error is not recoverable: exiting now 需要安装bzi ...

  2. 关于robotframework,app,appium的xpath定位问题及常用方法

    关于类似的帖子好像很多,但是没有找到具体能帮我解决问题的办法.还是自己深究了好久才基本知道app上面的xpath定位和web上的不同点: 先放一个图: A,先说说不用xpath的场景,一般是用于存在i ...

  3. git diff 生成patch, git apply patch 打补丁方法说明,以及分支管理的简单操作。

    git diff 简易操作说明 先git log 查看commit ID, 记录你想要打的补丁的ID 比如说: git log commit 4ff35d800fa62123a28b7bda2a04e ...

  4. bash学习

    if 的使用 declare -i x=10 if [ $x -lt 14 ];then echo $x'小于14'; elif [ $x -gt 9 ];then echo $x'大于9'; els ...

  5. JSON中eval与parse的区别

    json的的解析方法 (非原创) json的解析方法共有两种:eval_r() 和 JSON.parse(),使用方法如下: var jsonData = '{"data1":&q ...

  6. hibernate id 策略

    @Id@GeneratedValue(generator = "paymentableGenerator")@GenericGenerator(name = "payme ...

  7. 【转】使用Python matplotlib绘制股票走势图

    转载出处 一.前言 matplotlib[1]是著名的python绘图库,它提供了一整套绘图API,十分适合交互式绘图.本人在工作过程中涉及到股票数据的处理如绘制K线等,因此将matplotlib的使 ...

  8. git: fatal: Not a git repository (or any of the parent directories): .git

    在看书 FlaskWeb开发:基于Python的Web应用开发实战 时,下载完源码后 git clone https://github.com/miguelgrinberg/flasky.git 试着 ...

  9. C++ Bitstream类

    从raknet上剥下来的 比较适用于前后端通讯,可以对BitStream进行二次封装,方便使用. BitStream.h: #ifndef __BITSTREAM_H #define __BITSTR ...

  10. Linux学习记录

    ---恢复内容开始--- linux与unix的关系 linux是借鉴了unix设计思想,也称linux位类unix系统. Linux常用命令 1.命令基本格式 命令[选项][参数] 注意:个别命令不 ...