方法一

三步骤:先重命名,再git add重命名后的文件,最后git rm 原始名称文件即可。

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
 G:\mygitea\GitLearn\learn01   master 
$ ls
images/ index.html js/ LICENSE Readme.md styles/

G:\mygitea\GitLearn\learn01   master 
$ mv Readme.md readme

G:\mygitea\GitLearn\learn01   master ± 
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: Readme.md

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

no changes added to commit (use "git add" and/or "git commit -a")

G:\mygitea\GitLearn\learn01   master ± 
$ git add readme

G:\mygitea\GitLearn\learn01   master ± 
$ git rm Readme.md
rm 'Readme.md'

G:\mygitea\GitLearn\learn01   master ± 
$ git status
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:
(use "git restore --staged <file>..." to unstage)
renamed: Readme.md -> readme

换另一种方法,先清空工作区和暂存区的操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 G:\mygitea\GitLearn\learn01   master ± 
$ ls
images/ index.html js/ LICENSE readme styles/

G:\mygitea\GitLearn\learn01   master ± 
$ git reset --hard
HEAD is now at 012ae46 e1

G:\mygitea\GitLearn\learn01   master 
$ ls
images/ index.html js/ LICENSE Readme.md styles/

G:\mygitea\GitLearn\learn01   master 
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)

nothing to commit, working tree clean

可以看到,工作目录恢复成原来的样子了。

方法2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 G:\mygitea\GitLearn\learn01   master 
$ git mv Readme.md readme

G:\mygitea\GitLearn\learn01   master ± 
$ git status
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:
(use "git restore --staged <file>..." to unstage)
renamed: Readme.md -> readme

G:\mygitea\GitLearn\learn01   master ± 
$ git commit -m "rename test"
[master 5bc7fdf] rename test
1 file changed, 0 insertions(+), 0 deletions(-)
rename Readme.md => readme (100%)

重命名后要记得将工作区文件提交。