git push trying to push files not listed in git ls-files

后端 未结 3 1376
遥遥无期
遥遥无期 2021-01-19 15:54

I accidentally committed an unnecessary large subfolder xxxxx in my repo, and when I realized this while pushing, I stopped the push midway through.

Then I removed a

相关标签:
3条回答
  • 2021-01-19 16:41

    git push does not push files, it pushes commits. Specifically, it pushes commits that you have that they don't (whoever "they" are—the remote to which you are pushing). They are complaining that at least one of your commits contains at least one large file.

    You'll need to push some different commits, so that the commits you're pushing don't contain the large file.

    As a general rule, the way to do this is to take each "good" commit intact, copying it to a new branch, and for each "bad" commit, extract the good parts, remove the bad parts, and make a new commit out of the result. For instance, suppose this is a drawing of the commits the remote has:

    A <- B <- C <- D    <-- origin/master
    

    Suppose you've added three commits but there's a problem in the middle one:

    A - B - C - D       <-- origin/master
                 \
                  E - F - G   <-- master
    

    When you run git push your git sends commits E, F, and G, and the remote complains because F has file(s) it should not. To fix this you can simply copy E to a new branch—we'll call the copy E'—then copy-but-fix F to make F', then copy G to make G'. Let's also rename the old ("bad") master; in fact, let's just drop the name entirely, so that we have this instead:

                  E' - F' - G'   <-- master
                 /
    A - B - C - D       <-- origin/master
                 \
                  E - F - G      [abandoned]
    

    The git command that does this copying is git rebase. Normally it just copies straight through; you want it instead to copy E, but then stop and let you fix F, and then copy G, and the way to get it to do that is to use the -i or --interactive flag.

    In general, you should only copy-while-changing commits that only you have, but fortunately this is exactly the set that git push should be pushing, and whatever branch you're pushing, origin/branch (or whatever the remote name is, if it's not origin) will delimit the commits that the remote has (and other people have).

    Interactive rebase can do a lot more than this, so see the Git Book for instructions. It's also up to you to figure out which of your commit(s) contain which large file(s); for this, git show and git diff are helpful tools.

    0 讨论(0)
  • 2021-01-19 16:44

    Why don't you try to put this folder in gitignore file of git.

    0 讨论(0)
  • 2021-01-19 16:47

    Since the commit, that needs editing is on the head of branch, you probably can get away just with amending it. So do the git rm <what you need> and then git commit --amend

    This will merge changes into most recent commit, instead of creating new one

    0 讨论(0)
提交回复
热议问题