Git: Remove changes

You may have to retrace your steps, want to cancel the changes to our project.

According to the stage of development, there are several ways to undo your changes using git.

Introduction

First of all it is good to understand to what state we are at the present moment:

  • Unstaged: we changed locally, but we have not yet added the changes using git add
  • Staged: added to the repository using git add
  • Committed: the file has been committed to the local repository using git commit

Git remove unstaged / untracked changes

Changes or additions have not yet been added to the staged tree.
You can proceed with:

Remove all local changes, saving them for possible use later (put the changes aside):

git stash

 

Remove local changes, saving them in a file:

git checkout -- filename

 

Remove forever:

git reset --hard

Git remove staged changes

You can remove all your changes from the current commit, but keep them in your project:

git reset

 

Or you can undo the changes to a single file

git reset HEAD filename

 

Undo the changes, but save them for later

git stash

 

Remove all the changes forever (as in unstaged changes):

git reset --hard

 

Git remove committed changes

Until you push your changes, you have control over what you did without public shame.

Undo changes in a particular commit:

git revert commit-id

 

Undo changes in to a file in a commit, keeping them in staged state:

git checkout commit-id filename

 

Undo changes in a file in a commit, keeping them in an unstaged state:

git reset commit-id filename

Doubts? Feel free to leave a comment!