Git 常用命令
以下命令参数仅列出常用部分,想要熟悉其他的可使用 git 命令 --help
查看,或者查找官方文档。
1、创建版本库
git init
初始化本地git仓库(创建新仓库)。
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test
$ mkdir gittest
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test
$ cd gittest/
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest
$ git init
Initialized empty Git repository in E:/work/test/gittest/.git/
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ ls -a
./ ../ .git/
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ cd .git
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest/.git (GIT_DIR!)
$ ls -a
./ ../ config description HEAD hooks/ info/ objects/ refs/
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest/.git (GIT_DIR!)
$ tree .
.
|-- HEAD
|-- config
|-- description
|-- hooks
| | `-- applypatch-msg.sample
| | `-- commit-msg.sample
| | `-- fsmonitor-watchman.sample
| | `-- post-update.sample
| | `-- pre-applypatch.sample
| | `-- pre-commit.sample
| | `-- pre-merge-commit.sample
| | `-- pre-push.sample
| | `-- pre-rebase.sample
| | `-- pre-receive.sample
| | `-- prepare-commit-msg.sample
| `-- update.sample
|-- index
|-- info
| `-- exclude
|-- logs
| | `-- HEAD
| `-- refs
| | `-- heads
| | `-- master
| `-- remotes
| `-- origin
| `-- HEAD
|-- objects
| |-- info
| `-- pack
| |-- pack-49b545fa75e15e2e5fef17c521687333fd75d41a.idx
| `-- pack-49b545fa75e15e2e5fef17c521687333fd75d41a.pack
|-- packed-refs
`-- refs
|-- heads
| `-- master
|-- remotes
| `-- origin
| `-- HEAD
`-- tags
15 directories, 25 files
-
HEAD:这就是我们常说的HEAD指针,它指向了当前分支。
-
config(配置):该文件包含你的仓库配置,比如远程的 url,你的邮箱和用户名等,每次你在控制台使用 git config 都会在此产生影响。
-
description(描述):供 gitweb [github的一种前身] 使用,显示仓库的描述。
-
hooks(钩子)这是一个有趣的特性,Git 提供了一套脚本,可以在每个有意义的 Git 阶段自动运行,这些被称为钩子的脚本可以在提交[commit],变基[rebase]拉取[pull]操作的前后运行,脚本命预示着它的执行时机,如我们可以编写 pre-push 的作为钩子,进行推送代码前的检查。
-
index(暂存区):所有的添加修改删除操作都保存在这个文件中。
-
info(信息):你可以将不想被git管理的文件记录到 .gitignore 文件中,排除文件的意思是不行共享这个文件,例如你不想共享你的 IDE 自定义配置,将其添加到 .gitinore 文件中即可。
-
logs文件夹,保持所有更新的记录,logs文件夹中有两个文件 refs文件夹和HEAD文件。
-
refs文件夹:
- heads文件夹:里面存储的是本地分支的对象,每个对象的文件名就是本地的一个分支名。我们使用git branch查看本地所有分支时,查询出的分支就是heads文件夹下所有文件的名称,这些分支文件中存储的是对应分支下的操作记录。
- remotes文件夹:remotes文件夹里存储的是远程的所有分支对象,每个对象的文件名称就是远程的一个分支名称。这些分支文件中保存了远程仓库对应分支所有操作。
-
HEAD文件:里面保持的是所有操作记录,使用 git reflog 查询的结果就是从HEAD文件里面来的。
-
-
objects文件夹:存放着所有 git 对象,哈希值一共40位,前2位作为文件夹名称,后38位作为对象文件名。
-
refs文件夹:表示一系列的引用。
- heads:表示一系列的本地分支。
- remotes:远程仓库分支。
- tags:标识某些重要节点,版本发布等记录重要版本。
第一次初始化的两种方式:
git init # 初始化本地git仓库
git remote add origin git@github.com:帐号名/仓库名.git # 添加远程版本库
git pull origin master # 拉取远程分支
git clone git@github.com:git帐号名/仓库名.git # 克隆仓库
测试1(初始化本地仓库,关联远程版本库):
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git remote add origin git@gitee.com:public-investment-adviser/testing.git
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git pull origin master
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 10 (delta 0), reused 0 (delta 0), pack-reused 7
Unpacking objects: 100% (10/10), 2.74 KiB | 7.00 KiB/s, done.
From gitee.com:public-investment-adviser/testing
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ ls -a
./ ../ .git/ README.en.md README.md
测试2(克隆远程仓库):
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test
$ rm -rf ./gittest/
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test
$ ls
index.html index.php ModalBox/ phpinfo.php SwiperUpDown_demo-master/
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test
$ git clone git@gitee.com:public-investment-adviser/testing.git gittest
Cloning into 'gittest'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 10 (delta 0), reused 0 (delta 0), pack-reused 7
Receiving objects: 100% (10/10), done.
Resolving deltas: 100% (1/1), done.
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test
$ ls
gittest/
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test
$ cd gittest/
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ ls -a
./ ../ .git/ README.en.md README.md
2、修改和提交
git add
将文件添加到暂存区。常用的就是 git add .
和 git add 文件名
。
git add 文件名 # 将工作区的某个文件添加到暂存区;
git add . # 将当前工作区的所有文件都加入暂存区;
git add -u # 添加所有被tracked(跟踪)文件中被修改或删除的文件信息到暂存区,不处理untracked(取消跟踪)的文件;
git add -A # 添加所有被tracked(跟踪)文件中被修改或删除的文件信息到暂存区,包括untracked(取消跟踪)的文件;
git add -i # 添加所有被tracked(跟踪)文件中被修改或删除的文件信息到暂存区,包括untracked(取消跟踪)的文件进入交互界面模式,按需添加文件到缓存区;
git commit
将暂存区内容提交到本地仓库。
git commit -m "message" # 将暂存区内容提交到本地仓库;
git commit -am "message" # “提交说明” 跳过缓存区操作,直接把工作区内容提交到本地仓库;
git push
将本地的分支版本上传到远程并合并,如果本地分支名与远程分支名相同,则可以省略冒号。
git push 远程主机名 本地分支名:远程分支名
命令测试:
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ echo '1111' >> test.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ ls
README.en.md README.md test.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git add test.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: test.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git commit -m '提交命令测试'
[master 7d49cab] 鎻愪氦鍛戒护娴嬭瘯
1 file changed, 1 insertion(+)
create mode 100644 test.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 6 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 328 bytes | 328.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:public-investment-adviser/testing.git
83d6125..7d49cab master -> master
3、提交记录
git log
查看所有commit记录(SHA-A校验和,作者名称,邮箱,提交时间,提交说明)。
git log -p -次数 # 查看最近多少次的提交记录;
git log --stat # 简略显示每次提交的内容更改;
git log --name-only # 仅显示已修改的文件清单;
git log --name-status # 显示新增,修改,删除的文件清单;
git log --oneline # 让提交记录以精简的一行输出;
git log --graph –all –online # 图形展示分支的合并历史;
git log --author=作者 # 查询作者的提交记录(和grep同时使用要加一个–all–match参数);
git log --grep=过滤信息 # 列出提交信息中包含过滤信息的提交记录;
git log -S # 查询内容 和–grep类似,S和查询内容间没有空格;
git log 文件名 # 查看某文件的修改记录;
命令测试:
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log
commit 7d49cabcee1132bd064b69da5dfed743632044c9 (HEAD -> master, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Sun Jan 29 14:52:02 2023 +0800
提交命令测试
commit 83d6125f47b2f0f7e9905edf71ce71695aad63c5
Author: 云三木 <xx@qq.com>
Date: Wed Dec 22 14:41:53 2021 +0800
2222222
commit f43ec26afd20a5102bbcd407ac6fca714b88d624
Author: 云三木 <xx@qq.com>
Date: Tue Dec 21 16:42:22 2021 +0800
1111
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log -p -2
commit 7d49cabcee1132bd064b69da5dfed743632044c9 (HEAD -> master, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Sun Jan 29 14:52:02 2023 +0800
提交命令测试
diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..5f2f16b
--- /dev/null
+++ b/test.txt
@@ -0,0 +1 @@
+1111
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log --stat test.txt
commit 7d49cabcee1132bd064b69da5dfed743632044c9 (HEAD -> master, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Sun Jan 29 14:52:02 2023 +0800
提交命令测试
test.txt | 1 +
1 file changed, 1 insertion(+)
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log --name-only
commit 7d49cabcee1132bd064b69da5dfed743632044c9 (HEAD -> master, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Sun Jan 29 14:52:02 2023 +0800
提交命令测试
test.txt
commit 83d6125f47b2f0f7e9905edf71ce71695aad63c5
Author: 云三木 <xx@qq.com>
Date: Wed Dec 22 14:41:53 2021 +0800
2222222
README.en.md
commit f43ec26afd20a5102bbcd407ac6fca714b88d624
Author: 云三木 <xx@qq.com>
Date: Tue Dec 21 16:42:22 2021 +0800
1111
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log --name-status
commit 7d49cabcee1132bd064b69da5dfed743632044c9 (HEAD -> master, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Sun Jan 29 14:52:02 2023 +0800
提交命令测试
A test.txt
commit 83d6125f47b2f0f7e9905edf71ce71695aad63c5
Author: 云三木 <xx@qq.com>
Date: Wed Dec 22 14:41:53 2021 +0800
2222222
M README.en.md
commit f43ec26afd20a5102bbcd407ac6fca714b88d624
Author: 云三木 <xx@qq.com>
Date: Tue Dec 21 16:42:22 2021 +0800
1111
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log --oneline
7d49cab (HEAD -> master, origin/master, origin/HEAD) 提交命令测试
83d6125 2222222
f43ec26 1111
a7f4aed Initial commit
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log test.txt
commit 7d49cabcee1132bd064b69da5dfed743632044c9 (HEAD -> master, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Sun Jan 29 14:52:02 2023 +0800
提交命令测试
4、比较文件异同
git diff
比较工作区与暂存区的差异。
git diff [] # 工作区与某分支的差异,远程分支这样写:remotes/origin/分支名;
git diff HEAD # 工作区与HEAD指针指向的内容差异;
git diff commit id # 文件路径 工作区某文件当前版本与历史版本的差异;
git diff --staged # 工作区文件与上次提交的差异(1.6 版本前用 –cached);
git diff 版本TAG # 查看从某个版本后都改动内容;
git diff [devA] [devB] # 比较从分支A和分支B的差异(也支持比较两个TAG);
git diff [devA]…[devB] # 比较两分支在分开后各自的改动;
另外:如果只想统计哪些文件被改动,多少行被改动,可以添加 --stat 参数。
命令测试:
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ echo '222' >> test.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ cat test.txt
1111
222
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git diff master
diff --git a/test.txt b/test.txt
index 5f2f16b..265fd37 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
1111
+222
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git diff remotes/origin/master
diff --git a/test.txt b/test.txt
index 5f2f16b..265fd37 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
1111
+222
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git diff HEAD
diff --git a/test.txt b/test.txt
index 5f2f16b..265fd37 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
1111
+222
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git diff 7d49cabcee1132bd064b69da5dfed743632044c9
diff --git a/test.txt b/test.txt
index 5f2f16b..265fd37 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
1111
+222
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git add .
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git diff --cached
diff --git a/test.txt b/test.txt
index 5f2f16b..265fd37 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
1111
+222
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git diff --staged
diff --git a/test.txt b/test.txt
index 5f2f16b..265fd37 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
1111
+222
5、分支相关
git branch
查看分支、创建分支、删除分支操作。
git branch # 查看分支
git branch -r # 列出所有远程分支
git branch -a # 列出所有本地分支和远程分支
git branch [本地分支] # 新建一个分支,但依然停留在当前分支
git branch [本地分支] [commitid] # 新建一个分支,指向指定commit
git branch --track [本地分支] [远程分支] # 新建一个分支,与指定的远程分支建立追踪关系
git branch --set-upstream [本地分支] [远程分支] # 建立追踪关系,在现有分支与指定的远程分支之间
git branch -d [本地分支] # 删除分支
git branch -dr [远程分支] # 删除远程分支
git push origin --delete [本地分支] # 删除远程分支
git checkout
主要用于切换分支。
git checkout -b [dev] # 新建一个分支,并切换到该分支
git checkout [dev] # 切换到指定分支,并更新工作区
git checkout - # 切换到上一个分支
上面的创建分支并切换分支就相当于 [创建分支] + [切换分支] 两个命令:
git checkout -b [dev]
=git branch [dev] (创建分支)
+git checkout [dev](切换分支)
。
注意:切换分支前需要先保存本地文件提交到暂存区,否则切换分支后修改文件会影响到其他分支。
命令测试(本地分支):
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch
* master
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch -r
origin/HEAD -> origin/master
origin/master
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch dev
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch -v
dev 7d49cab 提交命令测试
* master 7d49cab 提交命令测试
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log
commit 7d49cabcee1132bd064b69da5dfed743632044c9 (HEAD -> master, origin/master, origin/HEAD, dev)
Author: 云三木 <xx@qq.com>
Date: Sun Jan 29 14:52:02 2023 +0800
提交命令测试
commit 83d6125f47b2f0f7e9905edf71ce71695aad63c5
Author: 云三木 <xx@qq.com>
Date: Wed Dec 22 14:41:53 2021 +0800
2222222
commit f43ec26afd20a5102bbcd407ac6fca714b88d624
Author: 云三木 <xx@qq.com>
Date: Tue Dec 21 16:42:22 2021 +0800
1111
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch dev2 83d6125f47b2f0f7e9905edf71ce71695aad63c5
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch -v
dev 7d49cab 提交命令测试
dev2 83d6125 2222222
* master 7d49cab 提交命令测试
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git branch -d dev2
Deleted branch dev2 (was 83d6125).
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
命令测试(远程分支):
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git checkout dev
Switched to branch 'dev'
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git push origin dev
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'dev' on Gitee by visiting:
remote: https://gitee.com/public-investment-adviser/testing/pull/new/public-investment-adviser:dev...public-investment-adviser:master
To gitee.com:public-investment-adviser/testing.git
* [new branch] dev -> dev
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
# 两种删除远程分支的方式
# 第一种:
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git push origin --delete dev
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:public-investment-adviser/testing.git
- [deleted] dev
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
# 重新创建关联远程分支后再测试第二种删除方式:
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git branch -dr origin/dev
Deleted remote-tracking branch origin/dev (was 7d49cab).
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
6、标签
git tag
git tag [tag] # 打标签命令,默认为HEAD
git tag # 显示所有标签
git tag [tag] [commitid] # 给某个commit版本添加标签
git show
git show [tag-name] # 查看某个标签的详细信息
命令测试:
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git tag tag1
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git tag tag2 83d6125f47b2f0f7e9905edf71ce71695aad63c5
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git tag
tag1
tag2
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git show
commit 78a627ce5a422780f91e330d3d17d30e00665787 (HEAD -> master, tag: tag1, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Sun Jan 29 15:52:52 2023 +0800
ceshi222
diff --git a/test.txt b/test.txt
index 5f2f16b..265fd37 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
1111
+222
7、合并
git merge
git merge [dev] # 用于合并指定分支到当前分支
git merge --quit # 退出当前分支合并,当合并后冲突很多,要撤回合并分支就可以用这个命令
git merge --no-ff -m "message" [dev] # 加上–no-ff参数(Fast forward)就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并
git log --graph --pretty=oneline --abbrev-commit # 查看分支合并图
- git merge --no-ff -m [message] [branch-name] 不使用Fast forward合并分支,这样会创建新的commit,所以需要message。这样被合并的分支HEAD指向是会变的。
如果使用了Fast forward方式合并分支,那么删除次要分支的时候历史分支记录也会被删除,这样就无法追寻分支合拼信息了。
合并模式
1、Fast Forward(默认)
Fast Forward
意为 快进模式
。主要使用在多分支合并的情况下。即:当前分支合并另一个分支的时候,如果合并的过程中没有 Conflict冲突
的时候,则会通过直接移动两个分支的指针,来达到合并的过程,这个过程就叫做 Fast Forward
。
在 Fast Forward
模式下,当我们 merge
合并后,如果删除分支,则会丢失分支信息。因为在这个过程中没有创建 commit
。
什么是丢掉分支信息呢?看下面的例子:
dev
分支添加a.txt
,b.txt
文件,两次分别保存到暂存区,然后合并至master
,然后删除dev
分支,这时如果去查看git log
会发现我们无法区分哪些是dev
分支提交的,log
中只显示提交记录并没有区分master
和dev
的区别。
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch dev
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git checkout dev
Switched to branch 'dev'
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ ls
README.en.md README.md
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ touch a.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git add .
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git commit -m 'a.txt'
[dev 725f1bd] a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ touch b.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git add .
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git commit -m 'b.txt'
[dev 0d92e44] b.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git merge dev
Updating d99f83c..0d92e44
Fast-forward
a.txt | 0
b.txt | 0
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log
commit 0d92e447cdaaa79a9c48a17a583b4fdfabbfb689 (HEAD -> master, dev)
Author: 云三木 <xx@qq.com>
Date: Mon Jan 30 10:36:15 2023 +0800
b.txt
commit 725f1bd2e58ef522709cad71887eac3c46d529d8
Author: 云三木 <xx@qq.com>
Date: Mon Jan 30 10:35:48 2023 +0800
a.txt
2、–no-ff(No Fast Forward)
如果要强制禁用 Fast Forward
模式,Git
就会在 merge
时生成一个新的 commit
,这样,从分支历史上就可以看出分支信息。此处就会使用到 --no-ff
参数方式merge提交了
命令测试:
- 同样的步骤在这里使用
--no-ff方式
合并后,使用git log
查看会看到合并前分支会有一次提交记录,commit id
后显示分支名。commit 024a3a78104478f182b9ebe230dcdb65412d44eb (dev)
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ ls
README.en.md README.md
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch dev
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git checkout dev
Switched to branch 'dev'
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ ls
README.en.md README.md
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ touch c.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git add .
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git commit -m 'dev add c'
[dev 27c2226] dev add c
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 c.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ touch d.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git add .
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git commit -m 'dev add d'
[dev 024a3a7] dev add d
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 d.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 9 commits.
(use "git push" to publish your local commits)
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git merge --no-ff -m 'merge dev dc' dev
Merge made by the 'recursive' strategy.
c.txt | 0
d.txt | 0
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 c.txt
create mode 100644 d.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ ls
c.txt d.txt README.en.md README.md
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log
commit 16a3501a8ec4f12b2f6bf7c124f33d23d64d6205 (HEAD -> master)
Merge: 6f51c77 024a3a7
Author: 云三木 <xx@qq.com>
Date: Mon Jan 30 11:06:23 2023 +0800
merge dev dc
commit 024a3a78104478f182b9ebe230dcdb65412d44eb (dev)
Author: 云三木 <xx@qq.com>
Date: Mon Jan 30 11:04:59 2023 +0800
dev add d
commit 27c222695cb7bf16014b57da2e710bc196cb8a38
Author: 云三木 <xx@qq.com>
Date: Mon Jan 30 11:04:35 2023 +0800
dev add c
3、–squash(分支的多次commit合并到当前分支,分支commit记录不合并)
--squash
选项的含义是:本地文件内容与不使用该选项的合并结果相同,但是不提交、不移动HEAD,因此需要一条额外的commit命令。其效果相当于将其他分支上的多个commit合并成一个,放在当前分支上,原来的commit历史则没有拿过来。
判断是否使用 [–squash] 选项最根本的标准是,待合并分支上的历史是否有意义。
命令测试:
dev
分支添加aa.txt
,bb.txt
分别提交,master
使用--squash
模式合并dev
分支,并重新提交内容。
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git branch dev
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git checkout dev
Switched to branch 'dev'
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ ls
a.txt c.txt d.txt README.en.md README.md
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ touch aa.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git add .
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git commit -m 'dev add aa'
[dev 9b22fdd] dev add aa
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 aa.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ touch bb.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git add .
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git commit -m 'dev add bb'
[dev b7891b3] dev add bb
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 bb.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (dev)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git merge --squash dev
Updating 092c283..b7891b3
Fast-forward
Squash commit -- not updating HEAD
aa.txt | 0
bb.txt | 0
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 aa.txt
create mode 100644 bb.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log
commit 092c28348e78a1b3312f1679d7135e3d4faf5322 (HEAD -> master, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Mon Jan 30 14:58:06 2023 +0800
add a
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ ls
a.txt aa.txt bb.txt c.txt d.txt README.en.md README.md
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git commit -m 'merge dev add aa,bb'
[master abdc76e] merge dev add aa,bb
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 aa.txt
create mode 100644 bb.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 6 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 247 bytes | 247.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:public-investment-adviser/testing.git
092c283..abdc76e master -> master
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git log
commit abdc76ec825d32d76cd553918c299a3ea11a8139 (HEAD -> master, origin/master, origin/HEAD)
Author: 云三木 <xx@qq.com>
Date: Mon Jan 30 15:07:34 2023 +0800
merge dev add aa,bb
commit 092c28348e78a1b3312f1679d7135e3d4faf5322
Author: 云三木 <xx@qq.com>
Date: Mon Jan 30 14:58:06 2023 +0800
add a
8、撤销
git reset
撤销之前添加到暂存区或本地版本库的内容。
git reset HEAD^ # 恢复成上次提交的版本
git reset HEAD^^ # 恢复成上上次提交的版本,就是多个^,以此类推或用~次数
git reset HEAD~N # N直接指定倒数第几版本
git reset [--hard] [版本号|文件名]
--soft:只是改变HEAD指针指向,缓存区和工作区不变;
--mixed:修改HEAD指针指向,暂存区内容丢失,工作区不变;
--hard:修改HEAD指针指向,暂存区内容丢失,工作区恢复以前状态;
git reflog
查看添加到暂存区的记录,找到版本号。
命令测试:
- 创建
a.txt
文件添加到暂存区,然后撤回暂存区内容回到当前版本,再撤回到上一版本,根据git relflog
找到版本号,回到当前版本。
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ touch a.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ ls
a.txt c.txt d.txt README.en.md README.md
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git add .
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: a.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git reset HEAD
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
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)
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git reset HEAD^
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git status
On branch master
Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
c.txt
d.txt
nothing added to commit but untracked files present (use "git add" to track)
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git status -s
?? a.txt
?? c.txt
?? d.txt
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git relog
git: 'relog' is not a git command. See 'git --help'.
The most similar command is
reflog
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git reflog
6f51c77 (HEAD -> master) HEAD@{0}: reset: moving to HEAD
6f51c77 (HEAD -> master) HEAD@{1}: reset: moving to HEAD^
16a3501 (origin/master, origin/HEAD) HEAD@{2}: reset: moving to HEAD
16a3501 (origin/master, origin/HEAD) HEAD@{3}: merge dev: Merge made by the 'recursive' strategy.
6f51c77 (HEAD -> master) HEAD@{4}: checkout: moving from dev to master
024a3a7 HEAD@{5}: commit: dev add d
27c2226 HEAD@{6}: commit: dev add c
4aa9348 HEAD@{7}: checkout: moving from master to dev
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git reset 16a3501
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git reflog
16a3501 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to 16a3501
6f51c77 HEAD@{1}: reset: moving to HEAD
6f51c77 HEAD@{2}: reset: moving to HEAD^
16a3501 (HEAD -> master, origin/master, origin/HEAD) HEAD@{3}: reset: moving to HEAD
16a3501 (HEAD -> master, origin/master, origin/HEAD) HEAD@{4}: merge dev: Merge made by the 'recursive' strategy.
6f51c77 HEAD@{5}: checkout: moving from dev to master
024a3a7 HEAD@{6}: commit: dev add d
27c2226 HEAD@{7}: commit: dev add c
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
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)
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git reset 16a3501
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git status -s
?? a.txt
9、远程仓库操作
git push
推送到远程仓库。
同步远程仓库:git push -u origin master
git push [-u] <远程主机名> <本地分支名>:<远程分支名> # 如果本地分支名与远程分支名相同,则可以省略冒号:
-u: 带上-u 参数其实就相当于记录了push到远端分支的默认值,
这样当下次我们还想要继续push的这个远端分支的时候推送命令就可以简写成git push即可。
git push origin master # 推送到远程分支
git push --force origin master # 强制推送
git push origin --delete master # 删除远程分支
git pull
从远程仓库拉取代码并与本地合并,其实就是 git fetch
和 git merge FETCH_HEAD
的简写。
git pull <远程主机名> <远程分支名>:<本地分支名>
git pull origin master:master
git pull origin master
git clone
从远程仓库克隆项目到本地。
git clone [url]
git remote
用于在远程仓库的操作。
本地仓库内容推送到远程仓库:git remote add origin git@github.com:帐号名/仓库名.git
查看远程库信息:git remote
git remote -v # 显示所有远程仓库
git remote show [remote] # 显示某个远程仓库的信息,remote为远程地址的别名
git remote add [shortname] [url] # 添加远程版本库,shortname 为本地的版本库
git remote rm name # 删除远程仓库
git remote rename old_name new_name # 修改仓库名
git fetch
从远程获取代码库。
git fetch origin <远程分支>:<本地分支> # 会在本地新建分支,但不会自动切换,还需checkout
git fetch origin master # 同步远程仓库更新
git pull origin master = git fetch origin master + git merge origin master
10、配置
git config
安装完Git后第一件要做的事,设置用户信息(global可换成local在单独项目生效):
git config --global user.name “用户名” # 设置用户名
git config --global user.email “用户邮箱” # 设置邮箱
git config --global user.name # 查看用户名是否配置成功
git config --global user.email # 查看邮箱是否配置
# 其他查看配置相关
git config --system --ist # 查看系统配置参数列表
git config --global --list # 查看全局设置相关参数列表
git config --local --list # 查看本地设置相关参数列表
git config --list # 查看所有Git的配置(全局+本地+系统)
git config --global color.ui true # 显示git相关颜色
命令测试:
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git config --global user.name
云三木
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git config --global user.email
xx@qq.com
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git config --system --list
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=D:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git config --global --list
user.name=云三木
user.email=xx@qq.com
credential.helper=manager
url.https://.insteadof=git://
http.sslverify=false
core.autocrlf=false
gui.encoding=utf-8
i18n.commitencoding=utf-8
i18n.logoutputencoding=utf-8
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git config --local --list
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=git@gitee.com:public-investment-adviser/testing.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
gui.wmstate=normal
gui.geometry=841x483+182+182 189 218
Admin@DESKTOP-CSH3FGN MINGW64 /e/work/test/gittest (master)
$ git config --list
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=D:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager
user.name=云三木
user.email=xx@qq.com
credential.helper=manager
url.https://.insteadof=git://
http.sslverify=false
core.autocrlf=false
gui.encoding=utf-8
i18n.commitencoding=utf-8
i18n.logoutputencoding=utf-8
core.repositoryformatversion=0
core.filemode=false