I just committed and pushed a change that accidentally added a new line to the end of every file in my repo. Now that my diff is really big, is there anyway I can remove these newlines?
-
The answers given are correct. I want to add that you can write a script which remove trailing newline characters at the end of your file and use the output of `git diff --name-only HEAD^` (assuming you have the commit in question checked out) to get the names of all files you changed in the commit. – Sascha Wolf Sep 23 '14 at 07:06
3 Answers
If that's the only change, you can git reset --hard HEAD^
to move your HEAD
to the previous commit and then git push --force
assuming no one else has pulled yet.
If your change is more than just the newlines, you'll have to reset
, fix up the commit and then push --force
again assuming that no one else has pulled in between.

- 71,383
- 13
- 135
- 169
here try this by using this you can undo your last commit
git reset --soft HEAD~1
then make the changes to your files and commit again.
here refer this for more: How to undo last commit(s) in Git?
-
This will be really dangerous for his git history, cause he already pushed the commit, so coming back will be a damage. – Kamafeather Sep 23 '14 at 09:41
The right and SAFE way
is to do it is to use git-revert
, cause you already pushed the commit to the repository
The wrong and DANGEROUS way
is instead performing a simple git-reset
, like
git reset --soft HEAD~1
or
git reset --hard HEAD^
because they will step back to the previous commit, but when you will commit again you will diverge and will be rewriting you git history; and that should be avoided as much as possible.
Note that there is (almost) always something wrong and smelling when you find yourself using the --force option, or when you try to diverge (take another path and forget the previous. Git doesn't like to forget things)) from pushed commits (in fact the commits will remain orphans and you will rewrite the history creating problems to other repository users).
To do it use
git revert HEAD
This is made exactly for cases like that, and will make a new commit that will put all the files back to the previous commit, undoing the ending line. You need to make a new commit if you want to preserve the integrity of your repository.

- 1
- 1

- 8,663
- 14
- 69
- 99