git-commit命令

git commit –amend

建议 git commit –amend 命令使用在未push到远端的场景。

提交还未push到远端

在某次修改时,修改了README.md文件,同时新增了amendTest文件。但是在commit的时候,只提交了README.md,忘记了amendTest文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@localhost gitTest]# git status
On branch master
Your branch is up to date with 'origin/master'.

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: README.md

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

amendTest

no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost gitTest]# git commit -am 'add amendTest file'
[master ce1e058] add amendTest file
1 file changed, 1 insertion(+)
[root@localhost gitTest]# git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)

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

amendTest

nothing added to commit but untracked files present (use "git add" to track)
[root@localhost gitTest]# git log -n 3 --oneline
a889479 (HEAD -> master) add amendTest file
11843d7 (origin/master, origin/HEAD) update
bf2a12f update
[root@localhost gitTest]# git diff --stat HEAD~1 HEAD
README.md | 1 +
1 file changed, 1 insertion(+)
[root@localhost gitTest]#

此时,如果想将amendTest文件提交,同时不产生新的commit记录(即新提交内容合并到上一次提交中),可以使用git commit --amend命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost gitTest]# git add .
[root@localhost gitTest]# git commit --amend
add amendTest file

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sun Jun 30 15:00:20 2019 +0800
#
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# modified: README.md
# new file: amendTest
Date: Sun Jun 30 15:00:20 2019 +0800
2 files changed, 2 insertions(+)
create mode 100644 amendTest
[root@localhost gitTest]# git log -n 3 --oneline
d2a42e9 (HEAD -> master) add amendTest file
11843d7 (origin/master, origin/HEAD) update
bf2a12f update
[root@localhost gitTest]# git diff --stat HEAD~1 HEAD
README.md | 1 +
amendTest | 1 +
2 files changed, 2 insertions(+)

使用该git commit --amend命令后,会生成一个新的commitid,合并本次提交与上一次提交内容。

提交已经push到远端

如果上一次commit已经push到远端,使用git commit --amend提交后,在push到远端时会被拒绝,通过git status提示发现当前分支与origin/master已经分叉

通过git pull将origin/master代码merge过来后,重新push可以推送成功

但是通过git log可以发现远端仍是有两次提交记录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
[root@localhost gitTest]# git log -n 3 --oneline
e30f084 (HEAD -> master) add amendTest file
11843d7 (origin/master, origin/HEAD) update
bf2a12f update
[root@localhost gitTest]# git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 404 bytes | 202.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:daybreakgx/gitTest.git
11843d7..e30f084 master -> master
[root@localhost gitTest]# git log -n 3 --oneline
e30f084 (HEAD -> master, origin/master, origin/HEAD) add amendTest file
11843d7 update
bf2a12f update
[root@localhost gitTest]# git add .
[root@localhost gitTest]# git commit --amend
[master 7e0656c] add amendTest file add 2
Date: Sun Jun 30 15:25:00 2019 +0800
2 files changed, 2 insertions(+)
create mode 100644 amendTest
[root@localhost gitTest]# git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean
[root@localhost gitTest]# git log -n 3 --oneline
7e0656c (HEAD -> master) add amendTest file add 2
11843d7 update
bf2a12f update

[root@localhost gitTest]# git push origin master
To github.com:daybreakgx/gitTest.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:daybreakgx/gitTest.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

[root@localhost gitTest]# git pull
Merge made by the 'recursive' strategy.
[root@localhost gitTest]# git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)

nothing to commit, working tree clean
[root@localhost gitTest]# git push origin master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 437 bytes | 218.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To github.com:daybreakgx/gitTest.git
e30f084..bef9cc4 master -> master
[root@localhost gitTest]# git log --graph
* commit bef9cc49c27913ca9924378dd50c0ef50f6d8d48 (HEAD -> master, origin/master, origin/HEAD)
|\ Merge: 7e0656c e30f084
| | Author: daybreakgx <daybreak.gx@gmail.com>
| | Date: Sun Jun 30 15:27:59 2019 +0800
| |
| | Merge branch 'master' of github.com:daybreakgx/gitTest
| |
| * commit e30f08487acfe730bfa241e0042a55503d41b791
| | Author: daybreakgx <daybreak.gx@gmail.com>
| | Date: Sun Jun 30 15:25:00 2019 +0800
| |
| | add amendTest file
| |
* | commit 7e0656c521428142bd248153050918a7a5f5d0ab
|/ Author: daybreakgx <daybreak.gx@gmail.com>
| Date: Sun Jun 30 15:25:00 2019 +0800
|
| add amendTest file add 2
|
* commit 11843d78157ea67dc2d4768366c1f6c446d93071
| Author: daybreakgx <daybreak.gx@gmail.com>
| Date: Sun Jun 30 14:58:02 2019 +0800
|
| update

撤销git commit –amend