GitHub入门与实践 二

2019-10-29  本文已影响0人  闲与行

#第四章 通过实践操作 学习Git

4.1 基本操作

$ mkdir git-tutorial
$ cd git-tutorial
$ git init
Initialized empty Git repository in /Users/hirocaster/github/github-book
/git-tutorial/.git/

执行了 git init命令的目录下就会生成 .git 目录。这个 .git 目录里存储着管理当前目录内容所需的仓库数据。

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

稍加改变后

$ touch README.md
$ git status
# On branch master
#
# Initial commit
## Untracked files:# (use "git add <file>..." to include in what will
be committed)#
# README.md
nothing added to commit but untracked files present (use "git add" to
track)
$ git add README.md
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: README.md
#

可以明显的看到状态的变化

$ git commit -m "First commit"
[master (root-commit) 9f129ba] First commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md

-m 参数后的 "First commit"称作提交信息,是对这个提交的
概述。

-记述详细提交信息

● 第一行:用一行文字简述提交的更改内容
● 第二行:空行
● 第三行以后:记述更改的原因和详细内容

$ git status
# On branch master
nothing to commit, working directory clean
$ git log
commit 9f129bae19b2c82fb4e98cde5890e52a6c546922
Author: hirocaster <hohtsuka@gmail.com>
Date: Sun May 5 16:06:49 2013 +0900
First commit

不妨养成这样一个好习惯:在执行 git commit命令之前先执行git diff HEAD命令,查看本次提交与上次提交之间有什么差别,等确认完毕后再进行提交。这里的 HEAD 是指向当前分支中最新一次提交的指针。

4.2 分支的操作

通过灵活运用分支,可以让多人同时高效地进行并行开发。

$ git branch
* master

git checkout -b——创建、切换分支
切换到 feature-A 分支并进行提交
git branch——显示分支一览表

$ git checkout -
Switched to branch 'feature-A'

特性分支顾名思义,是集中实现单一特性(主题),除此之外不进
行任何作业的分支。

$ git merge --no-ff feature-A

4.3 更改提交的操作

  1. 要让仓库的 HEAD、暂存区、当前工作树回溯到指定状态,需要用
    到 git rest --hard命令。
$ git checkout -b fix-B
Switched to a new branch 'fix-B'

$ git rebase -i HEAD~2

4.4 推送至远程仓库

$ git checkout -b feature-D

$ git push -u origin feature-D

4.5 从远程仓库获取

4.6 帮助大家深入理解 Git 的资料

上一篇 下一篇

猜你喜欢

热点阅读