使用Git Bash进行Git 操作

使用Git Bash进行Git 操作

查看当前文件副本状态

1
2
3
4
5
6
7
8
9
10
11
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: test.txt

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

将文件添加至暂存区

1
2
$ git add test.txt
# 如果没有任何提示,当前命令操作成功

将文件从暂存区中移除

1
2
3
$ git reset head test.txt
Unstaged changes after reset:
M test.txt

恢复文件至最近一个版本的状态,并放弃更改

1
$ git checkout test.txt

将暂存区文件提交至版本库

1
2
3
4
5
6
7
8
$ git commit -m '修改test文件'
[develop 5902113] 修改test文件
1 file changed, 3 insertions(+), 1 deletion(-)
# commit命令只会提交暂存区的文件,未添加进暂存区的文件不会被commit命令提交
# ###########可以看到,提交后返回一些信息
# 当前是在哪个分支(develop)提交的
# 本次提交的完整 SHA-1 # 校验和是什么(5902113)
# 以及在本次提交中,有多少文件修订过,多少行添改和删改过。

跳过暂存,直接将工作副本提交至版本库

1
2
3
4
5
6
7
$ git commit -a -m 'added new benchmarks'
[master 83e38c7] added new benchmarks
1 files changed, 5 insertions(+)

# 似乎不会提交未纳入版本控制的文件
# eg:新建了一个foo.txt,没有添加进暂存区
# 那么commit -a将不会为foo.txt创建版本,也不会提交此文件

创建文件夹和文件

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建文件夹
$ mkdir .note
# 在桌面GUI下,无法创建以“.”开头的文件夹和文件

# 创建文件
$ touch .gitignore

# 查看文件
$ cat .gitignore
# floders
.vs/

# Git Bash下运行的命令有些与Linux命令名称一致,有些与Windows命令名称一致

文本比较

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git diff
diff --git a/foo.txt b/foo.txt
index 2974b83..e8224ee 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1,3 @@
-add line 1
\ No newline at end of file
+----foo.txt
+add line 1
+add line 2
\ No newline at end of file

删除文件并移除版本控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git rm foo.txt
rm 'foo.txt'

$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 3 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

deleted: foo.txt

$ git commit -m '删除foo.txt'
[develop fb06b92] 删除foo.txt
1 file changed, 3 deletions(-)
delete mode 100644 foo.txt

移除版本控制但不删除文件

1
2
3
4
5
6
7
8
$ git rm --cached bar.txt
rm 'bar.txt'

$ git commit -a -m '移除bar.txt版本控制'

# 移除类型文件/目录版本控制
$ git rm log/\*.log

文件重命名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ dir
bar.txt test.txt

$ git mv test.txt flus.txt

$ dir
bar.txt flus.txt

$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 6 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

renamed: test.txt -> flus.txt

#其实,运行 git mv 就相当于运行了下面三条命令:
$ mv README.txt README
$ git rm README.txt
$ git add README

使用Git Bash进行Git 操作
https://jacksiongt.github.io/2020/01/11/git/
作者
Jacksion
发布于
2020年1月11日
许可协议