Distinguish some basic commands in Git

Tram Ho

When I first learned about git, did you notice that some of the commands in git have quite similar functions and have you ever wondered what the difference is between them? From my knowledge as well as picking up some documents online, today I will write an article to help you distinguish some common commands in git.


Git pull vs git fetch

Before we talk about the differences between these two commands, let’s emphasize their similarities: both are used to download new data from a remote repository. . Downloading new data to your computer is extremely necessary, because when you clone a project from the remote repository, that data is just a ” snapshot ” at the time you clone the project. . Therefore, when there are changes on the remote repository, they need to be downloaded in your local repository. We can now use ** git fetch ** or git pull to accomplish this. So what are the differences of these two statements?

  • Git fetch:
    When running the command git fetch <remote_origin> , Git will download the data of all branches of the repository on the remote server located at the address specified by <remote_origin> and update this data with the data of the branch below. local machine. However, git fetch does not update the data of the working directory , which means that if there are any changes made on the remote server, they will not affect your files or directories.
  • Git pull:
    When running the git pull <remote_origin> <branch_name> command , Git will fetch the remote repository’s data from the <branch_name> branch from the server located at the <remote_origin> address and apply the merge. Go to this folder and file in the working directory .
    Git pulls always try to auto-merge changes, so this can lead to conflicts .

Git reset vs git revert

During software development, it is often unavoidable to rollback some failed commits. In this case, we will have two main methods to deal with: git reset and git revert

  • Git reset :
    A Git reset is used to return to a commit, and clears the history of the commits before it.
    Consider the following example to better understand git reset:
    Suppose, your commit history has the same commit points (A, B, C, D) that are similar to the ones above. A, B are commits that are working normally, and C, D are commits that have problems. You can now return to commit B with the command

    After running the above statement, we have the result:

    As you can see, the HEAD cursor is pointing to the location of commit B, and the commit history C and D also disappear , just like you have never made a failed commit. The commit history also looks cleaner.
    However, these changes only take place on the local repository , to update this change on the remote repository you need to execute the command:

     

  • Git revert
    That’s with ** git reset **, what about git revert ?
    Git revert does not lose commits, instead it creates new commits that are identical to the commit you want to return.

    In the same case as with git reset , all we need to do is revert to commit D , then continue to revert to commit C.

    Results after running the above two commands:

    Here, we can see that two new D ‘and C’ commits have been created .
    As such, git reset will clear the commit history to make the history look cleaner, while git revert will create new commits while retaining the previous commit history.


Git reset, git reset –soft, git reset –hard

As distinguished between git reset and git revert above, we know the effect of git reset . However, depending on the circumstances as well as the purpose, we need to reset with different modes (for example, whether we keep the previous changes or not), so git gives us 3 options. Comes with: –soft, –hard, –mixed

  • Git reset <commit_id> : Moves the HEAD cursor to the commmit reset position and ** maintains all file changes **, but removes the changes from the stage .
  • Git reset –soft <commit_id> : This command only moves HEAD to the commit location. The state of the stage and all file changes will be preserved .
  • Git reset –hard <commit_id> : Moves the HEAD cursor to the reset reset position and removes all file changes .

Conclusion

Thank you for following up on my article! Hopefully through this tutorial you can identify some basic commands in git and know how to use them flexibly in each case!

References:

Share the news now

Source : Viblo