Jimmy C
Jimmy C

Reputation: 9690

Remove some files from latest commit

Some of my lastly commited files are unfortunately too big for github, so I would like to remove these from the latest commit, making it possible to push the other files. I have tried using git reset --HEAD for this, but it seems to only work if you haven't commited the files yet. Any other ideas?

Upvotes: 2

Views: 75

Answers (1)

TimWolla
TimWolla

Reputation: 32731

This should work for you:

  1. git rm the offending files
  2. git status should show the files as deleted
  3. git commit --amend to amend the last commit, the files should no longer be included
  4. git push

See this transcript:

[root@/tmp/test master]touch b c
[root@/tmp/test master]git add b c
[root@/tmp/test master]git commit -m "Add b and c"
[master 1cd809f] Add b and c
 0 files changed
 create mode 100644 b
 create mode 100644 c
[root@/tmp/test master]git rm c
rm 'c'
[root@/tmp/test master]git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    c
#
[root@/tmp/test master]git commit --amend -m "Add only b"
[master 9c5809e] Add only b
 0 files changed
 create mode 100644 b
[root@/tmp/test master]git diff HEAD^..HEAD
diff --git a/b b/b
new file mode 100644
index 0000000..e69de29

Note: This only works if the offending files were added in the last commit. In other cases refer to Remove sensitive data in the GitHub help, it explains to delete a file from the complete history.

In case this it not wanted either you can try an interactive rebase (git rebase -i <commitish>) and my method above.

Upvotes: 3

Related Questions