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. java从基础知识(十)java多线程(下)

    首先介绍可见性.原子性.有序性.重排序这几个概念 原子性:即一个操作或多个操作要么全部执行并且执行的过程不会被任何因素打断,要么都不执行. 可见性:一个线程对共享变量值的修改,能够及时地被其它线程看到 ...

  2. python调用其他程序或脚本方法(转)

    python运行(调用)其他程序或脚本 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地 ...

  3. inline-block和float

    Inline-block: 1.使块元素在一行显示 2.使内联支持宽高 3.换行被解析 4.不设置宽度,宽度由内容撑开 5.在IE6/7下不支持块标签 Float: 1.使块元素在一行显示 2.使内联 ...

  4. [转]SpringMVC Controller介绍及常用注解

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  5. python模块引用问题(比较杂乱,懒得整理)

    1 在stackoverflows摘抄 If the import module in the same dir, use e.g: from . import core If the import ...

  6. optparse

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...

  7. Nagios

    什么是Nagios? Nagios是一款用于系统和网络监控的应用程序.它可以在你设定的条件下对主机和服务进行监控, 在状态变差和变好的时候给出告警信息. Nagios更进一步的特征包括: 1. 监控网 ...

  8. item2快捷键

    整理使用 iTerm 2 过程中得常用快捷键,Mac 原来自带的终端工具 Terminal 不好用是出了名的,虽然最近几个版本苹果稍微做了些优化,功能上,可用性方面增强不少,无奈有个更好用的 Iter ...

  9. node的核心模块path

    //导入模块path var path=require("path"); //path.basename :输出文件名+后缀 //console.log(path.basename ...

  10. JSONArray的应用

    从json数组中得到相应java数组,如果要获取java数组中的元素,只需要遍历该数组. /** * 从json数组中得到相应java数组 * JSONArray下的toArray()方法的使用 * ...