Sunday, June 14, 2020

Revoke/Revert Git Commits from Remote or Local Repository || GIT Users must know

Developers use source control management tools like GIT or SVN daily and commits there code frequently(frequent commit is the good practice though) to avoid losing the code.
But there may be a circumstances we tend to commit the changes which is not relevant or wrong commits. In order to overcome this scenario GIT provides the powerful commands to revoke the changes made locally or even pushed to remote.
Lets see the commands to rollback the GIT commits from Local and Remote branches.
Below are the typical GIT commands to perform the GIT commits

#incrementally add the changes to GIT index before commit.
git add <filename> or git add * 

#Commit the added index to local repository
git commit -m "Good developers write meaningful commit messages"

#Logs all the commit logs of the repo
git logs

#List changes or index added on your workspace
git reflog


#Remove files added to index before commit
git rm <filename>

#Undo the Local commits
git reset --soft HEAD^     # Use --soft if you want to keep your changes
git reset --hard HEAD^     # Use --hard if you don't care about keeping the changes you made

So far we had seen the local commits and roll backing the changes, Now if the Changes are pushed to remote repository and undo the changes Lets wee whats needs to be followed.

#You are reverting the changes from the public repository , You should be very careful when executing the below command.. Below command is forcing the old reference as the recent change in the GIT.

git push -f origin last_known_good_commit:branch_name

Diagram depicts the GIT commit to local and remote workflow


No comments: