欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Git如何将文件重命名

程序员文章站 2024-02-21 23:12:55
...
#第一种方法

#1. 本地重命名 

[[email protected] ~/git_test]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	deleted:    a.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	a.log
no changes added to commit (use "git add" and/or "git commit -a")

#2. 把暂存区中的a.txt删除

[[email protected] ~/git_test]# git rm --cached a.txt
rm 'a.txt'
[[email protected] ~/git_test]# git status 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    a.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	a.log

#3. 将修改之后的文件名添加到暂存区 

[[email protected] ~/git_test]# git add .
[[email protected] ~/git_test]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	renamed:    a.txt -> a.log
#

#4.将修改之后的文件提交到本地仓库 
[[email protected] ~/git_test]# git commit  -m "rename a.txt  a.log"
[master 2345032] rename a.txt  a.log
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename a.txt => a.log (100%)
[[email protected] ~/git_test]# git status
# On branch master
nothing to commit, working directory clean

#第二种方法

#1. 直接重命名工作目录及暂存区的文件名称
[[email protected] ~/git_test]# git mv a.log a.txt
[[email protected] ~/git_test]# ll
total 0
-rw-r--r-- 1 root root 0 2020-05-11 14:48 a.txt
-rw-r--r-- 1 root root 0 2020-05-11 14:48 b.txt
-rw-r--r-- 1 root root 0 2020-05-11 14:36 c.txt
[[email protected] ~/git_test]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	renamed:    a.log -> a.txt
#

#2. 将修改之后的文件提交到本地仓库 

[[email protected] ~/git_test]# git commit  -m "rename a.log a.txt"
[master 51104b9] rename a.log a.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename a.log => a.txt (100%)
[[email protected] ~/git_test]# git status
# On branch master
nothing to commit, working directory clean