您的位置:首页 > 其它

Git小记

2016-04-24 14:12 330 查看
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

git提交历史事件线
git提交历史时间线
版本是通过指针指向的,更改版本,其实是修改了指针,而各个版本的记录仍然是存在的,只是看不到罢了。
$ 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
如果你执行了add或commit,之后对文件进行了一些修改,过了一小时却想退回到修改之前的状态呢?使用上面的代码吧!

分支管理怎么弄?

分支(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查看dev与master的不同之处。
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;[/code]没有反应?看看下面: 
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