Git小记

Git简~介

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

你的计算机上是否有Git?

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

仓库怎么创建?

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

[email protected] MINGW64 /e/Weixin
$ git init;
Initialized empty Git repository in E:/Weixin/.git/
[email protected] MINGW64 /e/Weixin (master)

文件还需要添加?

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

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

[email protected] 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)
[email protected] MINGW64 /e/Weixin (master)

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

怎么提交修改?

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

[email protected] MINGW64 /e/Weixin (master)
$ git add Home;

查看状态:

[email protected] 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

[email protected] MINGW64 /e/Weixin (master)

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

[email protected] MINGW64 /e/Weixin (master)
$ git commit -m "修改了标题";
On branch master
nothing to commit, working directory clean
[email protected] MINGW64 /e/Weixin (master)

版本怎样穿梭?

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

[email protected] MINGW64 /e/Weixin (master)
$ git status;
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   test.php

[email protected] 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

[email protected] 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‘;
[email protected] MINGW64 /e/Weixin (master)

提交日志是什么?

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

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

可以使之显示在一行

$ git log --pretty=oneline
52fc500fde16f4b9911e87e42d314a98af718a57 修改了test.php文件
578ebf64a3e451a89f144eff37727d5569834158 增加了test.php文件
[email protected] 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文件
[email protected] 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.‘;
[email protected] MINGW64 /e/Weixin (master)

撤销功能怎么实现?

$ git checkout test.php

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

分支管理怎么弄?

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

[email protected] MINGW64 /e/Weixin (master)

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

创建分支branch

$ git checkout -b dev
Switched to a new branch ‘dev‘
[email protected] MINGW64 /e/Weixin (dev)

git checkout -b dev的等于:

$ git branch dev
$ git checkout dev

查看当前分支:

$ git branch
* dev
  master
[email protected] MINGW64 /e/Weixin (master)

合并分支

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

[email protected] MINGW64 /e/Weixin <class="variable">(dev)
$ git checkout master
Switched to branch ‘master‘
Your branch is up-to-date with ‘origin/master‘.
[email protected] MINGW64 /e/Weixin <class="variable">(master)

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

[email protected] MINGW64 /e/Html5 (master)
$ git merge dev
Already up-to-date.
[email protected] MINGW64 /e/Html5 (master)

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

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

这里以开源中国Git为例

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

[email protected] MINGW64 /e/Html5
$ git init;
Initialized empty Git repository in E:/Html5/.git/

然后,添加远程仓库

[email protected] MINGW64 /e/Html5 (master)
$ git remote add origin https://git.oschina.net/yotaku/Html5.git;

没有反应?看看下面:

[email protected] 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)

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

[email protected] 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表示。 获取了之后呢?

[email protected] MINGW64 /e/Html5 (master)
$ git merge origin/master
Already up-to-date.
[email protected] MINGW64 /e/Html5 (master)

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

[email protected] MINGW64 /e/Html5 (master)
$ git pull
...
[email protected] MINGW64 /e/Html5 (master)

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

原文链接:Git小记

时间: 2024-08-14 00:40:01

Git小记的相关文章

Git 的 WindowsXP安装

文章1: http://blog.sina.com.cn/s/blog_5063e4c80100sqzq.html 一.安装必要客户端 1. TortoiseGit http://tortoisegit.googlecode.com/files/TortoiseGit-1.0.2.0-32bit.msi 下载安装后,重启系统. 2. 安装 msysgit a. 从 http://msysgit.googlecode.com/files/Git-1.6.2.1-preview20090322.ex

git 学习小记之记住https方式推送密码

昨天刚刚学了点git基础操作,但是不幸的是[email protected]给出公告说尽量使用 https 进行操作.可是在用 https 进行 push 时,都需要输入帐号和密码. 各种百度谷歌之后在[email protected]官网找到了解决方法<https方式使用[email protected]设置密码的方式>文中给出了几个方法,并且都非常简单. 关于 cache 缓存方式,我不太喜欢,因为要设置时间,而且会过期.而 store 相应的非常方便,设置全局后,方便多个库使用.当然如果

git入门小记

git提交三步走 git add git commit git push 查看状态 git status 创建git文件初始化一个文件夹 git  init [--bare]#括号内可选,为接收提交 git log #查看提交记录 git clone   ssh:// 下载project git checkout   -branch  dfjjkl   创建分支 git fetch 有类似功效 git pull  origin master   更新 git mergetool git sour

git 学习小记之图形化界面客户端

习惯了 Windows 的用户,一直不喜欢用类似命令行的东西来操作,当然我也不是不喜欢,只是操作太慢了.也许 Linux 大神在命令行的帮助下,办事效率翻倍,那也是非常常见的事情..当然我不是大神,所以还是得选择一个合适的工具才行. 其实相信大家也是一样,简单学习之后,直接就上工具了..我找到一篇不错的文章<Git图形化界面客户端大汇总>里面介绍了 11 款 git 工具,而且有截图和简单描述,貌似作者按喜好排序的. 我下载了前三个测试了下.TortoiseGit 差不多直接上手,因为我用的

git 用法详解小记

1.安装 sudo apt-get install git    git config --global user.name "yourname"       git config --global user.email "137505******@163.com"2.初始化:①创建一个目录:mkdir reangittoday②git init ③->git add filename -> git commit -m "添加说明文字"

git服务器的建立——Git折腾小记

转自:http://blog.csdn.net/xsl1990/article/details/25486211 如果你能看到一些sshd相关的进程信息,则说明你已经有这个服务了,否则(或者你想更新的话),使用下面的命令安装openssh [plain] view plaincopy sudo apt-get install openssh-server openssh-client 然后,安装git“服务器” [plain] view plaincopy sudo apt-get instal

git使用小记

1.安装node ,新版的自带了npm和bower https://nodejs.org/dist/v4.2.6/node-v4.2.6-x64.msi 2.安装git https://github-cloud.s3.amazonaws.com/releases/23216272/d855ed4a-c9d4-11e5-9991-fd9275240fd9.exe?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAISTNZFOVBIJMK3

Git命令行技能小记

查看Git分支: git checkout -b xxx origin/xxx 把远程的xxx分支拉到本地,取名xxx git branch -vv 查看本地分支详细信息 git branch -rv 查看远端分支详细信息 git fetch origin xxx 拉取远程分支xxx的代码到本地,但不会合并 git merge xxx 把xxx分支的代码合并到当前分支 git pull origin abc 把远端分支abc的代码拉到本地,并合并代码到当前的分支(上面两个操作fetch,merg

git版本库使用小记

1.从master上做分支,在本人的分支上开发,提交时先提交本地工作副本,再推送大到远程自己的分支上,最后提merge request,审核完后merge到master. 2.提过一次merge request之后,在审核者处理之前,再往自己分支上提交后,就不用再提merge request了,merge时会一次性全部处理. 3.拉取与获取的区别 git  pull     从远程拉取最新版本 到本地  自动合并 merge git  fetch   从远程获取最新版本 到本地   不会自动合并