Advertisement

Responsive Advertisement

Undoing commits and changes in git

 Undoing commits and changes in git:


Lets clearly understand undoing changes from one stahe to other i.e from staging area to working area and commit tree to staging or indexed area ad from remote repo as well

Case 1:Untracked files in working directory

Let us imagine a scenario where you have created files in working directory and added to indes or staging area using git add and if you make any changes to file and see git status the file will be moved to untracked file and modified message will be displayed for the same.

If you modify commited files also same scenario will happen.In these 2 cases if you want to undo changes i.e revert back to your original files which you had in your staging area or commit area you can do so using below cmd.
 
                 $git checkout <filename>

                 $git checkout . (-for all files  in current directory)

Case 2:tracked files in staging area(index area):

If you want to move files from staging are to untracked area then use below command.

        $git reset HEAD <filename>  (For single file)

        $git reset HEAD .  (To move all staged files to working directory)


Note:If you use git rm as shown below then files present in both index area and commited files also move to working directory.

     $git rm --cached <filename> (For single file)

    $git rm -r --cached . (for all files in index area and commited files)


Case 3:Move commited files to staging area:

Here we will use reset but there are three modes i.e --soft,--mixed and hard.We will see then below in detail.Before using these you need to use git log to see all commits.

     $ git log --oneline --graph

Shows all commits in single line like below
$ git log --oneline --graph

* 802a2ab (HEAD -> feature, origin/feature) feature commit
* 7a9ad7f (origin/master, master) version 2 commit
* 98a14be Version 2 commit
* 53a7dcf Version 1.0 commit
* 0a9e448 added files
* bd6903f first commit
If we want to undo any commit i.e if you remove files checked in using a specific commit or 2 commits back (in this cases all other after commits will be removed ) we will use this.head points to latest commit  initially, we need to use HEAD simply and if we want to go back one commit before head use (HEAD^) and 2 commits before head use (HEAD~2) or simply specify the commit id.And if we didnot specify HEAD the latest commit will be invoked.

1)$git reset --hard HEAD 
             -This will move the state of project to commit you specified.There are 2 points to note
  1.   All the files that you have created after that commit will be deleted(They are neither present in your working directory nor in staging area).
  2. All the files which are present in staging area when you perform this reset are also deleted.
            -To get deleted files use below cmd

          $git reset --hard HEAD@{1}

Note:Using above comand you will get deleted files which you commited i.e since you roll back commit files which are deleted in that commit will come back again but the files which are in staging area by the time you perform git hard reset ($git reset --hard HEAD ) will never come back.

So this is not recommended because if you use this, files in present staging area are removed along with files which are created after commit which you moved on to.

2)$git reset --soft HEAD^

     -This will move all the files after the commit which you specified into staging area rather than deleting those.The files in staging area and working are remains unchanged.And as we specified HEAD^ it will move one commit before head.

Note:This is used if we want to edit commit message.Since the files are moved to staging area we can again commit it by changing commit message.

3)$git reset --mixed HEAD~2
        -If we did not specify any mode i.e(--hard/--soft/--mixed) then by default mixed mode was invoked.It will moves both files after commit and files in staging area to working area.

Note:If we delete any commits using reset and if we try to push into remote repo.Then it will throw an error since the number of commits in local amd remote repo are not equal.So do force push by appending '+' before branch name.

$git push origin +master

Case 4:Undoing files pushed into remote repo(Git hub or bitbucket etc):

In case if you pushed code to remote repo then you want to make changes to a particular commit then use git revert.This will just undo things done under specified commit it deleted files which are pushed under the commit and not keeps them in staging area or working area.

As it will delete files under specified commit in local repo only we need to push it again so remote also gets modified,by default it will create new commit with same commit message prefixed with revert.You need to specify --no-commit parameter to restrict default behaviour i.e not to create commit.This is used if you want to make changes further and want to commit manually.

For the ease use --no-edit option because while revertinh it will promt to enter commit message.So if you don't want to change git default commit message use this option.

The most beautiful part of this is ,it does not messup wirh commit history as it creates new commit for reverting.

Syntax:
        $git revert <commit id>

        $git revert <commit id> --no-edit
  
        $git revert <commid> --no-commit

          

Addtional case:Removing untracked directories from working area


References:

Post a Comment

0 Comments