table of Contents
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
git status
allows us to keep track of the current state of the warehouse
The last sentence tells us: has readme.txt
been modified, but has not yet been committed (commit)
View modified content
Although we git status
can 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 diff
command
git diff readme.txt
From the above output, we can see that we have added a paragraph of text in the last line
Update and submit
We git diff
can 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 commit
submission, let's take a git status
look at the current state of the warehouse
git status
Here you can see the readme.txt
files 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"
Finally,git status
checking the status
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.txt
modified 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
The red box is git commit -m "xxxxxx"
the log in quotation marks
If the output information is too much, you can add --pretty=omline
parameters:
git log --pretty=oneline
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 id
is 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:
Version rollback
If you want to jump, you must first know which version it is currently
In Git:
HEAD
Indicates the current versionHEAD^
Indicates the previous versionHEAD^^
Indicates the previous versionHEAD~100
Represents the last 100 versions
Now, if we want the 2st update
to roll back the current version to the previous version the 1st update
, we can use the git reset
command
git reset --hard HEAD^
--hard
What is the meaning of the parameters? We will talk about it later, now please feel free to use
Take a look readme.txt
at the content
cat readme.txt
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
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 id
is5aa1503e270a325654e435872e12c2769253e6e5
Then we usegit reset --hard 版本号
git reset --hard 5aa15
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
it now, I'm back again
What if I can't find commit id
it?
Git provides git reflog
to record every command
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, HEAD
which version you want to point to, you will locate the version.