4

解决git提交敏感信息(回退git版本库到某一个commit)

 2 years ago
source link: https://www.linuxzen.com/jie-jue-gitti-jiao-min-gan-xin-xi-hui-tui-gitban-ben-ku-dao-mou-yi-ge-commit.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

git是一个很好的版本库, 现在很多人用它, 并在github上创建项目, 相信大家都有过将敏感信息提交版本的经历, 如何删除? 好像只有删除版本库来解决, 其实我们可以通过回退版本库删除相应的commit来将提交的敏感信息去掉.

备份本地代码

首先我们将本地代码的更改备份一下, 以防丢失更改

回退本地代码的commit

备份完数据, 我们就可以先回退本地的版本库

git reset --hard HEAD~1        # 回退到上一次的提交, 如果是上n次就将1改成对应的数字

回退远端版本库

接下来如果你直接提交会发现提交不了, 说远端做了更改需要先pull一下, 如果pull咱们就白白做上面的操作, 所以我们可以在别的分支操作

新建一个分支, 并提交

git checkout -b temp
git push origin temp:temp

重建主分支

下面我们可以删除并重建主分支, 如果是github的话需要将Default Branch切换到别的分支(项目主页->Settings即可看到)

git push origin --delete master   # 删除远端主分支
git branch -d master              # 删除本地主分支
git checkout -b master            # 新建主分支并切换到主分支
git push origin master            # 提交主分支

这样我们就删除之前提交的敏感信息(如果是github现在就可以把默认分支切换到 master)

删除临时分支

git branch -d temp
git push origin --delete temp

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK