Git installation address Official: git-scm.com/ mirror: npm.taobao.org/mirrors/git…

What is Git?

Git is a free, distributed file version control tool. Git is currently the most popular version control tool. Git is managed locally without the support of a remote repositoryCopy the code

Git family members?

Git: a version control system that is a command and a tool. Gitlib: a development library used to implement Git functionality; Github is a remote code hosting repository based on Git implementation, open to the Internet. GitLab is a remote code hosting repository based on Git implementation, generally used in corporate Intranet environment. 【 Enterprise Edition 】Copy the code

Integrated versus distributed

Git local repository: the operation records are stored locally, and the remote repository can be pushed when there is a network

.gitignore

Git abandons monitoring of specified files.

# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
Copy the code

Git Principles

Vim common commands

The vim command will be supplemented later

# exit and save Esc + :wqCopy the code

Git git

Git file commands

# directory switchCD folder CD..Empty the command line
clear

# add folder
mkdirFolder name# add fileOpen the file in Vim mode, and save the content of the file Esc + :xprintf 'content'< span style = "box-sizing: border-box! Important; word-wrap: break-word! Important;'content'Create a new file and write the content into the file.# delete folder
rmdirFolder name# delete fileThe rm file name# View file contentsThe cat filenameCheck the path of the current file
pwd

# check the list of files in this directory.
ls

# check the list of files in this directory.
ls -a

View the details of the files in this directory
ls -l
Copy the code

Set the user information and SSH key

Set user name, email address
git config --global user.email 'email'       
git config --global user.name 'Username'  
    
Set git repository username and email address
git config --local user.email 'email'
git config --local user.email 'Username'

Git git configuration
git config -l   

Create and view SSH keysCreate: ssh-keygen -t rsa-c'email'SSH: CD ~/. SSH: ls check whether there are id_rsa and id_rsa.pub files. To check the public key: cat id_rsa.pubCopy the code

Basic commands

# git check version
git --version

# updated git
git update-git-for-windows
Copy the code
Git init creates a git folder in your project folder.
Initialize git repository
git init   

Create a new folder and initialize the Git repositoryGit init fileCopy the code
# add- Submit code to staging area
# add all untraced files and modified files in the current directory to temporary storage (excluding deleted files)
git add .

Commit the specified file to the staging areaGit add name of the fileDelete file (not new file)
git add -u
Copy the code
# commit- Commit code to the repository
Commit staging files to your local Git repository
git commit -m  "Comment"  

Commit specific files (folders) to your local Git repositoryGit commit file name (folder) -m"Comment"

# add/commit
git commit -am "Comment"Only files that have been managed by Git before can be used.No rule verification is performed when submitting code to a local Git repository
git commit -m "Comment" -n  
 
Git log will not add a new commit to the git log.
git commit --amend --no-edit 

Git log: "git commit";
git commit --amend -m "Comment"
Copy the code
Check the current status of the repository
git status   
Copy the code

Delete the file

Delete the real fileGit rm name-f "git" -f "-f" -f "-f" -f "-f" -f "-f" -f "-f" -f "-f" -fGit rm -f File nameDelete files from the repository, temporary storage, and commitGit rm -- Cache file nameCopy the code

See the log

View the history of the current branch
git log  
 
View the change file each time you commit
git log --stat
 
# view the specified number of commits
git log -2-- Pretty -- OnelineRecord every operation of the computer
git reflog

# view the last change you committed
git show   

# view the changes specified for submission
git show commitId
Copy the code

Rollback, reverse election, undo

undo

Undo workspace

Changes have only been made in the workspace and have not yet been committed to staging and repository, i.e., add and COMMIT operations.

Undo the modification of the specified fileGit checkout -- git restore -wUndo all workspace files for this change
git checkout -- .
git restore -W .   

Replace the current file in the workspace with the specified file from the last committed versionGit checkout HEAD -- git restore -s HEAD~1The file name# replace the current file in the workspace with the file in the specified committed versionGit checkout CommitId -- git checkout CommitIdCopy the code

Revoke the staging area

The modified file has been committed to staging, not to repository, add operation, but not commit operation.

