Complete git tutorial: 003—Git version management (version update and rollback)

updated version

Modify file

A readme.txt has been created before, now update the content

Git is a version control system.
Git is free software.The 1st update

Entering instructions at this time git status
Insert picture description here
git statusallows us to keep track of the current state of the warehouse

The last sentence tells us: has readme.txtbeen modified, but has not yet been committed (commit)

View modified content

Although we git statuscan know that the file has been modified using commands, we don't know what has been modified.
For example, when I go back to work after the Spring Festival, I have forgotten what I modified before, then I need to
use the git diffcommand

git diff readme.txt

Insert picture description here
From the above output, we can see that we have added a paragraph of text in the last line

Update and submit

We git diffcan view the modified content, and after knowing it, we can safely submit it to the warehouse.

first stepgit add

git add readme.txt

There is no output, indicating that there is no problem

Before performing the second git commitsubmission, let's take a git statuslook at the current state of the warehouse

git status

Insert picture description here
Here you can see the readme.txtfiles that will be submitted. The
green color indicates the working area. I will talk about it in the next article.

Second stepgit commit

git commit -m "the 1st update"

Insert picture description here

Finally,git status checking the status
Insert picture description here
git tells us that there are currently no changes that need to be submitted, and the working directory is clean

Version rollback

Prior to version rollback, please do an exercise, and then updated version
will be readme.txtmodified to read as follows

Git is free software.The 2st update

Practice the method of adding to the warehouse by yourself. If you forget to read the previous content

  • Continue to modify and update the files like this, and then continue to submit them to the repository.
    Just like playing an RPG game, every time you pass a level, the game progress will be saved automatically. If a certain level has not been passed, you can choose to read the archive of the previous level. Sometimes, before hitting the boss, you will manually save the disk so that if the hitting of the boss fails, you can start over from the nearest place.

  • Git is the same. Whenever you feel that the file has been modified to a certain extent, you can "save a snapshot". This snapshot is called commit in Git. Once you mess up the file, or delete the file by mistake, you can recover from the last commit and continue to work instead of losing all the results of months of work.

View update history

When preparing the game to read files, you need to check all the archives. If there is a problem with the version update, you need to check all the previous versions

Here we do a review by ourselves

Original copy

Git is a version control system.
Git is free software.

First update

Git is a version control system.
Git is free software.The 1st update

Second update

Git is free software.The 2st update

Of course, at work, we can't remember as clearly as above, after all, there are hundreds of lines of code at work.
How to do?

usegit log
Insert picture description here

The red box is git commit -m "xxxxxx"the log in quotation marks

If the output information is too much, you can add --pretty=omlineparameters:

git log --pretty=oneline

Insert picture description here
This is a bit refreshing,
but without the submitter and email address, there is still update time

The yellow string is commit id(version number), which is different from SVN. Git's commit idis not 1, 2, 3, but a very large number calculated by SHA1, expressed in hexadecimal, and you see The commit id is definitely not the same as mine, subject to your own.

Why have commit id(version number)

You can use the version number to select the version to return to, that is, the game save

Because Git is a distributed version control system, we will study multiple people working in the same version library later. If everyone uses 1, 2, 3... as the version number, it will definitely conflict.

Every time a new version is submitted, Git will actually automatically string them into a timeline. If you use a visualization tool to view the Git history, you can see the timeline of the commit history more clearly:

Insert picture description here

Version rollback

If you want to jump, you must first know which version it is currently

In Git:

  • HEADIndicates the current version
  • HEAD^Indicates the previous version
  • HEAD^^Indicates the previous version
  • HEAD~100Represents the last 100 versions

Now, if we want the 2st updateto roll back the current version to the previous version the 1st update, we can use the git resetcommand

git reset --hard HEAD^

Insert picture description here
--hardWhat is the meaning of the parameters? We will talk about it later, now please feel free to use

Take a look readme.txtat the content

cat readme.txt

Insert picture description here
Sure enough, it was restored

Go back to the previous version

What should I do if I regret it after rolling back?
Check first and find git log
Insert picture description here
that the second update has disappeared. It's like taking a time machine to go back to the past and unable to return to the future. What should I do?

If the window is not closed, you can look up commit id, my latest version commit idis5aa1503e270a325654e435872e12c2769253e6e5

Then we usegit reset --hard 版本号

git reset --hard 5aa15

Insert picture description here
In fact, you don't need to write so complete the version number, just write the first few digits, and Git will find it automatically.
Of course, you can't just write one or two, because Git may find multiple version numbers, and you can't determine which one it is.

Look at readme.txt
Insert picture description here
it now, I'm back again

What if I can't find commit idit?
Git provides git reflogto record every command
Insert picture description here
you make, you can see the version number, and
we can pass it git reset --hard 版本号back again.

Git version switching principle

The Git version rollback speed is very fast, because Git has a HEAD pointer to the current version internally. When you roll back the version, Git just points the HEAD fromthe 2st update

┌────┐
│HEAD│
└────┘

└──> ○ the 2st update
      │
      ○ the 1st update
       │
      ○ wrote a readme file

改为指向the 1st update
┌────┐
│HEAD│
└────┘

│     ○ the 2st update
│     │
└──> ○ the 1st update
       │
      ○ wrote a readme file

After the point is changed, Git will update the files in the workspace by the way.
That is, HEADwhich version you want to point to, you will locate the version.

Guess you like

Origin blog.csdn.net/qq_28258885/article/details/114982509