This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022.

The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

Note: 😎 let’s learn new commands today! What should you do and who should you turn to for help when a major bug happens or you delete some files accidentally after you commit a file and the service crashes? Or do you know what files you changed that caused all these problems? 😭 😭 😭

πŸ˜‹ Git use

πŸ‹ regret medicine

By now, I’m guessing you’ve learned how to modify and add submissions.

NowπŸ’, let’s practice again, modify hello. TXT file as follows:

helloGit!
Git is very nice software!
Copy the code

Then you are ready to try adding a submission:

$git add hello. TXT $git commit -m "hello. TXT "[master 20a20e7] hello. TXT add very 1 file changed, 1 insertion(+), 1 deletion(-)Copy the code

Like this, the daily grind keeps you making changes to the file, and then committing changes to the repository. For example, every time the project manager makes a request, you do it and submit the code. But what if, on one of the submissions, a Bug appears, or an important file is accidentally deleted, causing the whole project to wobble and crash? 😭 😭 😭

As the saying goes, life is alive, there is always too late to regret things, but life is no regret medicine can eat ah! But just because there is no regret, life is destined to be wonderful, must want to live well this time, will be very meaningful. If anyone has one, please give me one so that I can go back to a time and say to that girl… Ahem, a bit of a digression! Ha ha πŸ€ͺ

​

In Git, don’t worry, because Git always has a remedy for regret.

​

When working with files, you can “save a snapshot” whenever you feel the file has changed enough. This snapshot is called a commit in Git. Once you make a change or delete a file by mistake, you can recover from your most recent commit and continue working, rather than losing months of work. Do you feel very good πŸ‘! Hey hey, specific how to operate, please see as follows:

  • First, let’s review what we did with hello.txt and how many versions were committed to the repository:

    • Add hello. TXT to the local repository where the content of hello. TXT is
    helloGit!
    Copy the code
    • The second time, modify the hello. TXT content
    helloGit!
    Git is nice software!
    Copy the code
    • For the third time, add very to hello. TXT
    helloGit!
    Git is very nice software!
    Copy the code
  • In practice, how can we remember the contents of a file every time πŸ˜‚ (of course, you can forget that I did not say this word), but we add the file to the commit Git repository what use. Git can help me keep track of the status of the file. There must be some command that tells us the history of the file.

    Git log Git log Git log

  • Next, let me explain the git log command πŸ’ͺ :

    The git log command displays the latest commit logs from the most recent to the most recent commit. We can see 3 commits, the latest is to add nice to hello. TXT, the last is to modify hello.

    Pretty =oneline: pretty=oneline:

    • πŸ‘‰ Here, it’s important to note that you see a bunch of similar20a20e7a27c...iscommit id(The version numberGit)commit idNot one, two, three… Increasing number, but a very large number calculated by SHA1, expressed in hexadecimal, and what you seecommit idIt’s definitely not the same as mine. Your own will prevail.Pay attention to).

    Why is the COMMIT ID represented by such a large number? Because Git is a distributed version control system, Git uses this unique id to distinguish each commit. As a version number, that’s definitely a conflict.

    Every time you commit a new version, Git actually strings them together into an automatic timeline. If you use a visualization tool to view Git history, you can see the timeline of Git commit history more clearly:

  • Git: hello.txt ($Git, $Git, $Git, $Git, $Git)

    • How does Git determine the current version of Git?

      In Git, HEAD represents the current version, that is, the latest commit 20a20e7a27c… (Note that my submission ID is not the same as yours), the previous version is HEAD^, and the previous version is HEAD^^, of course, it is easier to write 100 ^ in the previous 100 versions, so write HEAD~20a.

    • Git reset: “hello. TXT” “hello. TXT” “hello. TXT”

      $git reset - hard head ^ (or written git reset - hard 20 a20e7a27c81692148cdf1be04107caefd23b04) head is now at 96 c9275 Modify the hello. TXT contentCopy the code

    • Let me explain the git reset command:

      What happens inside Git when it executes this code?

      When you go back, Git simply adds nice from HEAD to hello. TXT:

      β”Œ ─ ─ ─ ─ ┐ β”‚ HEAD β”‚ β”” ─ ─ ─ ─ β”˜ β”‚ β”” ─ ─ > a. hello. TXT add nice β”‚.through modify hello. TXT contents β”‚.through add hello. TXT to local repositoryCopy the code

      To modify the hello.txt content:

      β”Œ ─ ─ ─ ─ ┐ β”‚ HEAD β”‚ β”” ─ ─ ─ ─ β”˜ β”‚ β”‚.through the hello. TXT add nice β”‚ β”‚ β”” ─ ─, a. modify > hello. TXT contents β”‚.through add hello. TXT to local repositoryCopy the code

      I also updated the files in the workspace. So whatever version number you want HEAD to point to, that’s where you place the current version. So this is how you roll back or restore. Hey, hey, you know what? 😜 😜 😜

      There is a parameter called hard. What does that do? This place can have three parameters, what are they? πŸ‘‡ πŸ‘‡ πŸ‘‡

      • Soft: Leave working tree and index untouched Soft reset: does not change the workspace and index
      • Mixed: Leave working tree untouchd,reset index Mixed: Leave workspace unchanged,reset index file
      • Hard: Reset working tree and index(discard all local changes) Hard: Reset working tree and index(discard all local changes)
    • Now let’s see if the contents of our hello. TXT file really change to the contents of the previous version:

      $ cat hello.txt
      helloGit!
      Git is nice software!
      Copy the code

      As you can see, it was restored.

      Git hello. TXT: git hello. TXT: git hello. TXT: git hello.

      96 $git log commit c927552c917fdc82db336fbebd9f28832a84e6 (HEAD - > master) Author: favorite canned fish < [email protected] > Date: Wed Jan 26 15:20:52 2022 + 0800 to modify the hello. TXT content commit c672725837dc0f5276303e30f4e6b67187e8f1ed Author: Favorite canned fish <[email protected]> Date: Tue Jan 25 15:13:02 2022 +0800 Add hello.txt to local repositoryCopy the code

      The latest version of hello. TXT with “very” no longer exists! This is like you, you took the time shuttle from the 21st century to the 20th century, do you think you now have a 5g signal, probably not even a phone? So the latest version, certainly not see! πŸ˜†

  • But, but, I now go back on my words, there is nothing good in the 20th century, but also want to go back to the 21st century, how to do? Oh, no, no. I’m never going back, am I?

    Git: commit id 20a20e: commit id 20a20e: commit id 20a20e: commit id 20a20e: commit id 20a20e: , so you can specify a future version:

    $git reset --hard 20a20e HEAD is now at 20a20e7 hello. TXTCopy the code

    It is not necessary to write the full version number, just the first few digits will do, Git will automatically find it. Of course, you can’t just write the first one or two, because Git may find multiple version numbers and never know which one it is.

    Let’s now carefully see if the contents of hello. TXT are changed back:

    $ cat hello.txt
    helloGit!
    Git is very nice software!
    Copy the code

    Hey hey, did not think of, others after 18 years ability is a hero again, and I came back immediately πŸ˜…. Imagining

    • But what if my command line window closes, or I regret it the next day and want to restore to the latest version? We cannot find the latest version of the commit ID. I don’t think so. 😡

      Let me tell you. Remember I just said that in Git, there is always a remedy for regret. Git felog is used to record every command you make:

      $ git reflog 20a20e7 (HEAD -> master) HEAD@{0}: reset: moving to 20a20e 96c9275 HEAD@{1}: reset: Head ^ 20a20e7 (head -> master) head @{2}: commit: hello. TXT C672725 HEAD@{4}: commit (initial): Add hello. TXT to the local repositoryCopy the code

      It’s a relief to be here, and now you can travel back to the future in a time machine. Now find your latest version of the commit ID and execute the following code:

      $git reset --hard 20a20e HEAD is now at 20a20e7 hello. TXTCopy the code

      Let’s see if the contents of hello. TXT are changed back:

      $ cat hello.txt
      helloGit!
      Git is very nice software!
      Copy the code

      πŸ˜€πŸ˜€πŸ˜€ he-he-hey, just a item for ΨΨβ˜α–—δΉ› one item perfect!! Yeah, I feel it. It’s all back.

    • After learning this, don’t you feel that it is really nice to have Git, but also to have regret medicine to eat, travel through time and space at will!

Let’s keep this in mind:

  • HEADThe version you point to is the current version, so Git allows you to run commands through the history of versionsgit reset --hard commit id.
  • Before the shuttle, usegitΒ logYou can view the commit history to determine which version you want to fall back to.
  • To go back to the future, usegitΒ reflogReview the command history to determine which version to go back to in the future.

🌸 summary

I believe that you see here, today’s git tutorial should be simple for you! What we need to do today is how to use the regret medicine provided by Git. There is no regret medicine in life, so cherish every drop of life, thank for happening, thank for the past, and look forward to the future!

Git log View submission history git reflog View command historyCopy the code

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little bit, please kindly and lovely talent leaders give a thumblike πŸ‘, save it, and welcome to follow ❀️❀️❀️ [favorite food of canned fish] πŸ‘, thank you very much! If you like canned fish too! πŸ’• πŸ’• πŸ’•

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!