Unadd the specified file and make it untraceable
git resetGit restore -s Specifies the name of the git fileUndo add for all files
git reset HEAD .
git restore -S .   
Copy the code

Undo version library

The file has been committed to the repository, rollback to the staging state

# undo commit repository operation for specified file and return to staging area
git reset --soft HEAD^
Copy the code

The rollback

# discard changes
git reset

Version rollback only, workspace will not be updated
git reset HEAD^

Git log will not be the same as the previous version, and the files in the workspace will be reverted to the previous version
git reset --hard HEAD^

# roll back the repository to three versions without modifying the staging area and workspace
git reset --soft HEAD^^^

# roll back the repository and staging area to the previous 4 versions without modifying the workspace
git reset --mixed HEAD~4 

Restore the workspace, staging area, and repository to the specified version
git reset --hard CommitId 
Copy the code

Git reset –hard CommitId rollback to the specified version. Git reset –hard HEAD^^ Version 1 — version 2 — version 3 Git log to view the history, there is only one commit for version 1 left. Git Push Origin Master will fail to commit to the remote repository after version rollback. Git push -f Origin Master force push code, remote repository will also delete version 2 and 3 commit records.

Git reset –hard resets the code to the specified version. Git reflog can view rollback code records. Git reset –hard CommitId Resets to the specified version before deleting.

The selected

Git generates a new commit that removes the specified commit from the current branch
git revert CommitId
Copy the code

Git Revert CommitId is a reverse versionrevert. Git Revert creates a COMMIT to override the current COMMIT. Git revert version 2CommitId locally differentiates between version 2 and post-version code.

<<<<<<< HEAD
222222222222222222222222222
1111111111111111111111111
=======
>>>>>>> parent of cf21934... to#c33
Copy the code

Manual code changes are required.

Viewing version Changes

# Difference comparison between workspace and staging area
git diff

# Comparison of differences between staging and repository
git diff --cached

Compare the differences between workspaces and version libraries
git diff master
Copy the code

Label management

Create a new tag for the current commitGit tag Specifies the git tag name# Create a new tag before specifying commitGit tag Tag name commitId For example, git tag V0.9 4ab025

# view all tags
git tag

# Check the gap with a tagGit show tag name# delete the local tagGit tag -d Specifies the tag name# delete remote tag
git pushOrigin :refs/tags/ tag nameCopy the code

branch

# Switch branchesGit checkout branch git switch2.23Release 】Create a new branch and switch to the specified branchGit checkout -b git switch -c2.23Release 】Copy the code
# check how many branches there are locally
git branch

Rename the current branchGit branch -m Specifies the branch nameCreate a new branch, but it will not automatically switch to that branchGit branch Specifies the branch nameCreate a new branch and specify a commitGit branch The branch name is commitIdCreate a new branch and set up a trace relationship with the specified remote branchGit branch --track Branch name Name of the remote branchView the branch of the remote repository
git branch -r

View the local and remote repository branches
git branch -a

Check the last submitted object of each branch
git branch -v

# check the merged branches of the current branch
git branch --merged

# view branches that are not merged into the current branch
git branch --no-merged

# delete the specified branchGit branch -d Specifies the branch name# delete the specified branch forciblyGit branch -d Specifies the branch name# delete remote branch
git push --deleteBranch name originCopy the code
# merge branchesGit merge branch nameCopy the code

Remote warehouse

Check the remote host name
git remote

# View more remote information
git remote -v 

Check the remote warehouse address
git remote show origin

Associate the remote repositoryGit remote add Origin Specifies the remote repository addressUnbind the remote repositoryGit remote rm Remote library addressCommit the contents of this branch to the specified branch of the remote repository
git pushRemote host name Branch name Example: Gitpush origin daily1

# Force commit code to remote repository
git push-f origin Specifies the name of the remote branch# delete remote branch
git push --deleteBranch name originCopy data from remote repositoryGit Clone repository address# merge remote repository code locallyGit pull Remote host name Branch name Example: Git pull Origin daily1Copy the code

conclusion

Git operation command more, so make a note for everyone to refer to. Write nuggets for the first time, the article format may not be very formal, later will be changed at any time.