221 - 《Git 技巧》

发布于 2022年11月25日

以下一些 git 技巧,结合了文末参考的两篇文章。是从文中摘录的常用的点,有些可以提效,有些可以救命。如果这些不能满足你的求知欲,可以阅读原文以及原文下大量的参考资料,知识个人觉得必要性不大,git 学会常用场景的解 + GUI(我用 SourceTree)就够了。

1、设置错误的远程库怎么办?

$ git remote -v
$ git remote set-url origin {{url}}

2、Github Fork 的项目如何更新源项目更新?

$ git remote add upstream {{url}}
# 1
$ git fetch upstream
$ git merge upstream/master
# 或者 2
$ git pull upstream main

3、提交信息写错了怎么办?

$ git commit --amend --only
$ git commit --amend --only -m 'xxx'

4、提交时用了错误的用户名或邮箱?(单次)

$ git commit --amend --no-edit --author "yunqian <yunqian@alibaba-inc.com>"

或者

$ git config --global author.name yunqian
$ git config --global author.email yunqian@alibaba-inc.com
$ git commit --amend --reset-author --no-edit

5、最后一次提交不想要了?

如果已推送。

$ git reset HEAD^ --hard
$ git push --force-with-lease [remote] [branch]

如果还没推送。

$ git reset --soft HEAD@{1}

6、提交内容需要修改怎么办?比如提交了敏感信息。

修改或删除,

# 编辑后 add
$ git add sensitive_file
# 或删除
$ git rm sensitive_file
# 或

内容预览已结束

此内容需要会员权限。请先登录以查看完整内容。