1. 修改还未push的记录的注释
- $ > git commit --amend
修改后保存退出。
2. 修改已push到远端的记录的注释
对于最后一次提交的注释,可以使用下面的命令修改:
- $ > git commit --amend
执行后会进入修改页面修改注释信息,修改后:wq
保存退出。
注:也可以使用
git commit --amend -m "new comments"
来直接修改commit的注释。
再使用git push --force-with-lease origin master
重新推送:
- $ > git push --force-with-lease origin master
- Counting objects: 31, done.
- Delta compression using up to 4 threads.
- Compressing objects: 100% (26/26), done.
- Writing objects: 100% (31/31), 19.10 KiB | 0 bytes/s, done.
- Total 31 (delta 21), reused 0 (delta 0)
- remote: Resolving deltas: 100% (21/21), completed with 19 local objects.
- To github.com:LennonChin/Git-Test.git
- + 31b9f4e...cdac7db master -> master (forced update)
注:如果其他人已经下载或改动,则尝试使用下面的方法重置,但需要注意的是,
reset hard
操作会将提交的代码硬重置,即会丢弃已提交的代码,需谨慎使用:
- $ > git fetch origin
- $ > git reset --hard origin/master
3. 修改更早之前的历史修改
可以使用rebase操作来修改之前已经push的提交的注释,使用如下命令:
- $ > git rebase -i HEAD~3
假设要修改当前版本往前三次版本的状态,四次就把HEAD~3
改成HEAD~4
,以此类推;假设要从第一个版本开始修改,可以直接使用--root
- $ > git rebase -i --root
然后会进入编辑页面,显示类似如下内容:
- pick 49e85bb feat(Project): init project and first commit.
- pick 6677c12 chore(Project): add .gitignore file.
- pick 0ab7d4e feat(Principle): Open Closed Principle Tests
- pick 59fa993 feat(Principle): Dependence Inversion Principle Tests
- pick 0b16342 feat(Principle): Single Responsibility Principle Tests
- pick 7a000e0 feat(Principle): Interface Segregation Principle Tests
- @ Rebase 7a000e0 onto 287e5ef (6 commands)
- @
- @ Commands:
- @ p, pick = use commit
- @ r, reword = use commit, but edit the commit message
- @ e, edit = use commit, but stop for amending
- @ s, squash = use commit, but meld into previous commit
- @ f, fixup = like "squash", but discard this commit's log message
- @ x, exec = run command (the rest of the line) using shell
- @ d, drop = remove commit
- @
- @ 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
想要改哪个版本,就把那个版本前面的pick
改成edit
,其他的内容不要修改:
- pick 49e85bb feat(Project): init project and first commit.
- pick 6677c12 chore(Project): add .gitignore file.
- edit 0ab7d4e feat(Principle): Open Closed Principle Tests
- edit 59fa993 feat(Principle): Dependence Inversion Principle Tests
- edit 0b16342 feat(Principle): Single Responsibility Principle Tests
- pick 7a000e0 feat(Principle): Interface Segregation Principle Tests
- @ Rebase 7a000e0 onto 287e5ef (6 commands)
- @
- @ Commands:
- @ p, pick = use commit
- @ r, reword = use commit, but edit the commit message
- @ e, edit = use commit, but stop for amending
- @ s, squash = use commit, but meld into previous commit
- @ f, fixup = like "squash", but discard this commit's log message
- @ x, exec = run command (the rest of the line) using shell
- @ d, drop = remove commit
- @
- @ 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
保存退出,然后执行下面的命令:
- $ > git commit --amend
就会进入编辑页面,这时候可以修改之前提交时写的注释了;修改完后保存并退出,控制台回打印下面的内容:
- $ > git commit --amend
- [detached HEAD baa4a35] feat(Principle): Open Closed Principle Tests
- Date: Tue Oct 2 14:37:45 2018 +0800
- 20 files changed, 1442 insertions(+)
此时只需要执行git rebase --continue
:
- $ > git rebase --continue
- Stopped at 59fa993... feat(Principle): Dependence Inversion Principle Tests
- You can amend the commit now, with
- git commit --amend
- Once you are satisfied with your changes, run
- git rebase --continue
此时如果还有更多需要修改的注释,需要继续执行git commit --amend
,这样依次重复,就可以将之前标为edit
的版本的注释依次修改。
在修改完所有需要修改的注释后,重新push即可,此时需要强制提交:
- $ > git push -f origin master
4. 压缩掉不需要的版本
压缩版本与上面的修改历史注释差不多,也是使用rebase操作,但此时则需要将需要压缩掉哪个版本的pick
改成squash
,然后保存退出,会出现编辑提交注释的编辑页面,写好新的注释后保存退出,即可将标记为squash
的版本压缩为刚刚编写的新的注释所对应的新版本。
5. 删除某次提交
同样的,我们可以将某个版本的pick
改成drop
,然后保存退出,会出现编辑提交注释的编辑页面,写好新的注释后保存退出,即可将标记为drop
的版本强制删除,然后使用git push -f origin master
推送到远程库即可。
推荐阅读
Java多线程 46 - ScheduledThreadPoolExecutor详解(2)
ScheduledThreadPoolExecutor用于执行周期性或延时性的定时任务,它是在ThreadPoolExe...