如何修改git已提交记录的邮箱

 

有时候,公司提交的代码必须使用公司邮箱,而你误操作,直接把自己个人邮箱提交上去了,此时你就会遇到这样的需求:如何修改git已提交的邮箱?

而这个需求对于新手来说,往往要花费半天的时间才能理解修改过程,简直太傻比了,所以我这里做一个详细的文档来帮助自己和你搞清楚这个流程。尤其要理解变基,它不是一个命令执行就完成了,而是一连串命令的组合。

步骤1:变基

git rebase -i 

执行后,会打开最近一条的提交记录,当然上面的命令可以指定某一条记录,命令是:

git rebase -i "your commit id"

对于sourcetree用户来说,commit id是SHA-1,可以右键某条提交记录,选择菜单"复制SHA-1到剪贴板",如下图:

image-20190513113342522

变基rebase命令执行完成后,会打印类似如下内容:

pick bd81df5 更新API

# Rebase abcb9d0..bd81df5 onto abcb9d0 (1 command)
#
# 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

新手往往会一脸懵逼,不止所错,此时是在rebase的过程中,你需要把pick改为edit,如下:

edit bd81df5 更新API

# Rebase abcb9d0..bd81df5 onto abcb9d0 (1 command)
#
# 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

更改完成后,保存并退出vi编辑器::wq

然后会打印这样的消息:

chengmingdeMacBook-Pro:server cmlanche$ git rebase -i "abcb9d0d1e99cdad25d8d08119e494436b000e59"
Stopped at bd81df5...  更新API
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue
chengmingdeMacBook-Pro:server cmlanche$ 

给大家先科普一下这个amend英文单词,是修改的意思,对我来说好陌生,为啥不用change或者fix之类的。

image-20190513114137949

上面的信息说了,如果你要amend,也就是要修改这个提交的话,那么用

git commit --amend

如果你对这次修改满意的话,就用如下命令结束此次变基

git rebase --continue

重置账户邮箱信息

我们当然要修改啦,那么执行如下命令,重置提交的账户信息:

git commit --amend --author="cmlanche <1204833748@qq.com>" --no-edit

同事,要注意你的sourcetree,出现了新情况!

image-20190513114851415

我们可以看到一个新的提交,并且,邮箱账号都经过了修改,如果你去掉--no-edit还可以修改commit message,也就是图中的"更新API",举栗子吧,我可以继续用amend修改此次变基

git commit --amend --author="cmlanche <1204833748@qq.com>"

image-20190513115132114

保存退出vi编辑器,看sourcetree咋样了:

image-20190513115216969

真的很完美,接下来就是合并了,退出变基。

退出变基

git rebase --continue

在控制台中打印如上命令退出变基,我们看到退出变基也就是使用最新的修改了,就一条分支了。

chengmingdeMacBook-Pro:server cmlanche$ git rebase --continue
Successfully rebased and updated refs/heads/bulma.

image-20190513115442779

最后总结一下

变基真的很有用,他不是一条命令搞定的,是一个过程,就像变成中打开了一个输入流,最后用完你得关闭输入流一样。

通过变基你可以轻松实现提交信息的任意重新修改!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
修改git用户名和邮箱,有两种方法可以实现。 方法一是使用命令来修改git的用户名和邮箱。如果你要修改当前全局的用户名和邮箱,可以使用以下命令: git config --global user.name "你的目标用户名" git config --global user.email "你的目标邮箱名" 如果你只想修改当前项目的用户名和邮箱,可以使用以下命令: git config user.name "你的目标用户名" git config user.email "你的目标邮箱名" 方法二是通过直接修改git的配置文件来进行修改。你可以通过以下步骤来实现: 1. 打开全局的.gitconfig文件,可以通过命令"vi ~/.gitconfig"来打开该文件。 2. 在文件中找到对应的用户名和邮箱字段,直接修改为你希望的用户名和邮箱。 3. 保存文件并退出。 另外,如果你想修改当前项目中的配置文件,该文件位于每个项目的.git目录下。你可以进入该目录,找到config文件进行编辑。如果在该文件中没有对应的用户名和邮箱字段,你可以手动添加。 请注意,修改用户名和邮箱后,你需要重新提交你的更改,才能在提交记录中显示正确的用户名和邮箱。 希望以上信息对你有所帮助!<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [Git 修改用户名以及邮箱](https://blog.csdn.net/risingfine/article/details/83378605)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Git修改用户名和邮箱的方法(附Git常用命令)](https://blog.csdn.net/Sherlooock/article/details/107068965)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值