Git Remove Files From Last Add + Commit + Push

Feb 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:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.