Git简明教程

简介:

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

几个名词相关

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
1 file changed, 1 insertion(+)
create mode 100644 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
4 files changed, 4 insertions(+)
create mode 100644 blue_octocat.txt
create mode 100644 octofamily/baby_octocat.txt
create mode 100644 octofamily/momma_octocat.txt
create mode 100644 red_octocat.txt
复制代码

 

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

git log

 

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

复制代码
commit 3852b4db1634463d0bb4d267edb7b3f9cd02ace1
Author: Try Git <try_git@github.com>
Date: Sat Oct 10 08:30:00 2020 -0500

Add all the octocat txt files

commit b652edfd888cd3d5e7fcb857d0dabc5a0fcb5e28
Author: Try Git <try_git@github.com>
Date: Sat Oct 10 08:30:00 2020 -0500

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 | 1 +
1 file changed, 1 insertion(+)
create mode 100644 yellow_octocat.txt

 

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

git diff HEAD

 

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

复制代码
diff --git a/octocat.txt b/octocat.txt
index 7d8d808..e725ef6 100644
--- a/octocat.txt
+++ b/octocat.txt
@@ -1 +1 @@
-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 100644
index 0000000..cfbc74a
--- /dev/null
+++ b/octofamily/octodog.txt
@@ -0,0 +1 @@
+[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
5 files changed, 5 deletions(-)
delete mode 100644 blue_octocat.txt
delete mode 100644 octocat.txt
delete mode 100644 octofamily/baby_octocat.txt
delete mode 100644 octofamily/momma_octocat.txt
delete mode 100644 red_octocat.txt
复制代码

 

22.回到master分支:

git checkout master

23.合并clean_up到master:

git merge clean_up

 

显示:

复制代码
Updating 3852b4d..ec6888b
Fast-forward
blue_octocat.txt | 1 -
octocat.txt | 1 -
octofamily/baby_octocat.txt | 1 -
octofamily/momma_octocat.txt | 1 -
red_octocat.txt | 1 -
5 files changed, 5 deletions(-)
delete mode 100644 blue_octocat.txt
delete mode 100644 octocat.txt
delete mode 100644 octofamily/baby_octocat.txt
delete mode 100644 octofamily/momma_octocat.txt
delete mode 100644 red_octocat.txt
复制代码

 

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

git branch -d clean_up

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

git push

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4653810.html,如需转载请自行联系原作者

相关文章
|
16天前
|
开发工具 git
Git教程:深入了解删除分支的命令
【4月更文挑战第3天】
37 0
Git教程:深入了解删除分支的命令
|
1月前
|
存储 Shell Linux
【Shell 命令集合 文件管理】Linux git命令使用教程
【Shell 命令集合 文件管理】Linux git命令使用教程
34 0
|
2月前
|
安全 开发工具 git
git使用教程
git使用教程
47 0
|
6月前
|
开发工具 数据安全/隐私保护 git
百度搜索:蓝易云【Mac 安装homebrew Mac安装Git教程。】
现在,您已成功在Mac上安装了Homebrew和Git。您可以使用Homebrew安装其他软件包,并使用Git进行版本控制和代码管理。 希望这些信息对您有所帮助!如果您有任何其他问题,请随时提问。
123 0
|
6月前
|
自然语言处理 Java Go
项目总监必看:如何利用Git深度统计团队代码贡献?多语言实践教程揭秘!
项目总监必看:如何利用Git深度统计团队代码贡献?多语言实践教程揭秘!
156 0
|
5月前
|
存储 算法 开发工具
Git的入门详细教程
Git的入门详细教程
|
5月前
|
存储 Linux 网络安全
git教程
git教程
107 0
|
1月前
|
程序员 开发工具 git
好程序员Git入门到精通教程
本课程主要通过命令行和idea来介绍Git的安装、仓库创建、工作流、远程仓库、克隆仓库、标签管理和分支管理等Git的主要内容。 另外关于GitHub的使用介绍也有完整的说明和使用,接轨生产环境使用方式。
10 1
好程序员Git入门到精通教程
|
1月前
|
存储 算法 开发工具
|
2月前
|
存储 开发工具 git
Git 教程:解密 .gitignore 文件、合并分支、解决冲突、及 Git 帮助
如果你忘记了命令或命令的选项,你可以使用 Git 帮助。 在命令行中,有几种不同的使用帮助命令的方式: git command -help - 查看特定命令的所有可用选项 git help --all - 查看所有可能的命令 让我们看看不同的命令。
213 3

热门文章

最新文章

相关实验场景

更多