Ways to undo wrong Git operations

  sonic0002        2018-07-07 03:28:21       3,468        0    

While using Git to version code, programmers would inevitably perform some invalid operations which are not expected. Sometimes it's difficult to deal with this kind of awkward situations. If the programmer chooses to undo the operation, the programmer needs to bear the risk of deleting something which is not supposed to be deleted if the undo is done improperly. If the programmer leaves it as is, the file needs to be updated again manually with a new commit. In this post, we will try to provide advice on how to properly undo wrong operations performed using Git for some scenarios. 

Git add wrong file

In this scenario, the programmer wrongly adds some file s/he doesn't mean to with git add command. For example, some auto-generated files are added while creating a new project. See below:

In above screenshot, the .idea folder should not be added. If want to untrack the .idea folder, run below command.

git reset .idea

If want to untrack all files, just type git reset will do. The result of executing this command will be.

The .idea folder is untracked now. 

Sometimes you may see below error while running the git reset command.

fatal: Failed to resolve 'HEAD' as a valid ref

This error indicates that the git commit command has never been run in the local git repository which causes the missing of HEAD reference. In this case, below command can be issued to undo the operation.

git rm --cached [file] # File
git rm -r --cached . # Folder and files

To avoid the git add wrong doing, you can add the files which should not be added to the .gitignore file. Also try to add the specific files instead of using wildcards like .(dot).

Git commit wrong file

Somehow an unwanted file change is noticed only after the file has already been committed with git commit command. For example, below file is updated and the change gets committed.

But later you realized that you don't want this commit. How do you undo this commit? There are two cases.

1. If only the commit needs to be undone but the file change is still there, run below command

git reset HEAD~1

This command will reset the code one commit earlier before HEAD ref.

The output will be:

2. If the commit needs to be undone and also the changes to be discarded, run below.

git reset --hard HEAD~1

The end result looks like

How to avoid this kind of git commit errors? Try to run git status and git diff to make sure the changes are what one wants to commit.

Git branch not needed anymore

In case one branch is not needed anymore, you just need to delete the branch with below command.

git branch -d branch_name

If the branch has been pushed to remote origin and it needs to be deleted, issue below command.

git push origin --delete test # Delete remote branch
git checkout master
git branch -d test # Delete local branch
# git branch -D test # For deleting branch which has uncommited files

Conclusion

It's not scary to make mistakes, the scary part is one doesn't know how to recover from the mistake. So be cautious but also be prepared.

Reference

https://www.toutiao.com/a6575045501211640324/

GIT  GIT COMMIT  GIT RESET 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Hotfix on production environment