简述

Git详解 - Git基本操作

前情提示

系统:

一说

  • 同步更新最新版、完整版请移步PUSDN Powered By PUSDN - 平行宇宙软件开发者网www.pusdn.com ,转载请标明出处!

  • 部分截图、链接等因过期、更换域名、MD语法等可能不显示,可联系反馈(备注好博文地址),谢谢❤

  • 带有#号、删除线、不操作、不执行字样的为提示或者备份bash,实际不执行

前情提示

在Windows10下,Git2.24.1版本演示;

初始化仓库

使用git init进行仓库初始化。

实操及结果展示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial
$ cd c:/

geek@DESKTOP-G8R4IFF MINGW64 /c
$ mkdir -p git-tutorial/git-repo-001

geek@DESKTOP-G8R4IFF MINGW64 /c
$ cd git-tutorial/git-repo-001/

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001
$ git init
Initialized empty Git repository in C:/git-tutorial/git-repo-001/.git/

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$

在这里插入图片描述

初始化init完成后,会在当前目录生成.git隐藏目录。

查看仓库状态

git status命令查看仓库状态。

实操:

1
2
3
4
5
6
7
8
9
10
11
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git status
On branch master

No commits yet

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

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$

此时显示没有任何提交和改变。我们来新建一个文件a.txt随便在里面写点东西,然后再次git status,显示如下;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git status
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt

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

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$

显示有untrack未跟踪的文件,并且提示使用git add进行跟踪。

向暂存区添加文件

使用git add命令,向暂存区添加文件。

刚才新建的a.txt文件,显示是untracked file ,使用git add将其添加到暂存区(stage)中。暂存区是提交前的一个临时区域。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git add a.txt
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git status
On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: a.txt


geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$

此时再次git status,将会显示changes to be commit 需要提交的更改。

提交到本地仓库

使用git commit,提交更改到本地仓库,保存仓库的历史记录。git commit -m "本次提交的描述信息",建议每次都要描述好信息。

下面我们进行一次提交:

1
2
3
4
5
6
7
8
9
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git commit -m "第一次提交,添加了a.txt"
[master (root-commit) db17862] 第一次提交,添加了a.txt
1 file changed, 1 insertion(+)
create mode 100644 a.txt

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$

然后,查看状态:

1
2
3
4
5
6
7
8
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git status
On branch master
nothing to commit, working tree clean

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$

查看仓库提交日志

使用git log命令查看详细日志记录,谁,什么时候,提交了什么。

下面来看下,刚才的提交是否被记录到日志:

1
2
3
4
5
6
7
8
9
10
11
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git log
commit db17862da78b39cd5427812680bf0df19b981c55 (HEAD -> master)
Author: Gitee_JaneYork <janeyork1314@163.com>
Date: Tue May 19 15:00:59 2020 +0800

第一次提交,添加了a.txt

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$

上面 内容有 hash值,提交人author,提交时间date。

如果只想查看日志的第一行简短显示,可以使用git log --pretty=short。(等号左右没有空格哦)

只显示指定目录、文件日志:

1
git log a.txt

显示改动的不同

1
2
git log -p
git log -p a.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git log -p a.txt
commit db17862da78b39cd5427812680bf0df19b981c55 (HEAD -> master)
Author: Gitee_JaneYork <janeyork1314@163.com>
Date: Tue May 19 15:00:59 2020 +0800

第一次提交,添加了a.txt

diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/a.txt
@@ -0,0 +1 @@
+1

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$

查看更改前后的不同

使用git diff命令,可以查看工作区,暂存区,最新提交之间的区别。

接下来,随便编写下a.txt,写一些内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git diff
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
diff --git a/a.txt b/a.txt
index d00491f..1191247 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
1
+2


然后我们git add a.txt添加到暂存区。

查看工作区与最新提交的不同

使用git diff HEAD来查看。

1
2
3
4
5
6
7
8
9
10
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git diff HEAD
diff --git a/a.txt b/a.txt
index d00491f..1191247 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
1
+2

然后我们进行git commit -m "第二次提交",之后再查看日志git log

分支的操作

显示分支一览

1
2
3
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git branch
* master

显示只有一个master分支,星号代表当前所在分支。

创建、切换分支

1
2
3
git branch feature-A
git checkout feature-A
或者执行:git checkout -b feature-A

执行过程如下:

1
2
3
4
5
6
7
8
9
10
11
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git branch feature-A

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git branch
feature-A
* master

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git checkout feature-A
Switched to branch 'feature-A'

下面我们修改一下文件,并提交。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (feature-A)
$ vim a.txt

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (feature-A)
$ git add a.txt
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (feature-A)
$ git commit -m "add new branch"
[feature-A 1f757f2] add new branch
1 file changed, 1 insertion(+)

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (feature-A)
$ git status
On branch feature-A
nothing to commit, working tree clean
于是这一行就被添加到了分支feature-A中。

下面,再次切回到master分支中,看文件是否改变或者影响。

1
2
3
4
5
6
7
8
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (feature-A)
$ git checkout master
Switched to branch 'master'

geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ cat a.txt
1
2

可以发现,并不影响,而且可以多个分支同时进行多个功能开发。

git merge合并分支

假设我们feature-A开发完成,想要合并到master。

1
2
3
4
5
geek@DESKTOP-G8R4IFF MINGW64 /c/git-tutorial/git-repo-001 (master)
$ git merge --no-ff feature-A
Merge made by the 'recursive' strategy.
a.txt | 1 +
1 file changed, 1 insertion(+)

以图表形式查看分支

1
git log --graph

更改提交的操作

git commit –amend修改提交信息

1
2
3
git commit -a
git commit -am "message"
git commit -a -m "message"

推送到远程仓库

1
2
3
4
5
6
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://git.shidongvr.com/banlele/test.git
git push -u origin master

推送master以外分支

1
2
git checkout -b feature-D
git push -u origin feature-D

从远程仓库获取

获取远程仓库

1
2
3
git clone https://git.shidongvr.com/banlele/test.git
默认是master分支。
git branch -a 可以查看本次分支和远程分支信息

获取远程其他分支

1
2
git checkout -b feature-D origin/feature-D
-b本地分支名称

获取最新远程仓库分支

1
git pull origin feature-D