F:\GitTest\demo1 (master -> origin) λ 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: index2.html
Untracked files: (use "git add <file>..." to include in what will be committed)
index02.html
no changes added to commit (use "git add" and/or "git commit -a")
从上面的显示可以看出,在工作目录中重命名是先删除文件,再创建新文件。然后还需要执行以下命令: git add index02.html git rm index2.html 删除暂存区中的文件 执行 git status 打印如下: λ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
renamed: index2.html -> index02.html
把暂存区中所有的修改还原:
1
git reset --hard
上面的更改文件名的操作,可以使用下面的命令直接修改暂存区:
1 2 3 4 5 6 7 8 9 10 11
git mv index.html index02.html
此时再执行 git status,结果如下: λ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
λ git checkout febad01c1b Note: checking out 'febad01c1b'.
You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:
# Rebase 683cbac..a0b86da onto 683cbac (2 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Author: wz <wyzane1207@163.com> # Date: Tue Jun 23 23:02:37 2020 +0800 # # interactive rebase in progress; onto 683cbac # Last command done (1 command done): # reword f5b4cb5 修改文件名2 # Next command to do (1 remaining command): # pick a0b86da 添加用户页面2 # You are currently editing a commit while rebasing branch 'master' on '683cbac'. # # Changes to be committed: # renamed: index2.html -> index02.html
这时我们修改完 msg 后保存即可。
合并commit
对其他 commit 的 msg 修改时,需要用到 git rebase 命令。 首先我们要确定待修改commit的上一个commitID,以这个commit为基础。然后执行 git rebase -i 上一个commitID,会进入一个编辑页面,内容如下:
# Rebase 28f3903..8d1455b onto 28f3903 (4 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
# This is a combination of 3 commits. # This is the 1st commit message:
add index
# This is the commit message #2:
update
# This is the commit message #3:
修改文件名2
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Author: wz <wyzane1207@163.com> # Date: Tue Jun 23 22:41:51 2020 +0800 # # interactive rebase in progress; onto 28f3903 # Last commands done (3 commands done): # squash 683cbac update # squash 4a558f9 修改文件名2 # Next command to do (1 remaining command): # pick 8d1455b 添加用户页面2 # You are currently rebasing branch 'master' on '28f3903'. # # Changes to be committed: # new file: index.html # new file: index02.html