Git Git Git Git Git Git Git Git Git

  • The new warehouse

    • git init
    • git clone
  • Basic operation

    • git add
    • git rm
    • git mv
    • git commit
  • Branch operation

    • git branch
    • git checkout
    • git merge
  • Remote operation

    • git remote
    • git push
    • git pull
  • Check the information

    • git status
    • git log
    • git show
    • git reflog
    • git diff
  • Cancel the operation

    • git reset
  • Delete operation

    • git clean
  • Modify the configuration

    • git config

1. New warehouse

  • Initialization:git init
> Initialize the local repository
> git init
Copy the code
  • Cloning:git clone
> # Kron Remote Warehouse
> git clone< remote warehouse address >
> Clone remote repository and specify local repository name
> git clone< remote warehouse address > < local warehouse name >
Copy the code

2. Basic operations

  • Add:git add
> # Add the specified file in the workspace to the staging area. The file must be untraceable or modified
>Git add
> Add all files in the workspace to the staging area, including new, modified, and deleted files
> git add -A
> git add --all
> Add all files in the workspace to the staging area, but not the new ones
> git add -u
> git add --update
> Add all files in the current directory in the workspace to the staging area
> In version 2.x, include new, modified, and deleted files
> # In 1.x, deleted files are not included
> git add .
Copy the code
  • Delete:git rm
> # Stop tracking the file and delete the file from your hard drive
>Git rm
> # Stop tracking the file and delete the file from your hard drive, or remove the file from the staging area if it is there
>Git rm --force
> Stop tracking the file, but keep it on your hard disk, or remove it from the staging area if it is there
>Git rm --cached
Copy the code
  • Mobile:git mv
> Can be used for renaming and putting changes into staging
>Git mv git mv
Copy the code
  • Commit:git commit
> Commit the file in the staging area, then open the default editor to write the commit information
> git commit
> Submit the file in the staging area directly with the submission information
>Git commit -m
> # Put the files changed and deleted from the workspace into the staging area, and then commit the files in the staging area
> git commit -a
> Override the previous commit with a new commit
> git commit --amend
Copy the code

3. Branch operation

  • Branches:git branch
> # View local branches
> git branch
> View the local branch and display the most recent commit information
> git branch -v
> git branch --verbose
> # View remote branches
> git branch -r
> git branch --remote
> # View all branches
> git branch -a
> git branch --all
> Local branch = remote branch = remote branch = remote branch
> git branch -vv

> Create a new branch based on the current branch
>Git branch
> Create a new branch based on the current branch and associate it with a remote specified branch
>Git branch --set-upstream < upstream >

> # delete branch
>Git branch -d
>Git branch --delete < branch >
> # Force drop branch
>Git branch -d
>Git branch --delete --force

> Associate the current branch with the remote specified branch
>Git branch -u
> Associate the specified branch with the remote specified branch
>Git branch -u < remote branch name >
Copy the code
  • Check out:git checkout
> # Switch to an existing branch
>Git checkout
> Create a new branch based on the current branch and switch to that branch
>Git checkout -b
> Create a new branch based on the local specified branch, and switch to the branch
>Git checkout -b < new branch > < local branch >
> Create a new branch and switch to the branch based on the remote specified branch
>Git checkout -b < new branch name > origin

> # Specifically: Used to undo workspace - specified file modifications
> If the file is not in staging after the change, go back to the version of the repository (the last Git commit)
> If the file is in the staging area and changes are made, go back to the version of the staging area (the last git add)
>Git checkout < file name >
>Git Checkout -- < filename >
Copy the code
  • Merger:git merge
> Fast forward merge (if the specified branch header is the ancestor of the current branch header, it is called fast forward merge)
> If the merge is fast forward, the current branch head is pointed to the specified branch head, and no merge commit occurs
> # if the merge is not fast-forward and there are no conflicts, a merge commit is produced
>Git merge
> # Merge the specified branch into the current branch, resulting in a merge commit even if it can be resolved as a fast-forward merge
>Git merge --no-ff < branch name >
> # merge the specified branch into the current branch. If it cannot be resolved as a fast-forward merge, the merge will be rejected and exit
>Git merge --ff-only

> If a merge conflict occurs, either abort the merge and return to the state before the merge
> git merge --abort
> Either resolve the conflict manually and resubmit a merge commit later
> git add --all
> git commit
Copy the code

4. Remote operation

  • Remote warehouse:git remote
