这里是原网站:https://try.github.io/levels/1/challenges/1

这篇博文就当是笔记+翻译吧。

几个名词相关

changes:变更

repository:仓库

staging area:缓存区

local:本地

remote:远程

git的工作流程

本地文件变更>add>缓存区(staging area)>commit>本地仓库>push>远程仓库(GitHub)

开始

在git bash中进入工作目录,比如“octobox”

1.初始化一个git repository

git init

然后“octobox”目录下就会有一个空的repository保存在/.git/,这个repository是由Git操作的隐藏目录

2.看一下当前project的状态

git status

因为项目是刚刚创建的,所以提示没有需要提交的内容:

# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

3.在“octobox”中新建一个txt文档“octocat.txt”,再查看一下当前project的状态:

git status

此时会显示octocat.txt是“untracked files”,这是因为Git看到了这是一个文件:

# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# octocat.txt
nothing added to commit but untracked files present (use "git add" to track)

4.要告知Git开始追踪octocat.txt文件中的改动,我们首先要把该文件添加到staging area中:

git add octocat.txt

5.现在Git已经开始追踪我们的octocat.txt文件了,现在让我们看一下project的状态:

git status

然后我们可以看到Git如何展示“待提交的变更(changes to be committed)”,这里列举的文件都是Staging area中的文件,它们还没有保存到我们的repository中:

# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: octocat.txt
#

6.要保存我们在Staging area中的变更到repository,我们需要提交,一般会附加一条描述变更的信息:

git commit -m "Add cute octocat story"

接下来就会看到提交成功的信息 :

[master (root-commit) 20b5ccd] Add cute octocat story
file changed, insertion(+)
create mode octocat.txt

7.如何提交大量的变更呢?在当前目录“octobox”下:(1)新建一些txt文件;(2)新建一个目录,在新建目录下也放一些txt文件,然后添加到staging area:

git add '*.txt'

8.然后提交到repository:

git commit -m 'Add all the octocat txt files'

此时就可以看到我们刚刚新建的那些全部的txt都被提交到repository中了:

[master 3852b4d] Add all the octocat txt files
files changed, insertions(+)
create mode blue_octocat.txt
create mode octofamily/baby_octocat.txt
create mode octofamily/momma_octocat.txt
create mode red_octocat.txt

9.我们已经做了一些提交,那么如何浏览我们曾经提交了哪些变更呢?

git log

此时就可以看到我们全部的历史变更了:

commit 3852b4db1634463d0bb4d267edb7b3f9cd02ace1
Author: Try Git <try_git@github.com>
Date: Sat Oct :: - Add all the octocat txt files commit b652edfd888cd3d5e7fcb857d0dabc5a0fcb5e28
Author: Try Git <try_git@github.com>
Date: Sat Oct :: - Added cute octocat story

10.到目前位置的变更、提交一系列操作,我们还都是在本地机器上进行的,怎么保存到网上(GitHub)呢?

首先在GitHub上新建一个空的repository,然后记下该repository的url,比如:https://github.com/try-git/try_git.git

要想将我们本地的repository保存到GitHub服务器,我们要添加一个远程仓库(remote repository),git remote add这个命令需要提供远程仓库的名字(自定义)和repository的url(你在GitHub上创建的那个repository):

git remote add origin https://github.com/try-git/try_git.git

11.把我们的变更push到远程仓库 origin (on GitHub):

我们远程仓库名字是“origin”,本地默认分支(default branch)的名字是“master”,-u 是告诉Git记住后边的参数,这样下次我们就可以直接使用git push进行push了:

git push -u origin master

12.假如过了一段时间,有其他人从我们的GitHub上pull了我们的repository、并进行了提交、push,比如添加了一些文件,现在我们想把最新的repository下载(pull)到我们本地电脑中:

git pull origin master

然后我们可以看到pull的版本:

Updating 3852b4d..3e70b0f
Fast-forward
yellow_octocat.txt | +
file changed, insertion(+)
create mode yellow_octocat.txt

13.pull之后,我们可以查看现在的repository和我们最后一次提交时的状态有哪些不同,其中HEAD是一个指针,指向我们最近的一次提交commit:

