Sunday, October 4, 2015

Git tutorial - Undoing changes

Git revert command undoes a committed snapshot. But instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content . This prevents git from losing the history. 

git revert

Git revert should be used if we want to revert entire commit from the project history. This can be useful for example when a bug was introduced by a particular commit. Instead of going ahead and fixing it manually, the revert command can be used to revert this change automatically. 

git revert undoes only the one commit that it is asked to revert. It does not revert all of the subsequent commit that occurred after this. In git, this is called git reset. in git terminology, git revert is a safe way to undo a change while git reset is a dangerous method. when reset is done, there is no way to get back the original content, it removes all of them permanently unlike revert. 

However, git reset is a versatile command in many ways. The git reset can be used to revert the changes in the staging area and the working directory. In other words, it should be only used to undo the changes in the local directory. 

Some of the usages for the command include the below 

git reset

This command removes the specified file from the staging area, but leaving the working directory unchanged. This upstages the file without overwriting any changes. 

git reset 

reset the staging area to match the most recent commit, but leaving the working directory unchanged. This upstages all files without overwriting any changes, giving the opportunity to re-build the staged snapshot from scratch. 

git reset —-hard 

resets the staging area and the working directory to match the most recent commit. In addition to upstaging the file, this —hard flag tells git to overwrite all the changes in the working directory too. In other words, it removes all the uncommitted changes too . to be used with caution 

git reset move the current branch tip backward to commit. resets the staging area to match, but leaving the working directory alone. All changes made since commit will reside in the working drirectory, which lets us to re-commit the project history using cleaner more atomic snapshots. 

another variant is git reset —hard



References:
https://www.atlassian.com/git/tutorials/undoing-changes/

No comments:

Post a Comment