• notice
  • Congratulations on the launch of the Sought Tech site

How Git rolls back code

foreword

The version management tool most used in work now is git, and I believe everyone in Internet companies is using it. Therefore, today I will give a brief introduction to the use of git and common commands, one is used as a learning record for myself, and the other is used as a memo for work. Of course, a small partner in need can also be used as an answer reference item for search questions. Well, without further ado, let's introduce how git rolls back code, today's topic?


We will introduce the following three situations:

  • One is that the code has not been submitted to the staging area

  • The other is that the code has been submitted to the staging area

  • The last one is that the code has been pushed to the remote warehouse


Let's first explain the term temporary storage area :

Staging area: There are a lot of things stored in Git's version repository, the most important of which is the staging area called stage (or index), as well as the first branch that Git automatically created for us master, and mastera pointer to it calledHEAD .



1. The code has not been submitted to the staging area


Git will tell you that you git checkout -- filecan discard changes to the workspace:

$ git checkout --readme.txt


git checkout -- readme.txtThe meaning of the command is to readme.txtundo all the modifications of the file in the workspace. There are two situations here:

  • One is readme.txtthat it has not been placed in the temporary storage area since the modification, and now, undoing the modification will return to the same state as the version library;

  • One is readme.txtthat after it has been added to the temporary storage area, it has been modified again. Now, undo the modification and return to the state after adding to the temporary storage area.

git checkout --file

The command in the command is --very important. If it is not , it becomes the command of "switch to another branch". We will encounter the command --again in the later branch management .git checkout


2. The code has been submitted to the staging area

Git also tells us that git reset HEAD <file>the changes to the staging area can be undone (unstaged) and put back into the workspace with the command:

$ git reset HEAD readme.txt Unstaged changes after reset: M readme.txt

git resetThe command can either roll back the version or roll back the modification of the staging area to the working area. When we use HEADit, it means the latest version


3. The code has been pushed to the remote warehouse

In actual work, how can we remember in our minds what is changed every time in a file with thousands of lines, otherwise what would we do with the version control system. There must be some command in the version control system that can tell us the history, and in Git, we look at it with the git logcommand:


git logCommand to display commit logs from most recent to furthest


If you think that the output information is too much and you are dazzled, you can try adding --pretty=onelineparameters


First, Git must know which version the current version is. In Git, the HEADcurrent version is represented by

The last version is HEAD^, the last version is , of course , it is easier to HEAD^^write 100 of the 100 versions above , so it is written .^HEAD~100


to use the git resetcommand:

$ git reset --hard HEAD^

You can specify a version back in the future:

$ git reset --hard commitId


Now, you roll back to a certain version, turn off your computer, regret it the next morning, and want to revert to a new version? What if I can't find the new version commit id?

In Git, there is always regret medicine. When you $ git reset --hard HEAD^roll back to the xxx version, and then want to restore to yyy, you must find xthe commit id of xx. Git provides a command git reflogto log every command you make:

$ git reflog


in conclusion:

  • HEADThe version pointed to is the current version, so Git allows us to traverse the history of versions using commands git reset --hard commit_id.

  • Before switching, git logyou can view the commit history to determine which version to roll back to.

  • To go back to the future, use the git reflogcommand history to see which version of the future to go back to.


Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

atorvastatin medication & lt;a href="https://lipiws.top/"& gt;atorvastatin 10mg usa& lt;/a& gt; buy lipitor for sale

Yuhbxw

2024-03-11

Leave a Reply

+