Basic operation and skills of GIT local warehouse

Time:2020-10-17

What is git?

A distributed version control system is similar to SVN, but much more powerful than SVN. 1. Git can facilitate local version management, just as you have a local version management server. We can choose to push the local version to a unified version management server at the appropriate time ② Git will extract a complete image of the entire code warehouse every time, which is equivalent to a backup of the entire code warehouse. In this way, in addition to problems in the timing version server, we can directly use the local warehouse for recovery! Combined with the local version management function, if there is a problem with the remote version management server, we can still write our own code. When it is restored, we will submit our local version! Git was initially developed to better manage the Linux kernel, but now it has been widely used in various projects!

Let’s take a look at the basic operation of GIT local warehouse as follows:

Set name and email address

Set the name and email address when using GIT. Name must be entered in English
git config --global user.name "chuyaoxin"
git config --global user.email [email protected]

The result of running the command is to generate a. Gitconfig file in the path of “C: users / user name”, which can be opened in Notepad.

When you want to change this information, you can edit the settings file directly. The name and email address set here will be used in Git’s submission log. Since the name and email address here will also be disclosed along with the submission log when publishing the warehouse on GitHub, please do not use the privacy information that is inconvenient to be disclosed.

Create warehouse

Create a new folder, such as “testforgit”
Then right click on the folder and select git bash here
After opening, entergit initComplete the establishment of code warehouse

inputls -alYou can see that there is a. Git folder in the directory, but this directory is hidden by default.

Submit local code

First, add all the contents to be submitted with the add command, and then the commit operation is actually executed.
The first step is to tell git to add the file to the repository with the command git add
git add readme.txt
The second step is to tell git to submit the file to the repository with the command git commit
git commit -m "write a readme file"

.gitignore

. gitignore is git’s ignore configuration file, which can be placed in the root directory of the project, so that the files listed in the. Gitignore configuration file will not be tracked when the code is submitted. In addition, the. Gitignore file itself needs to be tracked and managed by GIT and cannot be ignored.

git add

Purpose: it can be used to track new files, add modified tracked files to the cache, or mark merged conflict files as resolved
The original intention of GIT add is to update the modified contents of the current working directory tree into the index (that is, into the cache)
It mainly aims at two kinds of files: (1) the files which are not tracked by the version library; (2) the files that have been tracked but modified by the version library
In addition, GIT add automatically ignores files that are ignored by the. Gitignore file.

git add .
Modify the file and create a new file! Do not include deleted files) to the staging area
Git add. Will add all untracked files to the staging area and filter them according to. Gitignore

git add *
Git add * ignores. Gitignore and adds any files to the staging area

git add -u
Modify the file, delete the file (note! Only the added files are monitored, not including the new files), which are added to the temporary storage area. U means update

git add -A
Add the modification of the file, deletion of the file, and creation of the file (equivalent to the combination of add. And add – U) to the temporary storage area. A means all other
have access togit add -hCommand to view


$ git add -h
usage: git add [<options>] [--] <pathspec>...
  -n, --dry-run     dry run
  -v, --verbose     be verbose
  -i, --interactive   interactive picking
  -p, --patch      select hunks interactively
  -e, --edit      edit current diff and apply
  -f, --force      allow adding otherwise ignored files
  -u, --update     update tracked files
  --renormalize     renormalize EOL of tracked files (implies -u)
  -N, --intent-to-add  record only the fact that the path will be added later
  -A, --all       add changes from all tracked and untracked files
  --ignore-removal   ignore paths removed in the working tree (same as --no-all)
  --refresh       don't add, only refresh the index
  --ignore-errors    just skip files which cannot be added because of errors
  --ignore-missing   check if - even missing - files are ignored in dry run
  --chmod (+|-)x    override the executable bit of the listed files
  --pathspec-from-file <file>
             read pathspec from file
  --pathspec-file-nul  with --pathspec-from-file, pathspec elements are separated with NUL character

git commit

git commit -m "write a readme file"

Can be used more than oncegit addUse it latergit commitTo submit.
In the GIT commit command, the description of this submission is entered after – M. any content can be entered, but it is better to be meaningful, so that the change record can be easily found from the history.
After successful execution, it can be seen from the figure that a file has been changed with 6 lines inserted.

git statusgit status
have access togit statusCommand to see if there are any unsolicited files

The above figure shows that the current documents have been submitted normally.
What if I modify the uploaded file but don’t mention it?

The error shown above will be reported! modified: readme.txt no changes added to commit
Literally: readme.txt Has been modified and the submission has not changed

git diffgit diff readme.txt
git statusYou can let us know whether there is a normal submission, such as the example above, but if you want to see what the file has modified, you can use theGit diff file namecommand

This command displays my record of changes to this file

View submission recordsgit log

Use git log to view historical submission information

You can see from this picture beforegit commit -m "write a readme file"The function of the – M part is the same as that of writing comments in programming. It can be omitted, but it is convenient for future work.
Another thing to note is the output,
Obviously, I submitted it twice. Take the first result as an example,

commit 418ee0fc27d78a71f5869451b5a1599cc028b0ca (HEAD -> master)
Author: chuyaoxin [email protected]
Date: Tue Aug 4 19:48:59 2020 +0800
write a readme file

The corresponding meanings are as follows

The corresponding version number of this submission
Submitted by: name email
Time of submission
The content of the revision of the submitted version: it is usgit commit -m “xxx”XXX in Li

Undo uncommitted changes

If you have made an incorrect change to the file, and it has been saved by Ctrl + s, but has not been submitted, you can use thecheckoutcommand

Not only was it not submitted, but there was no add
If this is the case, just one sentence is neededGit checkout modified file namethat will do
For example,git checkout readme.txt

At this point, open again readme.txt My second modification to this file has been cancelled

Has been added, but has not been submitted
If we have added, then direct checkout will not work. We must cancel the addition before withdrawing the submission
git reset HEAD readme.txt
git checkout readme.txt

Version fallbackgit reset
If we have submitted but want to go back to the previous version, we can usegit resetcommand
For example, I was there first readme.txt Add a row to thegit add readme.txtgit commit -m "add the third step", add and submit to the warehouse
When finished, use thegit logCheck the submission records.

PS: you can see from this picture that the latest version number is displayed at the topgit reset --hard HEAD
In git, head is the current version, head is the previous version, and head ^ is the previous version
To go back to the previous version, you need to enter the following commands in turn
git reset --hard HEAD
git reset --hard HEAD^
The execution results are as follows:

Re inputgit logView, the version number has changed at this time, and the latest version number is not displayed

Git reset -- hard version number
This command allows us to change the current version to any existing version
At the same time, look at this picture carefully,

It’s easy to guess that the version number needs only the first seven digits
So our previous fallback instruction can be replaced by this one,
git reset --hard 418ee0fgit reflog
If you find that the latest version is better after you go back, it will pass at this timegit logWe can’t find that version number,
have access togit reflog

Obviously, the latest version number before the version fallback is14a5f92
Then use thegit reset --hard 14a5f92
open readme.txt The changes that were removed because of the version fallback are back

summary

This article about git local warehouse basic operation and skills of the article introduced here, more related git local warehouse basic operation content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!