preface

Whether you’re working on a corporate project or with a group of fellow students, you’ll benefit from learning about the world’s most advanced distributed version control system. Let me try to walk you through some of his uses.

Preparations – Software installation

Git can be installed on many operating systems:

  • Linus
  • macOS
  • Windows
  • Solaris
  • Raspberry

My example is to use Git on Windows. You can go to the Git official website to download the installation program and proceed to the next step with the default options. After the installation is complete, ‘Git Bash’ will appear in the menu or right click on the desktop. After the installation is complete, the last step is to set your name and email address by typing:

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
Copy the code

Create a local version library

  1. I’m going to create a new folder (gitTest in my case)

  2. Right click inside the folder to go to the ‘Git Bash’ window, and make sure that the next action is for that folder

  3. Execute the git init command to turn this directory into a repository that Git can manage

  4. If the above is correct, it will appear in the folder.gitThe directory, if not, is probably hidden. You can unhide it as shown below. If not, please check the above operation is correct!!

  5. Next I’ll add a file to the directory, which I’ll call first.txt. The contents of the document are as follows:This is the first file. Next to executegit add first.txtStatement to add the file to the repository. If this step is correct, there will be no hint. Repass statementGit commit -mSubmit the file to the repository. This step, if successful, will inform you of any changes to the file. The diagram below:

Git commit = git add = git commit = git commit Git add adds one file at a time. Git commit commits multiple git add files at once.

Confusion after multiple submissions

This was just a basic introduction, but now we’re in a real situation where you’ve changed the document for a long time, have multiple versions, and are done with git add and Git commit.

  • Version 1:'This is a file'
  • Version 2:this is first demo
  • Version 3:this is first demo but it's not new

So how do you quickly and easily go back to each version? As the most advanced distributed version control system at present, of course, you have already thought about it!

  1. First of all, we may not remember what changes we made. We need to look at the history and execute the statement Git log

    Git commit -m: git commit -m: git commit -m Also notice the long numbers in each record. This is the version number.

  2. Required for rolling back the versiongit reset --hard HEAD^If you want to go back to the previous version you can use HEAD^, the previous version is HEAD^^, and so on. So let’s go back to the previous version and try it out.

    If you open the first.txt file again, you can see that the content has changed to version 2.

  3. It seems that we have completed the requirements, but in fact, we may still appear, after using the previous version for a period of time, we find that the original version is better, but we can not use the above instructions to back to the original version. This is where the version numbers mentioned above come in. Using the commandGit reset -- Hard version number

    At this point, look at the first.txt file, which has changed to version 3

Understand noun workspaces and staging areas

Workspaces are directory files that are visible locally and can be modified with a single click. We also mentioned above that adding files to a Git repository is a two-step process.

  1. Git adds a file from the workspace directory to the staging area
  2. Commit to the current branch with git commit. This means committing all files from the staging area to the current branch

Management of change

It’s very common to find errors in a document, where you can just open the document and delete a line, but where you’ve already committed a line and obviously can’t delete another line. Git checkout — readme.txt — Git checkout — readme.txt — Git checkout — readme.txt — Git checkout — readme.txt — Git checkout — readme.txt — Git checkout — readme.txt — Git checkout — readme.txt — Git checkout — readme.txt

  • The file has not been put into the staging area since the modification. After the statement is executed, the file will return to the same state as the repository.
  • The file has been added to the staging area, then modify it. After the statement is executed, you return to the state where the file was when it was first added to the staging area.

In general is to the git commit or git add state , of course, we are likely to encounter has to submit the error to the staging area (the second case is different with the above, it is stored in the staging area has been a mistake). Git reset HEAD

can be used to roll back the changes made in the staging area to the workspace. So we were able to successfully intercept the bug.

Delete the file

Git status: Git status: git status: git status: Git status: Git status

There are two cases when a file is deleted:

  1. This file is really not needed, we need to delete it in the version library. Git rm and git commit

  2. This file was deleted by mistake, we need to restore, execute statementgit checkout -- <file>, so that you can restore the file to the latest version of the repository. Of course, files not committed to the repository cannot be restored.

summary

Git has taken care of everything you can think of. What’s left is to become familiar with using the statements to achieve various effects. Next, we will lead you to set up gitHub, this website is to provide Git repository hosting, so we need an account, you can get Git remote repository for free. As well as introducing you to some Git branch management methods. Stay tuned.