Git Core Lecture

1、Git 简介

官方介绍:Git is a fast distributed revision control system (Git 是一个快速的分布式版本控制系统)

2、Git Core Command

2.1 git init

git 工程初始化,会在工作区 (working directory) 根目录中创建.git 目录

# 创建目录
$ mkdir git-init
$ cd git-init

# git 目录初始化
$ git init

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: 	git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: 	git branch -m <name>
Initialized empty Git repository in /core-command/git-init/.git/

说明:

  • git init 初始化的集成分支名为:master,可以通过如下命令修改
# 修改默认分支名
$ git config --global init.defaultBranch main

# 再执行初始化时,不再有如上提示
$ git init
Initialized empty Git repository in /temp/.git/
  • 命令行直接修改分支名称,(master -> main)
$ git branch -m main
  • 查看配置修改结果,通过 git config -l 命令
$ git config -l

------
init.defaultbranch=main
------

2.2 git status

显示工作区的状态,可多次使用

刚初始化的工作区是没有可提交的内容

$ git status

No commits yet

nothing to commit (create/copy files and use "git add" to track)

2.3 git add

将文件内容添加到索引中(暂存区)

该命令是在工作区中找到的当前内容更新到索引(暂存区),为下一次提交准备内容。
示例:

# 创建一个文件
$ echo '# Hello Git' > hello-git.md

# 查看工作区状态
$ git status
On branch main

No commits yet

# 未追踪的文件
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	hello-git.md

nothing added to commit but untracked files present (use "git add" to track)

# 将hello-git.md文件添加到索引(暂存区)
$ git add hello-git.md

# 查看工作区状态
$ git status
On branch main

No commits yet
# 将去提交的修改
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   hello-git.md

2.4 git commit

记录仓库的修改,即将索引(暂存区)的内容提交到对象仓库

提交信息包含索引当前的内容和描述变化的给定日志信息。
示例:

# 修改内容提交到对象库
$ git commit -m 'add hello-git.md file'
[main (root-commit) f3de15d] add hello-git.md file
 1 file changed, 1 insertion(+)
 create mode 100644 hello-git.md

$ git status
On branch main
nothing to commit, working tree clean

2.5 git diff

用于对比内容差异,包括工作区和索引,索引和对象库,以及分支之间等其他可对比的内容

示例:

# 修改 hello-git.md 文件
$ cat >> hello-git.md
## Git Core Command
### git init

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   hello-git.md

no changes added to commit (use "git add" and/or "git commit -a")

$ git diff
# 比较文件,a 是比较源文件,b 是比较目标文件
diff --git a/hello-git.md b/hello-git.md
index ab690e8..db1f38b 100644
--- a/hello-git.md
+++ b/hello-git.md
@@ -1 +1,3 @@
 # Hello Git
+## Git Core Command
+### git init

# 再次提交
# 将 git add 和 git commit 命令合并使用
$ git commit -a -m 'modify hello-git.md file'

说明

  • git 的工作空间分为三种,工作区(工作目录)、索引(暂存区)、对象库
  • git 的工作流:工作区修改的有效内容 添加到 索引 提交 到对象库

2.6 git restore

恢复工作区、索引中已修改的文件,默认恢复工作区

示例:

  • 恢复工作区
# 修改hello-git.md 文件
$ cat >> hello-git.md
git init is used to initial git working tree(directory).

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   hello-git.md

no changes added to commit (use "git add" and/or "git commit -a")

# 撤回修改(方式一): 将修改的内容手动撤回,如果改动太多,手动改成本太高,非常不推荐
$ vim hello-git.md
......

# 撤回修改(方式二): 使用 git restore 命令恢复修改的内容
$ git restore hello-git.md
# 查看 hello-git.md 内容
$ cat hello-git.md
# Hello Git
## Git Core Command
### git init
  • 恢复索引
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值