简体   繁体   中英

Remove first commit in repo

I hardcorded some credentials in the first commit of a repo, here is git log output:

commit 7958f03d51b0c3852bffc43bd169a121314dafca (HEAD -> master)
Author: Alexander Mills <alex@alexs-mac.local>
Date:   Tue May 7 13:14:45 2019 -0700

    modify dockerfile

commit d16acc2b2331e5afdc51d87d05b3aeb01397d4ef (origin/master)
Author: Alexander Mills <alex@alexs-mac.local>
Date:   Thu May 2 11:48:14 2019 -0700

    init

how can I remove the first commit ("init")?

I tried:

git reset --soft HEAD~1

but that will only allow me to reset to the first commit. If I try git rebase -i HEAD~1 , I get:

pick 7958f03 modify dockerfile

# Rebase d16acc2..7958f03 onto d16acc2 (1 command)
#  ...

So there is no obvious way to remove that first commit with I either git reset or git rebase ...is there a way to git rid of the first commit somehow?

Note : I could blast the .git repo folder, but as an exercise, some people might have many more commits than 2 and want to remove the first, or first 5, commits from a repo to get rid of hardcoded creds etc.

I would squash first two commits (meld into a single commit)

git rebase -i --root

Change the pick of the second line by s or squash , save the file

The result is a single commit resulting in the state of your current second commit. I asume you have already removed credentials in second commit.

In case you already pushed to remote, you have to force push with git push --force .

From https://git-scm.com/docs/git-rebase :

--root

Rebase all commits reachable from , instead of limiting them with an . This allows you to rebase the root commit(s) on a branch. When used with --onto, it will skip changes already contained in (instead of ) whereas without --onto it will operate on every change. When used together with both --onto and --preserve-merges, all root commits will be rewritten to have as parent instead.

-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).

The commit list format can be changed by setting the configuration option rebase.instructionFormat. A customized instruction format will automatically have the long commit hash prepended to the format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM