8

So this question isn't this one: Remove files from Git commit although it's similar.

I'm in a situation where I cannot do:

git reset --soft HEAD~1

because I am at the very first commit.

I'm wondering how I would go about removing files from my commit since I cannot backtrack.

A. L
  • 11,695
  • 23
  • 85
  • 163
  • You can commit them and then, remove it. – Víctor López Oct 24 '17 at 06:13
  • 1
    Possible duplicate of [Change first commit of project with Git?](https://stackoverflow.com/questions/2246208/change-first-commit-of-project-with-git) – Chris Maes Oct 24 '17 at 06:28
  • If you have only one commit and don't have anything else, remove `.git` directory and run `git init` + `git remote add` then commit. – jcubic Jan 01 '22 at 19:49

3 Answers3

7

If it's the most recent commit, you can easily amend it. Just make the changes you want, (delete the file for example), then type:

git commit --amend

Dave Wood
  • 13,143
  • 2
  • 59
  • 67
  • what if I want to keep the files? – A. L Oct 24 '17 at 06:15
  • Temporarily move the files to another location (Desktop for example), they'll appear deleted to git, amend the last commit, it'll be like the files never existed, then copy the files back and you're golden. – Dave Wood Oct 24 '17 at 06:17
  • I tried a `reset --hard` followed by your `git commit --amend`, taking out the big files, but when I force push it, it still pushes 500mb worth of stuff. i also tried a `git diff-tree --no-commit-id --name-only -r` and it doesn't show anything. – A. L Oct 24 '17 at 06:24
  • 4
    It sounds like you're making this overly complicated. It's the first commit. You could even just delete the repo, create a new one and start again. If you can't delete the repo, perhaps create a new blank branch `git checkout --orphan newbranch`, start again, then push that branch. Afterwards you can delete the old branch. – Dave Wood Oct 24 '17 at 06:41
  • 2
    Yeah I probably am making this WAAAAYY over complicated. Reinitialising the git is probably the best bet. – A. L Oct 24 '17 at 08:50
  • 1
    If you want to delete some file from that first but keep on storage, then `git rm --cached unnecessary_file` before doing `git commit --amend --no-edit`. – sal_guy Apr 11 '20 at 16:40
2

As you can see from here, you can do a interactive rebase. You have to set the first commit to be for edit. During the rebase you can remove files with git rm and add new files (with git add). When you are done - git rebase --continue and you will have a new edited commit. If you have a remote branch you can rewrite its history by force pushing your local branch.

  • hmm, I think I screwed up too much, I was using a bit of that before. Would the contents when you run `git reflog` affect your commit sizes? – A. L Oct 24 '17 at 06:27
  • I am not sure what exactly you are asking but `git reflog` is just a reference logs, it will help you recover states of the repo but **should not** affect any commit size. – yanislavgalyov Oct 24 '17 at 06:55
  • 2
    The link you give is dead. The proper command to use to rebase first commit is `git rebase -i --root` – tigrou Aug 06 '21 at 09:38
0

Let's say, you have some commit in your history with hash a1a1a1 (btw. you may get to know your commits' hashes with this command: git log --graph --oneline). This commit has binary files in it which you want (quite fairly) to exclude form your history. This is how to do it:

Firstly, create an empty commit before your very first commit:

git rebase --root --onto $(git commit-tree -m 'root_commit' $(git hash-object -t tree /dev/null))

Let's say, this root commit has got hash of a2a2a2. Note that.

Secondly, delete the files (binaries, etc.) and commit the deletion with this command:

git commit --fixup a1a1a1

Finally, perform the squash:

git rebase -i --autosquash a2a2a2

You're done.

P.S.: This action is to change every hash sum of every commit after the a2a2a2. This is why you can only modify your commit history this deeply only if you're the only developer of this project, or you are in close relations with other members of your team so you can settle it :-)

Václav
  • 430
  • 1
  • 7
  • 22