最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[未解决]删除git之前某次提交的历史

Git crifan 2994浏览 0评论

对于一个git仓库,

之前某次的提交,包含了点个人信息

注意到这点后,立刻就去清除掉了,然后重新提交,去掉个人信息

但是,commit历史中,却还是可以看到之前改动中的个人信息的

现在想要:

去掉之前那次提交的历史

但是同时确保代码是当前后来修改后的,最新版本的代码

git remove history commit

github – Make the current commit the only (initial) commit in a Git repository? – Stack Overflow

Git – Rewriting History

Removing selected commit log entries for a Git repository – Stack Overflow

Remove sensitive data – User Documentation

17. Removing a commit from a branch

git awsome-ness [git rebase –interactive] – MadBlog

git-rebase(1)

“-i

–interactive

Make a list of the commits which are about to be rebased. Let the user edit that list before rebasing. This mode can also be used to split commits (see SPLITTING COMMITS below)."

所以去试试:

git rebase -i HEAD~3

其中的3,指的是:

从最新的(HEAD)往前数,共3个commit,去编辑:

icrifandeMacBook-Pro:crifanLib crifan$ git rebase -i HEAD~3

pick 3e133ad 1.add isToday update Year/Month/Day/Hour/Minute/Second \r\n2.update drawBadgeView3.update CrifanLibDemo

pick 858a954 1. add isAllDigitOrLetter 2. add saveLastLogined, lastIsLogined 3. add CrifanLibHttp.swift 4. add CrifanLibHttpDemo.swift

pick 437a443 removed person info

# Rebase 2cb8a4a..437a443 onto 2cb8a4a (3 command(s))

#

# 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

~                                                                                                                       

~                                                                                                                       

~                                                                                                                       

~                                                                                                                       

~                                                                                                                       

~                                                                                                                       

~                                                                                                                       

~                                                                                                                       

~                                                                                                                       

~                                                                                                                      

"~/dev/dev_root/crifan/CrifanLib/crifanLib/.git/rebase-merge/git-rebase-todo" 22L, 926C

根据提示去试试:

把此处想要删除-》和对应的commit的内容保留,合并到之前一次的commit

-》此处即把:

pick 858a954 1. add isAllDigitOrLetter 2. add saveLastLogined, lastIsLogined 3. add CrifanLibHttp.swift 4. add CrifanLibHttpDemo.swift

改为:

squash 858a954 1. add isAllDigitOrLetter 2. add saveLastLogined, lastIsLogined 3. add CrifanLibHttp.swift 4. add CrifanLibHttpDemo.swift

即可:

但是同时发现,这样就丢失了那次commit的备注信息了

所以去把前一次的备注信息改掉

把前一次用reword,然后修改commit message

reword 3e133ad 1.add isToday update Year/Month/Day/Hour/Minute/Second 2.update drawBadgeView 3.update CrifanLibDemo 1. add isAllDigitOrLetter 2. add saveLastLogined, lastIsLogined 3. add CrifanLibHttp.swift 4. add CrifanLibHttpDemo.swift

squash 858a954 1. add isAllDigitOrLetter 2. add saveLastLogined, lastIsLogined 3. add CrifanLibHttp.swift 4. add CrifanLibHttpDemo.swift

pick 437a443 removed person info

如图:

然后跳出:

让编辑信息:

终于可以了:

再去查看log:

果然是了。

不过,突然发现,好像合并错了:

应该是最后一次和上面一次的。。。

所以再去重来一次:

licrifandeMacBook-Pro:crifanLib crifan$ git rebase -i HEAD~2

不过出现:

licrifandeMacBook-Pro:crifanLib crifan$ git rebase -i HEAD~2

Successfully rebased and updated refs/heads/master.

licrifandeMacBook-Pro:crifanLib crifan$ git status

On branch master

Your branch and ‘origin/master’ have diverged,

and have 2 and 3 different commits each, respectively.

  (use "git pull" to merge the remote branch into yours)

nothing to commit, working directory clean

licrifandeMacBook-Pro:crifanLib crifan$ git push

To https://github.com/crifan/crifanLib.git

! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to ‘https://github.com/crifan/crifanLib.git’

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: ‘git pull …’) before pushing again.

hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.

licrifandeMacBook-Pro:crifanLib crifan$ git pull

Merge made by the ‘recursive’ strategy.

licrifandeMacBook-Pro:crifanLib crifan$ git stash

No local changes to save

licrifandeMacBook-Pro:crifanLib crifan$ git status

On branch master

Your branch is ahead of ‘origin/master’ by 3 commits.

  (use "git push" to publish your local commits)

nothing to commit, working directory clean

licrifandeMacBook-Pro:crifanLib crifan$ git pull

Already up-to-date.

licrifandeMacBook-Pro:crifanLib crifan$

licrifandeMacBook-Pro:crifanLib crifan$

licrifandeMacBook-Pro:crifanLib crifan$

licrifandeMacBook-Pro:crifanLib crifan$ git status

On branch master

Your branch is ahead of ‘origin/master’ by 3 commits.

  (use "git push" to publish your local commits)

nothing to commit, working directory clean

licrifandeMacBook-Pro:crifanLib crifan$ git

所以要先去:

[已解决]git中如何丢弃本地的改动

然后继续重新去:

licrifandeMacBook-Pro:crifanLib crifan$ git log

commit 01b597d7779f379fa801588569620c42ccb39c70

Merge: 66616ed 437a443

Author: crifan <[email protected]>

Date:   Tue Jun 7 20:27:35 2016 +0800

    Merge branch ‘master’ of https://github.com/crifan/crifanLib

commit 66616edad41d0610e97759179fb5bfb00512631c

Author: crifan <[email protected]>

Date:   Tue Jun 7 15:49:25 2016 +0800

    removed person info

commit d48e26af8ee6eea369b291ac6ab7ae01fc43ce03

Author: crifan <[email protected]>

Date:   Thu Jun 2 22:52:04 2016 +0800

    1. add isToday update Year/Month/Day/Hour/Minute/Second

    2. update drawBadgeView3.update CrifanLibDemo

    3. add isAllDigitOrLetter

    4. add saveLastLogined, lastIsLogined

    5. add CrifanLibHttp.swift

    6. add CrifanLibHttpDemo.swift

commit 437a4430c64bafda7edf48ceee222a134a86c784

Author: crifan <[email protected]>

Date:   Tue Jun 7 15:49:25 2016 +0800

    removed person info

commit 858a9540ff3984ff39aa09817dcaa82f61030c08

然后去:

git rebase -i HEAD~7

最后改为:

licrifandeMacBook-Pro:crifanLib crifan$ git rebase -i HEAD~7

[detached HEAD f22854b] 1. add isAllDigitOrLetter 2. add saveLastLogined, lastIsLogined 3. add CrifanLibHttp.swift 4. add CrifanLibHttpDemo.swift

Date: Thu Jun 2 22:52:04 2016 +0800

4 files changed, 439 insertions(+), 23 deletions(-)

create mode 100644 swift/CrifanLibHttp.swift

create mode 100644 swift/CrifanLibHttpDemo.swift

Successfully rebased and updated refs/heads/master.

licrifandeMacBook-Pro:crifanLib crifan$

然后再去看看状态:

licrifandeMacBook-Pro:crifanLib crifan$ git status

On branch master

Your branch and ‘origin/master’ have diverged,

and have 1 and 3 different commits each, respectively.

  (use "git pull" to merge the remote branch into yours)

nothing to commit, working directory clean

licrifandeMacBook-Pro:crifanLib crifan$ git pull

fatal: unable to access ‘https://github.com/crifan/crifanLib.git/’: Empty reply from server

licrifandeMacBook-Pro:crifanLib crifan$ git push

To https://github.com/crifan/crifanLib.git

! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to ‘https://github.com/crifan/crifanLib.git’

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: ‘git pull …’) before pushing again.

hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.

最后去git pull:

licrifandeMacBook-Pro:crifanLib crifan$ git pull

Merge made by the ‘recursive’ strategy.

licrifandeMacBook-Pro:crifanLib crifan$ git status

On branch master

Your branch is ahead of ‘origin/master’ by 2 commits.

  (use "git push" to publish your local commits)

nothing to commit, working directory clean

licrifandeMacBook-Pro:crifanLib crifan$ git push

Counting objects: 2, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (2/2), 473 bytes | 0 bytes/s, done.

Total 2 (delta 0), reused 0 (delta 0)

To https://github.com/crifan/crifanLib.git

   437a443..9fe88bf  master -> master

licrifandeMacBook-Pro:crifanLib crifan$ git status

On branch master

Your branch is up-to-date with ‘origin/master’.

nothing to commit, working directory clean

最终,再去看commit 的log:

结果:

却还是存在对应的历史啊。。。

妹的。

估计是:

Don’t use git-rebase on public (remote) commits.

这句。

-》

rebates只适合本地

没法用于远程的git端???

licrifandeMacBook-Pro:crifanLib crifan$ git rebase –onto 9fe88bf^ 9fe88bf

First, rewinding head to replay your work on top of it…

Git – Rewriting History

“Don’t include any commit you’ve already pushed to a central server – doing so will confuse other developers by providing an alternate version of the same change."

好像是:

不能应用于远端??

算了,放弃。不弄了。

哪天有空,再弄吧。。。

转载请注明:在路上 » [未解决]删除git之前某次提交的历史

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.159 seconds, using 22.04MB memory