提交 git commit

Record changes to the repository

记录对存储库的更改

git commit 命令用于将更改记录(提交)到存储库。将索引库的 当前内容 与 描述更改的用户 和 日志消息 一起存储在新的提交中。

如果您提交,然后立即发现错误,可以使用 git reset 命令恢复。

提交至本地仓

表示提交的版本描述,如果给出了多个 -m ,它们的值将作为单独的段落连接起来。

# 对代码进行提交,会进入 VIM 模式,可对提交信息进行描述

git commit

# 快速提交,只对提交进行单行描述

git commit --message

git commit -m

 示例:对提交提交进行单行信息描述

git commit -m 'the commit messge'

如果你想将暂存区指定的文件修改提交到本地仓库区,你可以使用如下的命令。 表示你要提交的文件路径,文件可以是多个。

git commit ... -m

# 例如

git commit demo1.js demo2.js -m '提交demo文件'

提交跟踪文件

如果你想将工作区所有自上一次提交 commit 之后的变化直接提交到仓库区,你可以使用如下命令,相当于省略了 git add。

对于还没有跟踪 track 的文件,还是需要执行 git add 命令。

git commit -a

提交时查看文件修改详情

git commit -v

增补提交

如果你想重做上一次 commit,并包括指定文件的新变化,那么你可以使用如下命令。

增补提交,会使用与当前提交节点相同的父节点进行一次新的提交,旧的提交将会被取消。

git commit -amend

如果你想使用一次新的提交 commit,替代上一次提交,那么你可以使用如下命令。

如果代码没有任何新变化,则用来改写上一次 commit 的提交信息。

git commit -amend -m

增补提交能够不 reset 上次 commit 而通过再 commit 覆盖。

修改提交备注内容

修改最近一次 commit message

# 语法

git commit --amend --author=

# 示例 修改提交者

git commit --amend --author='tsejx '

# 修改后可以用 log 命令查看

git log --pretty=oneline

修改历史记录

先通过 git log 查看提交记录

git log

执行 git rebase 命令,修改近三次的信息

git rebase -i HEAD~3

将会得到如下信息,这里的提交日志是和 git log倒叙排序的,我们要修改的日志信息位于第一位。

1 pick 2275781 should find method from parent

2 pick 223fc80 unit test case

3 pick 9ac1179 update test case

4

5 # Rebase 79db0bd..9ac1179 onto 79db0bd (3 commands)

6 #

7 # Commands:

8 # p, pick = use commit

9 # r, reword = use commit, but edit the commit message

10 # e, edit = use commit, but stop for amending

11 # s, squash = use commit, but meld into previous commit

12 # f, fixup = like "squash", but discard this commit's log message

13 # x, exec = run command (the rest of the line) using shell

14 # d, drop = remove commit

15 #

16 # These lines can be re-ordered; they are executed from top to bottom.

17 #

18 # If you remove a line here THAT COMMIT WILL BE LOST.

19 #

20 # However, if you remove everything, the rebase will be aborted.

21 #

22 # Note that empty commits are commented out

我们现在要修改 should find method from parent 这条日志,那么修改的日志为第一个 pick 修改为 edit,然后 :wq 退出。

edit 2275781 should find method from parent

pick 223fc80 unit test case

pick 9ac1179 update test case

将会看到如下信息,意思就是如果要改日志,执行 git commit --amend,如果修改完成后,执行 git rebase --continue。

client_java git:(fix_aop_no_class_defined) git rebase -i HEAD~3

Stopped at 2275781... should find method from parent

You can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

➜ client_java git:(2275781)

正式修改,执行命令 -s,就是自动加上 Signed-off-by:

git commit --amend -s

修改完成后,:wq 退出,然后完成此次 log 的 rebase:

git rebase --continue

初始化提交记录

⚠️ 慎重操作

也就是把所有的改动都重新放回工作区,并清空所有的commit,这样就可以重新提交第一个 commit 了。

git update-ref -d HEAD

参考资料:

修改 git 历史提交 commit 信息(重写历史)

配置 git config

Get and set repository or global options

获取并设置仓库

git config 命令用于获取并设置存储库或全局选项。这些变量可以控制 Git 的外观和操作的各个方面。

一般在新的系统上,我们都需要先配置下自己的 git 工作环境。配置工作只需一次,以后升级时还会沿用现在的配置。当然,如果需要,你随时可以用相同的命令修改已有的配置。

查看配置信息

配置信息分为:

--local:局部(当前目录)配置信息 (默认值)--global:全局配置信息

# 当前目录

git config --local --list

# 全局

git config --global --list

配置开发者信息

当安装 Git 后你首先要做的事情是设置用户名称和电子邮件地址。这是非常重要的,因为每次 Git 提交都会使用该信息,它被永远的嵌入到了你的提交中。

# 全局配置(所有项目)

git config --global user.name

git config --global user.email

# 当前目录配置(当前项目)

git config user.name

git config user.email

 示例:

git config --global user.name "Mercedes-benz"

git config --global user.email "mercedes@gmail.com"

git config user.name "Lamborghini"

git config user.email "lammborghini@gmail.com"

添加配置项

entry-name 为配置项名称。

# 当前目录

git config --local –add

# 全局环境

git config --global –add

# 系统

git config --system –add

 示例:

git config -–add site.name yiibai

注意 add 后面的 section 、 key、 value 一项都不能少,否则添加失败。

删除配置项

# 当前目录

git config --local --unset

# 全局环境

git config --global--unset

# 系统环境

git config --system --unset

 示例:

git config --local -–unset site.name

简化命令

将冗长的命令简化。

git config --global alias.

 示例:

git status 改成 git st ,这样可以简化输入命令。

git config --global alias.st status

忽略文件的权限变化

不再将文件的权限变化视作改动。

 示例:

git config core.fileMode false

差异 git diff

how changes between commits, commit and working tree, etc

用于显示提交和工作树等之间的更改。此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容。

查看工作区与暂存区差异

查看文件在工作区与暂存区的差别。

git diff

如果还没 add 到暂存区,则查看文件自身修改前后的差别。也可查看和另一分支的区别。

git diff

查暂存区与最近版本的差异

表示查看已经 add 进暂存区但是尚未 commit 的内容同最新一次 commit 时的内容的差异。

git diff --cached

如果你要比较指定仓库版本。

git diff --cached

查看工作区与最近版本间差异

查看工作区同 Git 仓库指定提交版本的差异。

git diff

查看提交版本间的差异

git diff

查看工作区、暂存区和最近版本间的差异

git diff HEAD

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: