The command

git clone– Clone a repository to a new directory

#Clone the remote repository to the current directory
$ git clone https://github.com/leetcode.git
Copy the code

git init– Create an empty Git repository or re-initialize an existing Git repository

$ git init
Initialized empty Git repository in D:
Copy the code

git add– Add files to index (support interactive mode)

#Add all *.txt files in the path directory and its subdirectories to the index
$ git add path/\*.txt
#Add all *.txt files in the path directory to the index
$ git add path/*.txt
#Adds all files in the current directory and its subdirectories to the index
$ git add .
Copy the code

git status– Displays the working tree status

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   subdir/sub.txt
        new file:   subdir/sub1.txt
        new file:   test.txt

Copy the code

git commit– Records changes to the local repository

Parameter: -a --all Saves files that have been modified and deleted, but new files that are not notified to Git by the add command are not affected. -m < MSG > --message=< MSG > Uses the MSG after this parameter as the comment for submission. If there are more than one -m instruction, their MSGS will be spliced together.


#TXT file to the local repository, and commit the comments as MSg1, MSg2
$ git commit test.txt -m 'msg1' -m 'msg2'
[master (root-commit) 7345e2c] msg1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
 
#Commit the modification records of all files to the local repository with the commit MSG note
$ git commit -am 'commit msg'
Copy the code

git log– Display submission history (When there are too many records, use the Enter key to view more records, and use the Q key to exit the view)

#You can see the previous commit record
$ git log
commit 7345e2cf002e8ca0392ac56fd679e1725acd14da (HEAD -> master)
Author: Biubiu <[email protected]>
Date:   Mon Aug 2 22:30:06 2021 +0800

    msg1

    msg2

#Just look at one historical record
$ git log -n 1
commit 1e8a38425e4a807bd5dd1b0c5cbe8e28821fc037 (HEAD -> master)
Author: Biubiu <[email protected]>
Date:   Mon Aug 2 22:37:06 2021 +0800

    update test.txt

Copy the code

git branch– Show, create, or delete branches

#createtestbranch
$ git branch test

#Viewing local branches
$ git branch
* master
  test

#Viewing remote branches
$ git branch -r

#Forcibly delete the local branch
$ git branch -D test
Deleted branch test (was 1e8a384).

#Change the current branch name to dev
$ git branch -m dev
Copy the code

git checkout– Switch branches or restore working tree files

#Switch to the Master branch
$ git checkout master
Switched to branch 'master'

#Create a New one locallytestBranch, and track the remote Origin repositorytestbranch
$ git checkout -b test origin/test

Copy the code

git merge– Merge multiple commit histories

#testMultiple commit records on the branch are merged into the current branch
$ git merge test
Copy the code
  • fatal: refusing to merge unrelated historiesProblem solving
#Where two branches are not related, a direct merger can lead to an error
#In this case, run the following command
$ git merge test --allow-unrelated-histories
Copy the code

git stash– Hide changes in a temporary working directory

#Hide the current changes and add a note MSG
$ git stash -m 'msg'

Copy the code

git fetch– Pull the latest contents and changes of the remote repository

#Pull the contents and modifications in the remote Origin repository
$ git fetch origin

#Be aware of local outdated remote branch references before pulling
$ git fetch -p
or
$ git fetch --prune
Copy the code

git pull– Pull content and changes from other branches of the remote or local repository and merge with them

$ git pull origin nextIs equivalent to$ git fetch origin
$ git merge origin/next
Copy the code

git push– Commits local changes to the remote repository to update the contents in the remote repository

#Commit local changes to the Origin remote repository
$ git push origin test:test

#Delete the dev branch of the Origin remote repository
$ git push -d origin dev
Copy the code

git remote– Manage remote warehouses for tracking

#Add a remote trace repository for the local repository, alias Origin
$ git remote add origin https://github.com/leetcode.git

#Update the list of remote branches cached locally
$ git remote prune origin
Copy the code

git cherry-pick– Merges a commit to the current branch

#Pull based on the commit- ID
$ git cherry-pick 1e8a38425e4a807bd5dd1b0c5cbe8e28821fc037
Copy the code

git revert– Restore some commits

#Restore based on the commit- ID
$ git revert 7345e2cf002e8ca0392ac56fd679e1725acd14da
Copy the code

git config– Query or warehouse/global options

#Set the user name and email address
$ git config --global user.name [username]
$ git config --global user.email [email]

#Query options
$ git config -l
Copy the code

operation

There is already a code file locally that needs to be associated with the remote repository

$ git init
$ git add .
$ git commit am "init repo"
$ git branch -M master
$ git remote add origin https://github.com/leetcode.git
$ git push origin master
Copy the code

There are some local changes, but they are not finished yet and need to change other things

#Hide the files modified in the current workspace
$ git stash -m 'create user function - undone'
#View the Hidden list
$ git stash list
#0 indicates the latest hidden change. Using the pop command, the hidden change will be applied to the current branch and the hidden record in the hidden list will be deleted
$ git stash pop stash@{0}
Copy the code

reference

Git Official documentation