您现在的位置是:首页 >技术杂谈 >Git常用命令commit网站首页技术杂谈
Git常用命令commit
Git常用命令commit
将暂存区内容添加到本地仓库中。
# 将暂存区的文件提交到本地仓库并添加提交说明
$ git commit -m message
# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]
# 提交工作区自上次commit之后的变化,直接到仓库区
# 该命令将提交git add命令添加的所有文件,并提交git add命令之后更改的所有文件
$ git commit -a
# add和commit的合并,便捷写法
# 未跟踪的文件是无法提交上去的
$ git commit -am "本次提交的说明"
# 提交时显示所有diff信息
$ git commit -v
# 编辑器会弹出上一次提交的信息,可以在这里修改提交信息
# 把暂存的内容添加到上一次的提交
$ git commit --amend
# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
# 修改上次的提交内容,需要修改没有push之前的提交
# 修复提交,同时修改提交信息
$ git commit --amend -m message
# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...
# 追加新的内容到上次没有push的提交
# 加入--no-edit标记会修改提交但不修改提交信息,编辑器不会弹出上一次提交的信息
# 假设我们提交的时候,忘记了一个配置文件,不想修改log,不想添加新的commit-id
$ git commit --amend --no-edit
# 跳过验证继续提交
$ git commit --no-verify
# 这个选项可以绕过pre-commit和commit-msg
$ git commit -n
1.1 手动整理commit
有这样一个需求,代码量不多,但是因为你多次提交,会导致在建立 Merge Request 的时候,出现了几十个
commitId,为了解决这样的问题,我们可以巧妙的利用 git reset。
比如当前的commit是这样的:
touch README.md
git add README.md
git commit -m "add README.md"
touch a.txt
git add a.txt
git commit -m "add a.txt"
touch b.txt
git add b.txt
git commit -m "add b.txt"
touch c.txt
git add c.txt
git commit -m "add c.txt"
touch d.txt
git add d.txt
git commit -m "add d.txt"
touch e.txt
git add e.txt
git commit -m "add e.txt"
touch f.txt
git add f.txt
git commit -m "add f.txt"
$ git log --oneline
f7677a7 (HEAD -> master) add f.txt
93c7b77 add e.txt
2f72fe6 add d.txt
d04fd42 add c.txt
d7eb09e add b.txt
079edce add a.txt
9e370f8 add README.md
假设第一个提交是 768d900,那么我们执行如下的命令:
# 重置本地分支HEAD指针
$ git reset --soft 9e370f8
$ git commit -m "add files"
[master 8b0c832] add files
6 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
create mode 100644 c.txt
create mode 100644 d.txt
create mode 100644 e.txt
create mode 100644 f.txt
$ git log --oneline
8b0c832 (HEAD -> master) add files
9e370f8 add README.md
$ ls
a.txt b.txt c.txt d.txt e.txt f.txt README.md
# 提交到远程分支
$ git push origin master -f
-f 是force的含义,表示强行执行本次操作。当我们 git reset 之后,本地的 HEAD 指针指向的 commitId 会比远程
origin 对应的落后,直接 push 会被拒绝。通过 -f 命令可以强行将本地内容 push 到远程分支上,切记如果是多人
共同合作的开发分支或者远程 master 操作,千万不能加 -f 操作。
1.2 多提交了文件怎么办
假如基础 commit 是 A,修改了文件 foo.txt 和 bar.txt 以后生成 commit B。
如果还没push,这时发现 bar.txt 的修改是不需要 commit 进去的,那么可以用以下命令把 bar.txt 从 commit B
里面去掉:
$ touch init.txt
$ git add init.txt
$ git commit -m "add init.txt"
[master (root-commit) 5683da7] add init.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 init.txt
$ touch foo.txt
$ touch bar.txt
$ git add foo.txt bar.txt
$ git commit -m "add foo.txt bar.txt"
[master e21cdb1] add foo.txt bar.txt
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 bar.txt
create mode 100644 foo.txt
$ git reset HEAD^ -- bar.txt
# --no-edit不需要修改commit message
$ git commit --amend
[master dbe11cb] add foo.txt bar.txt
Date: Mon Mar 13 13:55:47 2023 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 foo.txt
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
bar.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git log
commit dbe11cbea7fb1bc91121125786e41d4015754486 (HEAD -> master)
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 13:55:47 2023 +0800
add foo.txt bar.txt
commit 5683da778ae61070953e42c4844e8e9bbb795075
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 13:54:44 2023 +0800
add init.txt
1.3 提交了错误代码
代码错误提交了怎么办,重新再一次提交一个版本呗,这个可能是很多人的解决方案,当然Git也是有提供自己的
解决方案的命令。
第一种就是再次将修改文件然后 git add .
到暂存区,最后 git commit --amend
提交修改。
$ echo success > a.txt
$ echo fail > b.txt
$ git add a.txt b.txt
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in b.txt.
The file will have its original line endings in your working directory.
$ git commit -m "add a.txt b.txt"
[master (root-commit) e922ec1] add a.txt b.txt
2 files changed, 2 insertions(+)
create mode 100644 a.txt
create mode 100644 b.txt
$ git log
commit e922ec1f9f70791eaaecfe645a5dfc4af682287f (HEAD -> master)
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 14:02:01 2023 +0800
add a.txt b.txt
# 修改文件内容
$ echo success > b.txt
$ git add b.txt
$ git commit --amend --no-edit
[master 620b123] add a.txt b.txt
Date: Mon Mar 13 14:02:01 2023 +0800
2 files changed, 2 insertions(+)
create mode 100644 a.txt
create mode 100644 b.txt
$ git log
commit 620b123eec601ffffaa87b6e48fc9f7ddeb01a50 (HEAD -> master)
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 14:02:01 2023 +0800
add a.txt b.txt
这种方法只能修改当前 HEAD,也就是最新的提交,那么要修改是倒数第二个或者倒数第三个的提交呢?
这时候就要使用 rebase -i (交互式rebase)进行操作了,这个命令是指定commit链中哪一个commit需要修改。
比如执行命令:git rebase -i HEAD^
,它表示的含义就是把当前commit内容rebase到HEAD之前的一个
commit上。若是想直接丢弃最新的commit的修改,则直接使用命令:git reset --hard HEAD^
,他表示当
前commit往前移动一次。
例如提交了三次代码,其中第二个阶段被提出有问题,需要修改第二个commit。
$ echo a > a.txt
$ 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.
$ git commit -m "add a.txt"
[master (root-commit) 85024a7] add a.txt
1 file changed, 1 insertion(+)
create mode 100644 a.txt
$ echo 1 > b.txt
$ git add b.txt
warning: LF will be replaced by CRLF in b.txt.
The file will have its original line endings in your working directory.
$ git commit -m "add b.txt"
[master eb04cfc] add b.txt
1 file changed, 1 insertion(+)
create mode 100644 b.txt
$ echo c > c.txt
$ git add c.txt
warning: LF will be replaced by CRLF in c.txt.
The file will have its original line endings in your working directory.
$ git commit -m "add c.txt"
[master c3f43f4] add c.txt
1 file changed, 1 insertion(+)
create mode 100644 c.txt
$ git reflog
c3f43f4 (HEAD -> master) HEAD@{0}: commit: add c.txt
eb04cfc HEAD@{1}: commit: add b.txt
85024a7 HEAD@{2}: commit (initial): add a.txt
进行修改,首先执行下面的命令:
# 倒数第2个提交
$ git rebase -i HEAD~2
Stopped at eb04cfc... add b.txt
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LsAGgkX1-1685280186838)(…/…/images/Git/023.png)]
按 i 输入,然后将需要修改的 commit 前的 pick 改为 edit 或者 e,esc退出编辑,输入 :wq 回车保存并退出。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SIzWfty1-1685280186838)(…/…/images/Git/024.png)]
此时使用 git log 查看提交记录,就会发现变成了要修改commit的那条记录了:
$ git log
commit eb04cfc1c8b4e882ebc0482f4ceb3ea5329e4bf0 (HEAD)
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 15:38:44 2023 +0800
add b.txt
commit 85024a7247d95f554285ab1823c22faaed593d9b
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 15:38:29 2023 +0800
add a.txt
此时也只能看到前面提交的两个文件:
$ ls
a.txt b.txt
此时可以修改你的代码:
$ echo b > b.txt
$ git status
interactive rebase in progress; onto 85024a7
Last command done (1 command done):
edit eb04cfc add b.txt
Next command to do (1 remaining command):
pick c3f43f4 add c.txt
(use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'master' on '85024a7'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
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: b.txt
no changes added to commit (use "git add" and/or "git commit -a")
添加文件:
$ git add b.txt
修改commit信息:
$ git commit --amend
[detached HEAD 79b58ae] add b.txt
Date: Mon Mar 13 15:38:44 2023 +0800
1 file changed, 1 insertion(+)
create mode 100644 b.txt
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9vSxt35t-1685280186840)(…/…/images/Git/025.png)]
$ git status
interactive rebase in progress; onto 85024a7
Last command done (1 command done):
edit eb04cfc add b.txt
Next command to do (1 remaining command):
pick c3f43f4 add c.txt
(use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'master' on '85024a7'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
nothing to commit, working tree clean
最后,执行 git rebase --continue
就可以了:
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
查看目前的状态:
$ git log
commit 74e53a894c0f86e74b8fe413776638b6ec7ec07a (HEAD -> master)
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 15:39:01 2023 +0800
add c.txt
commit 79b58ae981f070c3bf59fafa654984262fe4a78a
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 15:38:44 2023 +0800
add b.txt
commit 85024a7247d95f554285ab1823c22faaed593d9b
Author: zsx242030 <2420309401@qq.com>
Date: Mon Mar 13 15:38:29 2023 +0800
add a.txt
$ ls
a.txt b.txt c.txt
$ git status
On branch master
nothing to commit, working tree clean
如果 git push 无法推送的话,执行 git psuh -f。
1.4 提交信息写错了
# 两种方式
# 1
$ git commit --amend --only
# 2
$ git commit --amend --only -m 'message'
touch README.md
git add README.md
git commit -m "add README.md"
touch a.txt
git add a.txt
git commit -m "add a.txt"
touch b.txt
git add b.txt
git commit -m "update b.txt"
$ git log --oneline
384d8a5 (HEAD -> master) update b.txt
e233534 add a.txt
56d32f3 add README.md
$ git commit --amend -m "add b.txt"
[master 7f5f5c9] add b.txt
Date: Tue May 23 21:53:19 2023 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
$ git log --oneline
7f5f5c9 (HEAD -> master) add b.txt
e233534 add a.txt
56d32f3 add README.md
git commit --amend
既可以修改上次提交的文件内容,也可以修改上次提交的说明。会用一个新的 commit 更
新并替换最近一次提交的 commit 。如果暂存区有内容,这个新的 commit 会把任何修改内容和上一个 commit
的内容结合起来。如果暂存区没有内容,那么这个操作就只会把上次的 commit 消息重写一遍。永远不要修复一
个已经推送到公共仓库中的提交,会拒绝推送到仓库。
1.5 修改已提交的commit的作者信息
假设有下面的提交:
git config --local user.name "zhangsan"
git config --local user.email "zhangsan@qq.com"
touch a.txt
git add a.txt
git commit -m "add a.txt"
touch b.txt
git add b.txt
git commit -m "add b.txt"
git config --local user.name "lisi"
git config --local user.email "lisi@qq.com"
touch c.txt
git add c.txt
git commit -m "add c.txt"
touch d.txt
git add d.txt
git commit -m "add d.txt"
git config --local user.name "zhangsan"
git config --local user.email "zhangsan@qq.com"
touch e.txt
git add e.txt
git commit -m "add e.txt"
touch f.txt
git add f.txt
git commit -m "add f.txt"
$ git log --pretty="%an %ae"
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
lisi lisi@qq.com
lisi lisi@qq.com
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
$ git log --oneline
e6084e9 (HEAD -> master) add f.txt
cde3fea add e.txt
d1bd527 add d.txt
25d6209 add c.txt
3d0e8ce add b.txt
e24bb50 add a.txt
本身都是 zhangsan 提交,结果其中有两个提交是 lisi 提交的,那么如何将 lisi 的提交信息改为 zhangsan。
答案是使用万能的rebase。
$ git rebase -i 3d0e8ce
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RKmkNIJj-1685280186840)(…/…/images/Git/060.png)]
将要修改的提交前面的 pick 改为 edit 或者 e:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GcuDJQO1-1685280186840)(…/…/images/Git/061.png)]
保存,会输出下面的信息:
$ git rebase -i 3d0e8ce
Stopped at 25d6209... add c.txt
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
然后我们执行下面的命令:
# --no-edit不需要修改commit message
$ git commit --amend --author "正确的作者信息" --no-edit
$ git commit --amend --author "zhangsan <zhangsan@qq.com>" --no-edit
[detached HEAD 68d1315] add c.txt
Date: Wed May 24 20:53:15 2023 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 c.txt
$ git log --pretty="%an %ae"
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
然后执行 git rebase–continue 继续下一个修改的内容:
$ git rebase --continue
Stopped at d1bd527... add d.txt
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
# 继续修改内容
$ git commit --amend --author "zhangsan <zhangsan@qq.com>" --no-edit
[detached HEAD ea353c2] add d.txt
Date: Wed May 24 20:53:15 2023 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 d.txt
$ git log --pretty="%an %ae"
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
# 输出这个信息说明没有要修改的内容了
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
查看修改后的情况:
$ git log --pretty="%an %ae"
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
zhangsan zhangsan@qq.com
$ git log --oneline
623ca3e (HEAD -> master) add f.txt
b657ee2 add e.txt
ea353c2 add d.txt
68d1315 add c.txt
3d0e8ce add b.txt
e24bb50 add a.txt
已经将信息正确的改过来了。
如果是最后一次 commit 的作者信息写错了,只需要执行下面的命令即可:
$ git commit --amend --author "zhangsan <zhangsan@qq.com>" --no-edit
1.6 从一个提交里移除一个文件
使用 git commit
提交了几个文件,后来发现错误地将一个文件添加到了提交中。如何从上次提交中删除文件?
$ echo f > f.txt
$ echo g > g.txt
$ git add f.txt g.txt
warning: LF will be replaced by CRLF in f.txt.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in g.txt.
The file will have its original line endings in your working directory.
$ git commit -m "add f.txt g.txt"
[master 04bff5f] add f.txt g.txt
2 files changed, 2 insertions(+)
create mode 100644 f.txt
create mode 100644 g.txt
这个是一个将错误提交的文件从前一次提交移回到暂存区域的问题,而不取消对它们所做的更改。
# git reset --soft HEAD~1
$ git reset --soft HEAD^
$ git reset HEAD g.txt
# 现在再次提交,甚至可以重用相同的提交消息
$ git commit -c ORIG_HEAD
[master eb4fde6] add f.txt
Date: Thu Mar 9 09:57:12 2023 +0800
1 file changed, 1 insertion(+)
create mode 100644 f.txt
$ git push origin "master"
另外一种情况是希望从本地和远程存储库中完全删除该文件,则可以:
$ echo 3 > 3.txt
$ echo 4 > 4.txt
$ git add 3.txt 4.txt
warning: LF will be replaced by CRLF in 3.txt.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in 4.txt.
The file will have its original line endings in your working directory.
$ git commit -m "add 3.txt 4.txt"
[master 886556a] add 3.txt 4.txt
2 files changed, 2 insertions(+)
create mode 100644 3.txt
create mode 100644 4.txt
$ git rm 4.txt
rm '4.txt'
$ git commit --amend
[master 0fcb167] add 3.txt
Date: Thu Mar 9 10:20:31 2023 +0800
1 file changed, 1 insertion(+)
create mode 100644 3.txt
$ git push origin "master"
这将非常有用,当你有一个开放的补丁 (open patch),你往上面提交了一个不必要的文件,你需要强推 (force
push) 去更新这个远程补丁。