basic operation
1、 Basic operation command
- Create an empty folder
- Right click and click git bash here to start the command line (MAC opens the terminal in the current folder)
git init
Warehouse initialization- Create an initialization file index.html
git add index.html
Add files to the staging areaGit commit - M 'comment'
Submit to warehouse m is short for message
2、 . git directory
- Hooks directory contains client or server hook scripts, which are executed automatically under specific operations.
- Info contains a global exclusion file that can be ignored by the configuration file
- Logs saves log information
- The objects directory stores all the data content, and the local version inventory is put in the location
- The refs directory stores a pointer (Branch) to the submitted object of the data
- The config file contains project specific configuration options
- Description is used to display the description of the warehouse
- The head file indicates which branch is currently checked out
- Index buffer data
- Remember: don’t modify the contents of the. Git folder manually*
3、 Three areas of the repository
- Workspace (code editing area): represents the place where code is developed locally
- Temporary storage area (modify to be submitted area): the address representing the temporary storage code of the local warehouse
- Warehouse area (code saving area): represents that the code enters local version control
Common commands
1、 Common commands
git status
Version status view
Red: the document is in the workspace
Green: the document is in the staging area
It is not reflected. The description is in the version area
git add -A
Add all new files to the staging area (orgit add .
git add *
)
usegit restore
Changes to the workspace can be discarded
Git commit - M 'comment'
Submit modification and comment
useGit restore -- staged < File >
Temporary storage can be cancelled
git diff
View the differences between the workspace and the staging area (do not show deleted or new files) to show what changes have been made
//Interpreting the results
lipeihuadeMacBook-Pro% git diff
//In comparison, index.html (before change) index.html (i.e. after the change).
diff --git a/index.html b/index.html
//Represents the GIT hash value of two versions
index 16158b4..61045cd 100644
//"---" indicates the version before the change
--- a/index.html
//"+ +" indicates the changed version
+++ b/index.html
//Line 1-2 of the source file is different from line 1-5 of the target file;
@@ -1,2 +1,5 @@
//-The red part indicates the reduced part and the + green part indicates the increased part
index.html
-no 1
+
+
+
+Revise again
//\No newline at end of file
\ No newline at end of file
git diff --cached
View differences between staging area and warehouse
2、 Historical version rollback
2.1 view historical version
git log
By default, without any parameters, GIT log will list all the updates according to the submission time, and the latest updates are at the top. Each update has a SHA-1 check sum, author’s name and email address, submission time, and finally a paragraph indented to show the submission instructions.
git log –oneline
If the content is too much, you need to use the direction keys to scroll up and down, press
q
sign out
git log
The command has an option to change how warehouse information is displayed. This option is--oneline
.Each line displays a commit. The first seven characters of Sha display the commit message
2.2 roll back according to version number
Version rollback, local only. It doesn’t affect the content of GIT library.
git reset –hard b815fd5a6ae655b521a31a9
For version rollback, you don’t need to use the complete hash string, just the first seven digits
Before version switching, submit the current code status to the warehouse
git reflog
If you want to go back to the previous version after rollback, GIT reflog can view all operation records of all branches (including commit and reset operations), including the deleted commit records. Git log cannot view the deleted commit records
2.3 other rollbacks (understanding)
Git reset -- hard head ^ rollback to previous version
Git reset -- hard head ^ rollback to previous version
Git reset -- hard head ~ 100 roll back to before 100 versions
Configuration ignore file
1、 The file was not submitted in the warehouse
Some files in the project do not need to enter the version library, such as the configuration of the editor. A file needs to be created in GIT. gitignore is the same level directory as. Gitignore.
#Ignore all. Idea folders
.idea
#Ignore all files ending in. Test
*.test
#Ignore node_ Modules files and folders
/node_modules
2、 The file has been submitted in the warehouse
For a file that has been added to the version library, you can delete the file in the version library
git rm --cached .idea
git rm --cached ./css/go.css
Then configure ignore in. Gitignore
.idea
/css/go.css
Just submit add and commit
branch
Branching is one of the important features of GIT. Developers can separate new development lines on the basis of main development lines.
Create branch
Name is the name of the branch
git branch name
View branches
git branch
Switch branches
git checkout name
Merge branches
git merge name
Delete branch
git branch -d name
Switch and create branches
git checkout -b name
Note: submit the current branch before switching branches
conflict
When multiple branches modify the same file, there will be conflicts when merging branches. The solution to the conflict is very simple. Modify the content to the desired result, and then continue to execute git add and git commit.
Here is the article about the basic operation and instruction of GIT. For more information about the basic operation and instruction of GIT, please search previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!