A. L
A. L

Reputation: 12649

git remove files from very first commit

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.

Upvotes: 9

Views: 7678

Answers (4)

Lars Heinrichs
Lars Heinrichs

Reputation: 1

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

Upvotes: 0

Václav
Václav

Reputation: 460

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

Upvotes: 1

yanislavgalyov
yanislavgalyov

Reputation: 171

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.

Upvotes: 3

Dave Wood
Dave Wood

Reputation: 13333

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

Upvotes: 9

Related Questions