Git Local Repository Rollback

Git local repository rollback

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed different areas of a git repository through a demo.

https://cloudaffaire.com/git-repository-basic-concepts/

In this blog post, we will discuss how to roll back changes in git repository.

Git local repository rollback:

Demo:

#create a new file file2 and delete file1
echo "name: debjeet" > file2 && echo "fav game: football" >> file2 

#remove the file file1
rm file1

#check git status > file1 deleted and new untracked file2
git status

#add the change to staging
git add . # git add (.)dot or (*)start will add all changes in working tree to staging area

#add the change to local repository
git commit -m "file2 created and file1 deleted"

#check git log
git log -p

#check git status > our working tree, staging area and local repository is consistent
git status

##Undo a working tree change##
vi file2
-------------------
name: debjeet
fav game: tennis
-------------------
:wq

#check git status > working tree is not clean due to change
git status

#get difference between working tree and staging area (fav game changed to tennis in working tree)
git diff

#discard the change in working tree
git checkout -- file2

#check git status > working tree is clean again
git status

#difference between working tree and staging area
git diff

#fav game tennis changed to football again
cat file2

##Undo a staging area change##
vi file2
-------------------
name: debjeet
fav game: cricket
-------------------

#add the change from working tree to staging area
git add file2

#get the differences between working tree and staging area (no change as changes are already added)
git diff

#get the differences between staging area and local repo (change present as file is not committed yet)
git diff --staged

#check git status > changes are ready to get committed
git status

#discard changes in staging area
git reset HEAD file2

#get the differences between staging area and local repo (no change as we discarded the change from staging area)
git diff --staged

#get the differences between working tree and staging area (change present as working tree and staging area is not consistent)
git diff

#discard changes in working tree too
git checkout -- file2

#get the difference > file2 is consistent across working tree, staging area and local repo
git diff --staged
git diff

#check git status > file2 is consistent across working tree, staging area and local repo
git status

#check the content of the file2 > fav game is changed to football again
cat file2

##Undo local repository change##
#check logs
git log --oneline

#get the commit id for the commit where file1 was added and note it down
#dc8db78 file1 added

#check working tree files (file1 does not exist)
ls

#our git repo is consistent
git status

#restore the file file1 from local repository (file1 will be restored to working tree and staging area)
git checkout dc8db78 -- file1

#check working tree files (file1 restored)
ls

#check the status > file1 is ready for commit again
git status

#finally commit the changes
git commit -m "file1 restored"

#cleanup
cd .. && rm -rf mygit

Hope you have enjoyed this article. In the next blog post, we will discuss git branching.

To get more details on Git, please refer below git documentation

https://git-scm.com/doc

 

Leave a Reply

Close Menu