git diff HEAD

此时可以看到详细的不同:

diff --git a/octocat.txt b/octocat.txt
index 7d8d808..e725ef6
--- a/octocat.txt
+++ b/octocat.txt
@@ - + @@
-A Tale of Two Octocats
+[mA Tale of Two Octocats and an Octodog

14.diff命令还可以查看已经staged的文件(在staging area),staged files就是我们告诉git已经准备好提交的文件,假如我们变更了octofamily/octodog.txt,我们把它add到stage中:

git add octofamily/octodog.txt

15.然后看一下staged文件有哪些difference:

git diff --staged

然后看到下边的信息:

diff --git a/octofamily/octodog.txt b/octofamily/octodog.txt
new file mode
index ..cfbc74a
--- /dev/null
+++ b/octofamily/octodog.txt
@@ -, + @@
+[mwoof

16.我们不想提交octodog.txt了,要把octodog.txt从staging area中删掉(unstaged):

git reset octofamily/octodog.txt

17.git reset帮我们把octodog.txt从staging area中unstaged了,但是可以注意到它其实还是存在的,只是不在staging area中了,如果时间倒流,然octodog.txt回到octodog.txt出现之前(这个例子中也就是消失),那是不是很好。文件可以改回到我们的最后一次提交的状态,比如我们更改了octocat.txt文件,但是想让它回到最后一次提交的状态:

git checkout -- octocat.txt

18.当开发者开发一个新的feature(项目中的功能)或者修复bug的时候,他们通常都会创建一个repository的拷贝(分支,branch),以便让他们隔离提交,以至于不会影响真正的产品,然后当他们完成之后,就可以合并他们的branch和主要的master branch。

我们想清除一些txt文件,先新建一个branch clean_up:

git branch clean_up

19.现在如果输入git branch,就会看到两个本地分支,一个主要分支master,一个新建分支clean_up:

git branch

显示:

  clean_up
* master

*表示当前工作分支,我们要切换分支到clean_up:

git checkout clean_up

显示切换成功:

Switched to branch 'clean_up'

20.现在在clean_up分支上了,可以用git rm完成刚才想做的清除任务了(git rm不仅仅从硬盘上清除文件,还会把removal放到staging area中)

git rm '*.txt'

清除结果:

rm 'blue_octocat.txt'
rm 'octocat.txt'
rm 'octofamily/baby_octocat.txt'
rm 'octofamily/momma_octocat.txt'
rm 'red_octocat.txt'

21.可以用git status查看一下当前stage中的changes:

git status

显示:

# On branch clean_up
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: blue_octocat.txt
# deleted: octocat.txt
# deleted: octofamily/baby_octocat.txt
# deleted: octofamily/momma_octocat.txt
# deleted: red_octocat.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# octofamily/

然后提交刚刚的变更:

git commit -m "Remove all the cats"

提交结果:

[clean_up 63540fe] Remove all the cats
files changed, deletions(-)
delete mode blue_octocat.txt
delete mode octocat.txt
delete mode octofamily/baby_octocat.txt
delete mode octofamily/momma_octocat.txt
delete mode red_octocat.txt

22.回到master分支:

git checkout master

23.合并clean_up到master:

git merge clean_up

显示:

Updating 3852b4d..ec6888b
Fast-forward
blue_octocat.txt | -
octocat.txt | -
octofamily/baby_octocat.txt | -
octofamily/momma_octocat.txt | -
red_octocat.txt | -
files changed, deletions(-)
delete mode blue_octocat.txt
delete mode octocat.txt
delete mode octofamily/baby_octocat.txt
delete mode octofamily/momma_octocat.txt
delete mode red_octocat.txt

24.既然已经完成了清除工作,那么就不需要clean_up这个分支了,可以卸磨杀驴了:

git branch -d clean_up

25.前几步都是在本地玩的,现在可以把成品放到remote repository了:

git push

ZH奶酪:Git简明教程的更多相关文章

  1. Git简明教程一、基本概念

    文本是写给新手的Git入门教程.本文的目的是让新手能够快速了解并开始使用Git,因此只会介绍最基本.同时也是最核心的知识.其中包括使用Git的基本步骤和Git中最常用的命令,以及如何使用GitHub托 ...

  2. Git简明教程二、开始进行版本管理

    上一篇介绍了Git中的一些基本概念.本篇来实际看一看如何通过几个常用命令来快速上手Git,完成版本管理的日常操作(核心操作). 0. 准备工作 安装Git后,请先在你的电脑上新建或选择一个目录作为测试 ...

  3. 【笔记】Git简明教程

    前言 Git这个东西我曾经有学过,但学的内容太多了,有点懵,不太理解,磕磕碰碰的,走了不少弯路.不过最近我在B站上发现了一个讲的很好的教程:<表严肃讲Git>.因此,我决定用文字的方式分享 ...

  4. Git 简明教程

    其他Git资料: Git Community Book 中文版

  5. Git简明教程

    http://www.jianshu.com/p/16ad0722e4cc http://www.jianshu.com/p/f7ec8310ccd2

  6. Git简易教程-安装及简单使用

    Git是一种版本控制器,在项目开发中可以用来托管代码 一.下载安装Git 1. Git下载 地址:https://git-scm.com/download/win 2. 安装 检验是否安装成功 电脑桌 ...

  7. GIT使用教程与基本原理

    转自:http://blog.csdn.net/wengpingbo/article/details/8985132 说明:该教程全部图片都来自于<pro git>.以下所有的操作,除非特 ...

  8. appium简明教程

    appium简明教程 什么是appium? 下面这段介绍来自于appium的官网. Appium is an open-source tool you can use to automate mobi ...

  9. git使用教程指南

    前言  Git是一个开源的分布式版本控制系统.其核心就在于版本控制.  在实际编码过程中,我们往往会忘记上次对文件的修改内容.若是刚刚修改的还好说,撤销操作即可.但若这是你昨天做的修改并关闭了IDE呢 ...

随机推荐

  1. Windows Phone本地数据库(SQLCE):7、Database mapping(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第七篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  2. ECSHOP商城网站建设之自定义调用广告方法(二)

    原文地址:http://www.cnblogs.com/zgzy/p/3598991.html 使用ecshop进行商城网站建设时,ecshop默认的很多功能对于我们个性化设计之后不太使用.今天我们主 ...

  3. 【elasticsearch】关于elasticSearch的基础概念了解【转载】

    转载原文:https://www.cnblogs.com/chenmc/p/9516100.html 该作者本系列文章,写的很详尽 ================================== ...

  4. linux 统计文件夹空间

     du -sh * | sort -nr 

  5. Eclipse给安卓应用签名

  6. Kubernetes中StatefulSet介绍

    StatefulSet 是Kubernetes1.9版本中稳定的特性,本文使用的环境为 Kubernetes 1.11.如何搭建环境可以参考kubeadm安装kubernetes V1.11.1 集群 ...

  7. C#零基础入门03:打老鼠初级

    一:在源码管理器中打开VS 注意,下文这样的打开方式是在 TFS 的源码服务器中打开解决方案.如果我们使用 SVN,则直接在硬盘上打开解决方案就可以了. 打开VS: 然后按上图中的步骤1和2(双击2处 ...

  8. cross validation笔记

    preface:做实验少不了交叉验证,平时常用from sklearn.cross_validation import train_test_split,用train_test_split()函数将数 ...

  9. Windows Server 2012上安装.NET Framework 3.5(不需要安装光盘)

    因为在windows2012里,安装数据库,IIS部分组件都需要.NET3.5,而默认windows2012安装时,并不会把此组件复制到电脑里 导致,后期要安装.NET3.5还需要安装盘.但是,很多人 ...

  10. Grizzly HTTP CoDec ThreadCache 浅析

    Grizzly 的 HTTP CoDec 实现方法更 Netty 的 CoDec 完全不同, 他们思想上的差异主要在于: 1. 解码方式 Grizzly 使用流式解码, 它的HttpHeader对象内 ...