代码管理利器--修改git历史提交消息

git常用重置命令


# 直接指定提交信息
git commit -m <msg>

# 通常提交的时候都需要先 add,而 -a 参数能将所有已跟踪文件的更新自动提交到暂存区
# 注:新创建的没有被跟踪文件使用 -a 参数是无效的
git commit -a

# 将新的修改追加到上一个提交中
git commit --amend

# 提交一个空内容,在没有任何修改又想增加一个提交时可用
git commit --allow-empty

# 忽略 pre-commit hook 脚本
git commit -n
git commit --no-verify

# 在提交的时候指定作者信息
git commit --author="name <email@mail.com>" ...

# 修改提交的作者信息
git commit --amend --author="name <email@mail.com>"

# 在全局的配置改变了之后,修改某个作者信息
git commit --amend --reset-author --no-edit

# 修改前一个Commit的提交内容但是不修改提交信息
git add --all && git commit --amend --no-edit

如何修改git commit的author信息

git commit --amend --author="name <email@mail.com>"

或者 在全局的配置改变

git config global user.name 'username'
git config global user.email 'example@mail.com'
git commit --amend --reset-author --no-edit

注意某些情况我们的 committer 和author 可能会不一致

修改历史提交消息

某些时候,我们已经提交过好多次记录并且已经push到了远程分支
但是发现之前的一些信息弄错了,这是可以通过rebase操作,来修改历史提交信息

$ git rebase -i HEAD~3  

输出如下

pick 1 commit 1
pick 2 commit 2
pick 3 commit 3

要修改哪个,就把那行的pick改为edit,然后退出。

例如想修改commit 1的author,光标移到第一个pick,按i键进入INSERT模式,把pick改为edit:

edit 1 commit 1
pick 2 commit 2
pick 3 commit 3

...
-- INSERT --

然后按esc键,退出INSERT模式,输入:wq退出,

这时可以看到提示,可以修改commit 1的信息了

通过 amend命令重置用户信息:

  $ git commit --amend --reset-author

会出现commit 1的提交记录及注释内容,可进入INSERT模式修改注释,:wq退出。

这时再查看提交历史,发现commit 1的author已经变成b(b@email.com)了,且是最新一次记录。

通过continue命令回到正常状态:

 $ git rebase --continue

如何修改Github已经提交的commit里的提交者用户名和提交者邮箱

由于某种原因,需要删除或者更改提交者committer用户名和提交者邮箱地址,应该如何操作呢?

下面是解决方案:
复制下面的脚本到项目根目录下(即有.git文件夹的同级目录)

  #!/bin/sh
 
  git filter-branch --env-filter '
 
  OLD_EMAIL="your-old-email@example.com"
  CORRECT_NAME="Your Correct Name"
  CORRECT_EMAIL="your-correct-email@example.com"
 
  if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
  then
  export GIT_COMMITTER_NAME="$CORRECT_NAME"
  export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
  fi
  if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
  then
  export GIT_AUTHOR_NAME="$CORRECT_NAME"
  export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
  fi
  ' --tag-name-filter cat -- --branches --tags

注意替换重要的变量:

  • 之前的用户名,邮箱
  • 新的用户名,邮箱
git push --force --tags origin ‘refs/heads/*’

设置多个远程库

git remote add 新的远程库名  远程库地址

比如把远程仓库设置为github和gitee:

$ git remote add gitee https://gitee.com/jimmyfengqi/drawword-cloud
$ git remote add github https://github.com/JimyFengqi/drawWordcloud 
$ git remote -v
gitee	https://gitee.com/jimmyfengqi/drawword-cloud (fetch)
gitee	https://gitee.com/jimmyfengqi/drawword-cloud (push)
github https://github.com/JimyFengqi/drawWordcloud (fetch)
github https://github.com/JimyFengqi/drawWordcloud (push)

然后向远程库推送代码时:

$ git push gitee master 
$ git push github master 

这样就能把代码推到两个远程库了
也可以通过其他操作将gitee的内容推到github上

posted @ 2021-10-17 00:09  枫奇丶宛南  阅读(327)  评论(0)    收藏  举报