1. Obtain the Git version number and check whether the installation is successful

git --version
Copy the code

2. Configure information before use

# name mailbox configuration
git config --global user.name yourname
git config --global user.email youremail

Git configuration
git config --list
Copy the code

3. Configure security-free SSH

# Check the SSH key
ls -al ~/.ssh

Create a new SSH key on Github. https://help.github.com/articles/generating-ssh-keys
ssh-keygen -t ed25519 -C "[email protected]"
# or
ssh-keygen -t rsa -b 4096 -C "[email protected]"

Open the public key file, copy it, add it under Github - settings-ssh and GPG keys
cat ~/.ssh/id_ed25519.pub
Copy the code

4. Initialize the warehouse and commit the staging area and workspace

Initialize the repository
git init

Check the current file status
git status

Add the file to the cache
git add filename.txt
git add .

Commit the file to the workspace
git commit -m "feat: add some commit"
You will only have one commit -- the second commit will replace the result of the first.
git commit -m 'initial commit'
git add forgotten_file
git commit --amend

View the commit log
git log
git log --oneline
Copy the code

5. Cancel the operation

Overwrite the workspace file filename.txt with a file from the remote repository
git checkout -- filename.txt
After Git 2.23, overwrite local files with files in staging or repository
git restore

Git add filename.txt
git reset HEAD filename.txt

# rollback files that have been committed to the remote repository to a version number (commits after the specified version number are deleted, suitable for private branches)Git reset -- Hard version numberRoll back the previous version of HEAD
git reset --hard HEAD~
git reset --hard HEAD^

# Undo a commit (Undo the changes introduced by the specified version number, then add a new commit that undoes the changes. The commit after the specified version number remains, suitable for the public branch.)Git Revert version numberCopy the code

6. Temporary storage operation

Create a staging area
git stash

# Check the staging area
git stash list

Restore last staging and delete current staging
git stash pop

Clear all temporary storage
git stash clear
Copy the code

7. Remote warehouse operations

Clone from remote repository
git clone <url>

Mysql > alter database name = origin
git remote add <name> <url>

Delete the remote repository
git remote rm <name>

# pull code
git pull
git pull origin master

# push dev branch and create it to remote repository if the remote repository does not already have dev branch
git push origin

# push local dev branch to remote dev branch
git push origin dev

Push all local branches to the remote repository
git push -all <name>
Copy the code

8. Branch operations

# view branch
git branch

# View local and remote branches
git branch -a

Create the dev branch
git branch dev

Switch to the dev branch
git checkout dev

Create and switch to the dev branch
git checkout -b dev

# merge dev branch to test, should have already switched to test
# 
git merge dev

# delete dev_sub branch created locally
git branch -d dev_sub

Delete origin dev2 branch from remote repository
git push origin -d  dev2
Copy the code