Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Undo Changes in git

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 358
    Comment on it
    Previous Git Blogs

    1. Intialize git repostiory
    2. First Commit
    3. Two tree architecture and Three tree architecture
    4. Best basic practices for writing commit messages
    5. Viewing the commit logs
    6. Git Basic WorkFlow
    7. What is HEAD pointer in GIT
    8. Git Configuration
    9. Viewing history
    10.Updating a file and adding it to repository
    11.Viewing history
    12.Git Config, Add, Commit, View, Diff summary
    13.Pictorial Representation of basic Git flow
    14.Git Diff and add commit in one shot

    What if we have accidently deleted some code in my case deleted a line from readme.txt

    Let check the status

    naveen@naveen:/var/www/html/RND/symfony_sites/symfony_todoapp$ git status
    Git Display
    [master 0b599b9] Replace the text of readme.txt file
     1 file changed, 2 insertions(+)
    naveen@naveen:/var/www/html/RND/symfony_sites/symfony_todoapp$ git status
    On branch master
    Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git checkout -- ..." to discard changes in working directory)
        modified:   readme.txt

    Git says that readme file is modifed, lets check what has been modifed in readme.txt file

    naveen@naveen:/var/www/html/RND/symfony_sites/symfony_todoapp$ git diff readme.txt
    Git Display
    diff --git a/readme.txt b/readme.txt
    index 52d94d1..14929f6 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,5 +1,4 @@
     Please Read me
     MY new changes
     adding multiple commits to stagging
    -this is really cool and appreciative feature

    Git tell me that at line number @@ -1,5 +1,4 @@ a line has been deleted and git has attached a - `minuses` sign on front of it

    So what we want is we want the repository version back the one that Git has saved for us to be restored. What we're going to command, "Git, go back to the repository, get that version, which Git display when I do git diff and check it out and replace it with what I have in my working directory .

    we use the git checkout command to achieve this. we could do git checkout readme.txt .

    The issue here is checkout is used for more than one purpose.

    1. It's also used for working with branches. Because what checkout does is go to the repository, get the named thing that I've gave you, and make my working directory look like that.That's what it does. So if that named thing is a branch, it brings the branch down. If that named thing is the file, it brings the file down
    2. IF we say git checkout resources. But what if we also have a branch named readme Then it's hard for Git to tell which one we need and in that case it actually would give us the branch resources instead of the folder resources.

    It's a good practice when we're not trying to checkout a branch to put dash--dash, followed by readme.html that says stay on the current branch. double dash make sure that we're not checking out a new branch, we're just talking about a file in the current branch.

    naveen@naveen:/var/www/html/RND/symfony_sites/symfony_todoapp$ git checkout -- readme.txt

    No check your file by opening it

    naveen@naveen:/var/www/html/RND/symfony_sites/symfony_todoapp$ gedit readme.txt

    I can see my text `this is really cool and appreciative feature` is back. Thats cool

    now commit the changes

    naveen@naveen:/var/www/html/RND/symfony_sites/symfony_todoapp$ git commit -m"retreived the old version"

    check the status

    naveen@naveen:/var/www/html/RND/symfony_sites/symfony_todoapp$ git status


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: