I see castle peak more charming, material castle peak, see me, should be so. Programmers should

Introduction of git

As many of you know, Linus created the open source Linux in 1991, and since then Linux has grown to become the largest server system software.

Linus may have created Linux, but it was built by volunteers all over the world. With so many people writing code for Linux all over the world, how does Linux code manage?

In fact, before 2002, volunteers from all over the world sent source code files to Linus via diff, and Linus merged the code by hand!

You might be thinking, why doesn’t Linus just put Linux code in version control? Aren’t there free version control systems like CVS and SVN? Because Linus was adamantly opposed to CVS and SVN, these centralized version control systems were slow and had to be networked to use them. There are some commercial version control systems that are better than CVS or SVN, but they are paid for and not in the open source spirit of Linux.

By 2002, however, the Linux system was a decade old, the code base was too large for Linus to manage manually, and the community complained bitterly about this, so Linus chose a commercial version control system, BitKeeper. BitKeeper’s owner, BitMover, has licensed the version control system to the Linux community for free out of humanitarian interest.

The peace and unity broke down in 2005, when the Linux community became crowded and became infected with the habits of some of the heroes of Liangshan. Andrew, who developed Samba, tried to break BitKeeper’s protocol (and he wasn’t the only one), and BitMover caught him (good job monitoring!). BitMover took the Linux community back for free.

Linus could apologize to BitMover and promise to discipline the boys in the future. Well, that’s not going to happen. Here’s what actually happened:

Linus spent two weeks writing his own distributed version control system in C, called Git! Within a month, the source code for the Linux system was managed by Git! How do you define a cow? You can get a sense of that.

Git quickly became the most popular distributed version control system, especially in 2008, GitHub website was launched, it provides free Git storage for open source projects, numerous open source projects began to migrate to GitHub, including jQuery, PHP, Ruby and so on.

History is such an accident that if BitMover hadn’t threatened the Linux community, we probably wouldn’t have free and super useful Git today.

Git installed

  • Windows

    Go to the official Git website to download, select the installation path, default installation can be Git official website download

  • Lunix

    • You can usually enter git to see if it is installed. If it is not, you will be prompted to install the command

    • Sudo apt-get install git

    • For other versions of Linux, you can download the source code from the official website, decompress it, and then enter./config, make, sudo make install

  • Mac

    Xcode is the best IDE for Apple systems. Developers must download it. Xcode is integrated with Git, but there is no default installation, so you need to run Xcode. Locate Downloads in Preferences, select Command Line Tools, and click Install

The basic application

Create a version library

What is a version library? Git keeps track of all changes and deletions of files in a repository, so that they can be traced at any time in history or “restored” at some point in the future.

  • Select a suitable place and create an empty directory

    Mkdir GitHubRepository CD GitHubRepository PWD # Displays the current directoryCopy the code

    Note: In order to avoid confusing problems, it is recommended that directories (including parent directories) be in English

  • Make this directory a Git-managed repository with the git init command

     git init
    Copy the code

    Git repository: Git repository: Git repository: Git repository: Git repository: Git repository: Git repository: Git Repository Git repository is destroyed.

  • Add the file to the repository

    Git is no exception. All version control systems can only track changes to text files, such as TXT files, web pages, and all program code. The version control system can tell you each change, such as adding the word “Linux” on line 5 and deleting the word “Windows” on line 8. Binary files such as images and videos can also be managed by the version control system, but there is no way to track the changes in the file. Instead, the binary can only be strung together to know that the image has changed from 100KB to 120KB, but the version control system does not know what the changes are.

    Unfortunately, Microsoft Word is a binary format, so the version control system can’t track changes to a Word file. The example above is just to demonstrate that if you really want to use a version control system, you need to write the file in plain text.

    For example, Chinese has the commonly used GBK encoding and Japanese has Shift_JIS encoding. If there are no historical problems, it is strongly recommended to use the standard UTF-8 encoding. All languages use the same encoding, which has no conflicts and is supported by all platforms.

    • Write an abou.txt file with the following contents

      Git is a version control system

    • Add the files to the repository using the git add command

      git add about.txt

    Note: LF and CRLF are two line breaks, which will not affect Git learning this time

    • Commit the file to the repository using git commit

      Git commit -m “This is a message about me” # -m

    • Use git status to view the results

      git status

    Git add

    git add

    Git commit -m

    can commit multiple files at once


Time Travel (Version)

  • Now, to make changes to the project, we modify the about.txt file and add the following information

     Git is a free software.
    Copy the code

    Then use git status to view the result

    Git status allows you to keep track of the current status of your repository. The command output above tells you that the file has been modified but has not been committed yet

    • Run the git diff command to view the modified contents

      git diff about.txt

    • Then use git add separately. git commit; Git status command to check the git status

      Git commit -m “add a message” git status #

  • Version back

    • Run the git log command to check the file version

      git log

    The git log command displays commit logs from the most recent to the most distant

    If the output is too much, you can add the –pretty=oneline parameter

     git log --pretty=oneline
    Copy the code

    As a reminder, you see a bunch of things like 4B4258EE… Git has a commit id (version number). Unlike SVN, Git has a commit id not 1,2,3… Instead, it’s a very large number calculated by SHA1 in hexadecimal notation, and the COMMIT ID you see is definitely different from mine, whichever is yours. Why is the COMMIT ID represented by such a large string of numbers? Because Git is a distributed version control system, we will have to study how many people work in the same version library. If everyone uses 1,2,3… As a version number, that’s definitely a conflict.

    • Version back

    In Git, HEAD represents the current version, which is the latest commit 1094ADB… (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~100.

    Git reset --hard HEAD^Copy the code

    We went back in a time machine

    • Want to come back again

    If you want to come back, you must know the COMMIT ID. If you don’t remember it, Git provides a remedy for regret

    Git reset --hard 4b4258e cat about.txtCopy the code

    Hey hey, I hu Hansan back

    Note: It is not necessary to write the full version number, just the first few digits will do, Git will automatically find. 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.

  • Work area and staging area

    One thing that differentiates Git from other version control systems such as SVN is the concept of staging.

    • Working Directory

    A directory that you can see on your computer, like my LearnMysql folder, is a workspace

    • Repository (Repository)

    A workspace has a hidden directory. Git, which is not a workspace, but a git repository.

    Git’s repository contains a number of things, the most important of which is a staging area called stage (or index), the first branch that Git automatically creates for us, master, and a pointer to master called HEAD.

    • The working process

    The first step is to add the file with git add, which essentially adds the file changes to the staging area.

    The second step is to commit the changes with git commit, which essentially commits the entire contents of the staging area to the current branch.

  • Management of change

    Git commit commits the first change in the staging area. Git commit commits the first change in the staging area. The second change will not be committed.

    • You can use the git diff HEAD command to see the difference between the latest version of your workspace and your repository

      git diff HEAD –about.txt

  • Undo modify

    When you were working late at night to revise the project, you accidentally added the following to the project

     TMD,stupid boss
    Copy the code

    HMMM ~ uncomfortable! This month’s bonus, HMM

    • The content is only in the workspace and git Add is not used

      git checkout — about.txt

    Git checkout — file can discard workspace changes

    The modification was deleted

    Git checkout — file git checkout — file git checkout — file

    • The content is in the staging area, using git add command, not git commit command

      Git reset HEAD about.txt

    Now that the changes are in the workspace, you can use the command above to delete the workspace changes

    Well, the bonus was saved

    • If the brain is hot, usegit commitCommand to roll back to the previous version

  • Delete the file

    Now we have a useless file test.txt, we want to delete the file, the file is already in the version library

    First let’s delete the files in the workspace, either graphically or using the command rm

    Now we have two options

    • Delete files from the repository

      Git commit -m “remove test. TXT “# git add test. TXT

    • Delete, restore file from version library

      Git checkout — test.txt git checkout — test.txt

Tip:

  • Git Commit Information

    Submission information is an important resource to use when viewing changes made by others or your own history. Therefore, please carefully fill in the submission information of the modified content, so that others can easily understand. Git’s standard comments are as follows:

    Line 1: Submission of a summary of changes Line 2: blank line after line 3: Reason for changesCopy the code
  • Git update

    Git update-git-for-windowsCopy the code

Welcome everyone to pay attention to the public number programmers should be so, get wonderful content