Git—Basic knowledge of distributed version control system

1. Introduction to GIT

1. What is Git

 git is an open source distributed version control system for efficiently managing projects and files of various sizes.

2. The purpose of code management tools

  • To prevent code loss, make a backup
  • The version management and control of the project can be jumped by setting the node.
  • Establish their own development environment branches, independent of each other, easy to merge
  • Develop on multiple terminals to facilitate code project transfer

3. Commonly used code management tools

git、svn…

Two, GIT basic workflow

1. Git workflow

  • Workspace : It is the directory you can see on your computer.
  • Temporary storage area : English is called stage or index. It is generally stored in the index file (.git/index) under the .git directory, so we sometimes call the temporary storage area the index (index).
  • Repository : There is a hidden directory .git in the workspace. This is not a workspace, but a Git repository.
    Local Warehouse Workflow
Local Warehouse Workflow

2. Basic operation of Git

Git commonly used the following six commands: git clone, git push, git add, git commit, git checkout, git pull.

insert image description here

total workflow

illustrate:

  • workspace: workspace
  • Staging area: temporary storage area/cache area
  • local repository: repository or local repository
  • remote repository: remote warehouse

3. Initialize a new git repository

Right-click to select Git Bash Here-> enter $ git init

4. Add files and submit to the warehouse

 Files are added to the temporary storage area Command format: $ git add file name (Note: git add . is to submit all files)
 Submit the temporary storage area to the local warehouse and record the changes in the warehouse: $ git commit [file] -m 'operation Details' (the location of the file can be one or more files, not writing means to submit all the contents of the temporary storage area)
 Connect to the remote host: $ git remote add origin Remote warehouse address
 Finally upload the remote code and merge operation: $ git push -u origin main (the branch of the main warehouse, -u is only added for the first submission, origin is the remote host name)

5. Basic commands

$ git config: initial configuration
 git config --system [user.name username]: configure all user
 configuration file locations: /etc/gitconfig
 git config --global [user.mail mailbox]: configure the current user
 configuration file location: ~ /.gitconfig
 git config: Configure the current project
  configuration file location: project/.git/config
$ git status: View the status of the local warehouse.
$ git rm --cached foldername: Staging files for withdrawing commits.
$ git log : View commit log records.
$ git relog: View all commit operation records.
$ git log --pretty=online : View commit log records, each commit only displays one line
$ git diff file name: Compare the difference between the workspace and the temporary storage area files.

$ git checkout – file name: restore the original appearance of the file from the warehouse, and also restore the local files in the workspace deleted by $ rm file name through the warehouse.
$ git checkout branch name: switch branch

$ git mv [file] [path]: Move the file
$ git rm [file]: Delete the file
Note: These two operations will modify the content of the workspace and submit the operation record to the temporary storage area

6. Version control

$ git reset --hard HEAD^ : Roll back to the previous commit node, a ^ means roll back 1 version, and so on. After the version rolls back, the workspace will automatically be consistent with the current commit version.
$ git reset --hard [commit_id]: Roll back to the specified commit_id node (the first seven digits of the commit global unique code), even if you roll back to the state before the rollback, use relog to query the unique code, when there is a label, You can also replace the commit_id with the tag name
$ git tag [tag_name] (commit_id) -m 'message' : Add a snapshot at the important commit position of the project, save the current working state, generally used for version iteration, commit_id is default if not written View tagged in Latest Tags. Such as: git tag v1.0 -m 'version 1' at important nodes
$ git tag: view tags
$ git show v1.0: display tag details
$ git tag -d tag name: delete tag

7. Save the workspace

$ git stash save 'message' : Save the content of the workspace, description: seal up the uncommitted changes in the workspace, and let the workspace return to the state before the modification.
$ git stash list: View the workspace list, indicating that the latest saved workspace is at the top
$ git stash apply stash@{serial number}: apply a certain workspace, and then submit it to the warehouse with add and commit
$ git stash drop stash@{ serial number} : delete a workspace.
$ git stash clear : Delete all workspaces.

8. Branch management

Significance: branch means that everyone builds their own working environment on the basis of the original (code) branch, and develops independently without interfering with each other. After the development work is completed, the branches are unified and merged.
insert image description here

Branch schematic

$ git branch : View the branch situation, indicating that the branch with * in front indicates the current working branch.
$ git branch -a: With the -a parameter, you can view the remote branch, and the remote branch will be displayed in red
$ git branch [branch_name]: Create a branch, create a b branch based on the a branch, and then the b branch will have all of the a branch content, it is best to keep the a branch 'clean' when creating the b branch
$ git checkout -b branch name: create a branch and switch to the new branch

$ git merge branch name: Merge the specified branch to the current branch
Note: The conflict problem is the most difficult problem in the process of merging branches. When the branch is merged, the original branch and the previous changes will cause conflicts. When adding new modules (files) when merging branches, this conflict can be resolved automatically, and you only need to decide the commit operation yourself. When the two branches modify the same branch when merging branches, you need to manually resolve the conflict.
$ git branch -d branch name: delete the branch
$ git branch -D branch name: delete the branch that has not been merged

9. Remote warehouse management

$ git remote add origin remote warehouse address: connect to the remote host, only need to connect at the beginning
$ git remote rm origin: disconnect the origin remote host
$ git clone: ​​clone the remote warehouse
$ git push -u origin main: upload the remote code and Merge operation. main is the branch name of the warehouse, -u is added only when a new branch is uploaded for the first time, and origin is the name of the remote host. After connecting, submit directly git push
$ git push origin (–) tags: upload (all) tags
$ git push origin --delete tag tag name: delete tag name
$ git push origin: remote branch name: delete remote branch name
$ git push --force origin: push forcefully. The latest submission of the current local branch after reset is behind its corresponding remote branch, used when the update is rejected

$ git pull : pull the remote branch to the local
$ git fetch origin master:tmp : pull the remote branch master to the local, as a tmp branch
 difference: pull pulls the remote content directly to the local, and merges it with the corresponding branch content , fetch pulls the content of the remote branch to the local, but it will not merge with the corresponding local branch, you can use merge after judging by yourself

insert image description here

Detailed flow chart

Reference:
Reference 1 git-code management tool
reference 2 git installation and use
reference 3 git use detailed tutorial

Guess you like

Origin blog.csdn.net/weixin_44195690/article/details/128764727