Often we make mistakes and try to go back to previous status of Git.
Here are some important commands to undo some changes safely.
Discard changes in the working directory:
For example, you just started working after cloning or after some commits. Now, you modified some stuff but want to start fresh and discard what you did. Just do the following,
git checkout HEAD filename
it will restore the file as it was after last commit.
Unstage a file after commit:
You just added a file to the staging area after modification. But, before commit you realized that you don’t want to add that file in this commit. So, you can just unstage that file with the below command:
git reset HEAD filename
Delete a file from GIT repo:
You want to delete a file from your git repo completely. Below command is your help:
git rm filename
Below command will delete from Git Repo, but will keep it in filesystem.
git rm --cached filename
Undo a Git merge that hasn’t been pushed yet
You just merged to your local branch, but have not push to the remote branch yet.
But after merge you found that, the merge has something that you don’t want to upload to remote branch. So, you can undo the merge with below command:
git reset --merge ORIG_HEAD
Undo merge if there is a conflict
After merge there are conflicts and you don’t want to resolve conflicts, but undo merge. Below is the command:
git merge --abort