Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Git Reset (The Complete Guide)

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 129
    Comment on it

    Git Reset (The Complete Guide)

    Git is a widely used source control. As a software developer, you have to use source control like Git for your codebase. 

    Many times you need to reset some of your local files to match with the remote server. 

    Here the remote server can be your GitHub, Azure DevOps, or self-hosted environment.

    This tutorial will explain all you need to know about Git reset and its different options.

    Let's start with the basics.

    What is HEAD? 

    Did you notice, I am using all the capital letters here - HEAD. 

    Technically, HEAD is nothing but your active branch. 

    HEAD - branch, which is currently active. 
    Head - refers to the branch which you are not viewing.

    What is Git HEAD? 

    And if you are referring to the Git HEAD, it's about the last commit in your active branch.

    What is a local repository and remote repository in Git?

    The repository is the hidden .git subdirectory where you store your codebase.

    You will have a remote repository. So you can work in collaboration with your team. 

    And you will have a local repository where you commit your local changes. Once you complete your work with your code, you will push changes to the remote repository.

    What is the Working Directory in Git?

    The working directory is nothing but your project folder where you store all of your code files. 

    It is the area where you work (like writing your code) known as the working tree. It's where all of your files are stored. 

    It is also known as the "untracked" section of Git. Any file changes will be visible in the Working Tree. 

    What is Git Index? 

    Git index is also known as a staging area.

    Git index is the area between the working directory and your local repository.

    So you can add the changes that you wish to commit to your repository.

    Sounds good.

    So now we know the fundamentals. 

    And Now, It's time to dive deeper into advanced concepts.

    Git Reset

    Git reset command is one of the powerful git commands. It can help you undo (discard) your local changes to the state of a Git repository.
     
    The "git reset" command is to change the current HEAD to the given commit.

    There are three options you can use with Git reset.

    1. Soft 
    2. Mixed
    3. Hard

    1. Git Reset with Soft Option

    You can use the git reset command with the soft option. The --soft flag does not affect the Index (staging area) or the working tree.

    It only reset the HEAD. 

    Here is the syntax of the Git reset command with the --soft option

    $ git reset --soft <commit hash>

    The commit hash is a long string. It's a Git-generated unique identifier. "Commit reference" or "SHA" is another name for the "commit hash."

    What will happen when you execute the command with the soft option. Let's understand it with an example.

    $ git log --oneline --graph 

     

    As you can see, the latest commit is about the background color. 

    And now, you can use the following command to reset the head to its previous commit.

    $ git reset --soft 467c7d0

    Two important things will happen here. 

    • Shift the head to a particular commit. (In our case to the previous commit)
    • Your changes (between the original and current HEAD) will be available in the staged area. 

    2. Git Rest With Mixed Option (Default Option)  

    The mixed option is the default option of the Git reset command. 
    It means, if you do not use any option, Git reset command will use the mixed option.

    It will reset HEAD and the staging area (also known as the Index).

    Let's use the previous setup to understand. The latest commit is about the background color. 

     Now, if you execute the command Git reset ( or git reset --mixed)

    Here is the snapshot, which will explain the entire story of the Git reset command.

    Two important things will happen here. 

    • Shift the head to a particular commit. 
    • Your changes are not available in the staging area.

    The vital difference between the Soft and mixed options is that the --mixed option will also touch the staging area.

    3. Git Reset With Hard Option 

    You can use the --hard option to reset the index and your working directory.

    $ git reset --hard <commit hash>

    Let's use the previous example to understand the --hard option in Git reset command. Again you can see the latest commit is about the background color. 

    As you can see, after executing the command, the git head resets to the specific commit. Also, the staging area is empty.

    So Git reset with --hard option will do three important things. 

    • Shift the head to a particular commit. 
    • Your changes are not available in the staging area.
    • Your changes are not available in your working directory.

    Summary

    In this article, you learned how to use the “git reset” command. It will help you to reset your files to HEAD on Git simply.

    Also, we discussed the difference between different git reset options.

    The function of git reset is to take the current branch and reset it to point someplace else, possibly bringing the index and work tree with it.

    Which Git reset option do you like the most? Or which Git reset option you do not like? 

    Either way, let me know by leaving a comment.

    git

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: