Git Remove Files From Last Add + Commit + Push

February 17, 2020

I make some changes and added a new directory to the local directory and run

git add .
git commit -m "Some changes"
git push origin master

Now, I wanted to:

  • remove the new directory from the git, both local and server
  • keep the other changes I made
git reset --soft HEAD^

NOTE: Run git log -1 and you will see the last commit is “gone”.

NOTE: Run git status and you will see the new directory is still added.

To remove directory which is added by previous git add .

git reset HEAD DIRECTORY_NAME

NOTE: You could remove single file as well

NOTE: Run git status to confirm the directory/files are removed.

Commit again by re-using the same commit message

git commit -c ORIG_HEAD

Push

git push -f origin master

NOTE: Force is required, else it shall show complain of Updates were rejected because the tip of your current branch is behind its remote counterpart.

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.