> Display all remote repositories
> git remote
> Display the details of the specified remote repository
>Git remote show
> Add a new remote repository
>Git remote add
> Change the remote warehouse address
>Git remote set git remote set
> Delete the remote repository
>Git remote rm
> Rename the remote repository
>Git remote rename
Copy the code
  • Push:git push
> Push the current branch to the associated branch of the remote repository
> git push
> Push all branches to the associated branch of the remote repository
> git push --all
> Push the current branch to the associated branch of the remote repository, regardless of whether there is a conflict
> git push -f
> git push --force
> Push the current branch to the specified branch of the specified remote repository
>Git push < remote repository name >
> Push the current branch to the specified branch of the specified remote repository, and associate the current branch with the remote branch
>Git push -u < remote repository name >
>Git push --set-upstream < upstream >
> Drop the specified branch of the specified remote repository
>Git push < remote repository name >
>Git push < remote repository name >
Copy the code
  • Pull:git pull
> # pull the associated branches of the current branch and merge into the current branch
> git pull
> Pull the specified branch of the specified remote repository and merge it into the current branch
>Git pull < remote repository >
Copy the code

5. Information viewing

  • To view the current status:git status
> # View the changed file
> git status
Copy the code
  • To view the submission record:git log
> Check all commit records for the current branch
> git log
> Check the last two commits of the current branch
> git log2 -
> Use only one line to represent a single commit
> git log --oneline
> Display the files that change each time you commit
> git log --stat
> Display the commit record in the form of graph, you can see the branch merge information
> git log --graph --oneline
> Only commit records with the specified string in the commit message will be displayed
> git log--grep < string >
> Only commit records that contain the specified file in the change file will be displayed
> git log--follow < file name >
Copy the code
  • To view submission information:git show
> Display the last committed metadata and changed content
> git show
> Display the last committed metadata and the files that changed
> git show --name-only
> Display the metadata and changes specified for submission
> git show <commit id>
> Displays changes to the specified file in the specified commit
>Git show 
      
       :
      
Copy the code
  • View command records:git reflog
> View command logs, including deleted commands
> git reflog
Copy the code
  • View the differences:git diff
> View the difference between the staging area and the workspace
> git diff
> Check the differences between the staging area and the repository
> git diff --cached
> Check the differences between workspaces and repositories
> git diff HEAD
> For a given file, see the difference between the staging area and the workspace
>Git diff < file name >
> For a given file, see the difference between the staging area and the repository
>Git diff --cached
> # For a given file, see the difference between workspace and repository
>Git diff HEAD
Copy the code

6. Undo operation

  • Back:git reset
> # set the HEAD pointer to the specified version, reset the staging area, and leave the workspace unchanged
> git reset <commit id>
> # set the HEAD pointer to the specified version, the staging area remains unchanged, and the workspace remains unchanged
> git reset --soft <commit id>
> # set the HEAD pointer to the specified version, reset the staging area, reset the workspace
> git reset --hard <commit id>

> Back to the previous version
> git reset HEAD^
> git reset HEAD~1
> Go back to the previous three versions
> git reset HEAD~3

> # special: Remove the specified file from the staging area, preserving the state of the workspace
>Git reset HEAD
Copy the code

7. Delete operations

  • Delete:git clean
> Delete untracked files, excluding ignored files
> git clean -f
> Delete untraceable files and include only ignored ones
> git clean -f -X
> Delete untracked files, both ignored and not ignored
> git clean -f -x

> Delete untracked files and directories, excluding ignored files
> git clean -f -d
> Delete files and directories that are not tracked, and only include those that are ignored
> git clean -f -X -d
> Delete untracked files and directories, including ignored files and directories
> git clean -f -x -d

> # Only view the content to delete, not actually delete it
> git clean -n
Copy the code

8. Modify the configuration

  • Configuration:git config
> List all configurations
> git config --list
> # list local configuration (for current directory)
> git config --list --local
> # list global configuration (for current user)
> git config --list --global
> # list system configuration (for all users)
> git config --list --system
> # Configure personal information
> git config user.name "user name"
> git config user.email "user email"
> # configuration source
> git config get registry
> git config set registry https://registry.npmjs.org # the official source
> git config set registry https://registry.npm.taobao.org # taobao source
> # configure proxy
> git config http.proxy "proxy address"
> git config https.proxy "proxy address"
> # Cancel agent
> git config --unset http.proxy
> git config --unset https.proxy
Copy the code

For a more comprehensive and detailed command usage, see the official documentation.