Home > front end >  git keeps pushing commits that it isn't supposed to
git keeps pushing commits that it isn't supposed to

Time:03-09

$ git log
commit 87da9 (HEAD -> master)
Author: me
Date:   Tue Mar 8 11:34:10 2022  0100

    .gitignore added

commit 2cdaf
Author: me
Date:   Tue Mar 8 01:16:09 2022  0100

    First commit

First commit was a mistake and contains a large file. Then, after adding .gitignore that blocks the file and a directory where it was extracted

$ cat .gitignore 
Big\ File/**
Big\ File.zip

Then .gitignore was refreshed:

$ git rm -r --cached .
...

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    .gitignore
        deleted:    File 1.doc
        deleted:    Big\ File.zip
...
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        File 1.doc
        Good File 1.pdf
        Good File 2.txt

after the refresh files were readded:

$ git add -A
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory

then a new commit was made:

$ git commit -m ".gitignore refreshed"
[master c8a6f46] .gitignore refreshed
 30 files changed, 0 insertions( ), 0 deletions(-)
 delete mode 100644 Big\ File.zip

Everything looks ok (all unneeded files were removed).

$ git status
On branch master
nothing to commit, working tree clean

$ git log
commit c8a6f (HEAD -> master)
Author: me
Date:   Tue Mar 8 11:46:57 2022  0100

    .gitignore refreshed

commit 87da9
Author: me
Date:   Tue Mar 8 11:34:10 2022  0100

    .gitignore added

commit 2cdaf
Author: me
Date:   Tue Mar 8 01:16:09 2022  0100

    First commit

The last commit is not really needed so:

$ git reset --soft 87da9

The first commit might be needed at some point -> push only the second:

$ git push -f -u origin 87da9:refs/heads/master

Looks good, but git thinks it's better to push the first commit as well for some reason and ends up running into the 100MB size limit on github.

How can git be told NOT to push the first commit?

CodePudding user response:

IMHO, because the story is super short and you cannot reset to the "zero commit" because the first commit was the one considered as wrong, I suggest you to:

rm -rf .git
git remote add origin <your.remote.repo.url>
git commit -am "very first commit"
git push -f origin main

CodePudding user response:

What worked:

# move master into another branch
$ git branch -m master old_master
# check 
$ git branch
* old_master
# create a new separate branch separate from the old one
$ git checkout --orphan master
Switched to a new branch 'master'
# rm old branch 
$ git branch -D old_master
Deleted branch old_master (was 2cdaf8a).
# update HEAD to point to it
$ git update-ref -d HEAD
# check
$ git log
fatal: your current branch 'master' does not have any commits yet
# clear cache if needed
$ git rm -r --cached .
# add everything
$ git add .
# commit
$ git commit -m "new branch with fresh history and no big files"
# push 
$ git push -u origin master
  • Related