git remove files from very first commit
Asked Answered
git
S

4

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.

Solution answered 24/10, 2017 at 6:10 Comment(3)
You can commit them and then, remove it.Halfback
Possible duplicate of Change first commit of project with Git?Gonorrhea
If you have only one commit and don't have anything else, remove .git directory and run git init + git remote add then commit.Heehaw
S
8

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

Sunsunbaked answered 24/10, 2017 at 6:13 Comment(6)
what if I want to keep the files?Solution
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.Sunsunbaked
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.Solution
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.Sunsunbaked
Yeah I probably am making this WAAAAYY over complicated. Reinitialising the git is probably the best bet.Solution
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.Cartierbresson
S
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.

Stony answered 24/10, 2017 at 6:25 Comment(3)
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?Solution
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.Stony
The link you give is dead. The proper command to use to rebase first commit is git rebase -i --rootBaryram
D
1

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

Dogeared answered 26/6, 2023 at 11:23 Comment(0)
C
0

For Google Searchists:

I had the same problem. After trying a lot of different stuff, I ended up just deleting the git repository and recreating it.

Remove the .git folder which stores the commits etc

rm -rf .git

create an empty repository

git init

adjust your .gitignore to ignore the files you dont want or remove the files.

Stage everything

git add .

Check if your files are no longer included

git status

create your new, first commit

git commit -m "Initial commit"

because we removed the repository earlier, we lost the connection to our remote (github, gitlab, ...) repository. re-add your remote repository, e.g.

git remote add origin [email protected]:my_username/my_repo

force-push everything with recreating the branch. Adjust the branch name to be your actual branch name, otherwise you just create a new branch and the files you wanted to remove are still on your old branch.

git push --set-upstream origin master -f
Conquistador answered 29/7 at 15:35 Comment(1)
Please, please never suggest to remove the .git folder; always suggest to rename it. Otherwise, you may leave Git beginners behind without a backup who think that removing the .git folder is a good idea.Kerf

© 2022 - 2024 — McMap. All rights reserved.