Create a SSH Key

$ ssh-keygen -t rsa -C "[email protected]"
Copy the code

Configuring User Information

$ git config --global user.name "Your Name"             
$ git config --global user.email "[email protected]"
Copy the code

warehouse

Create a new Git code base in the current directory

$ git init
Copy the code

Create a new directory and initialize it as a Git code base

$ git init [project-name]
Copy the code

Download a project and its entire code history

$ git clone [url]
Copy the code

Add/delete files

Adds the specified file to the staging area

$ git add [file1] [file2] ...
Copy the code

Adds the specified directory to the staging area, including subdirectories

$ git add [dir]
Copy the code

Adds all files from the current directory to the staging area

$ git add .
Copy the code

Before adding each change, you are asked to confirm that multiple changes to the same file can be committed in batches

$ git add -p
Copy the code

Delete the workspace file and place the deletion in the staging area

$ git rm [file1] [file2] ...
Copy the code

Stops tracking the specified file, but it remains in the workspace

$ git rm --cached [file]
Copy the code

Rename the file and place the rename in the staging area

$ git mv [file-original] [file-renamed]
Copy the code

Submit code

Submit temporary storage area to warehouse area

$ git commit -m [message]
Copy the code

Commit the changes to the workspace since the last commit, directly to the warehouse

$ git commit -a
Copy the code

Display all diff information when committing

$ git commit -v
Copy the code

A new COMMIT is used to override the commit information from the previous commit if there are no new changes in the code

$ git commit --amend -m [message]
Copy the code

Redo the last COMMIT and include new changes to the specified file

$ git commit --amend [file1] [file2] ...
Copy the code

Check the information

The changed file is displayed

$ git status
Copy the code

Displays the version history of the current branch

$ git log
Copy the code

Displays the commit history and the files that changed each commit

$ git log --stat
Copy the code

Search submission history by keyword

$ git log -S [keyword]
Copy the code

Displays all changes after a commit, with each commit occupying one row

$ git log [tag] HEAD --pretty=format:%s
Copy the code

Displays all changes made after a COMMIT whose “commit instructions” must match the search criteria

$ git log [tag] HEAD --grep feature
Copy the code

Displays the version history of a file, including file renaming

$ git log --follow [file]
Copy the code

Displays each diff associated with the specified file

$ git log -p [file]
Copy the code

Displays the last five commits

$ git log -5 --pretty --oneline
Copy the code

Displays all submitted users, sorted by number of submissions

$ git shortlog -sn
Copy the code

Show who modified the specified file and when

$ git blame [file]
Copy the code

Shows the difference between staging area and workspace

$ git diff
Copy the code

Shows the difference between the staging area and the last COMMIT

$ git diff --cached [file]
Copy the code

Displays the difference between the workspace and the latest COMMIT for the current branch

$ git diff HEAD
Copy the code

Displays the difference between two commits

$git diff [first-branch]... [second-branch]
Copy the code

Shows how many lines of code you wrote today

$ git diff --shortstat "@{0 day ago}"
Copy the code

Displays metadata and content changes for a commit

$ git show [commit]
Copy the code

Displays the files that have changed during a commit

$ git show --name-only [commit]
Copy the code

Displays the contents of a file at the time of a commit

$ git show [commit]:[filename]
Copy the code

Displays the most recent commits for the current branch

$ git reflog
Copy the code

branch

Lists all local branches

$ git branch
Copy the code

List all remote branches

$ git branch -r
Copy the code

Lists all local and remote branches

$ git branch -a
Copy the code

Create a new branch, but remain in the current branch

$ git branch [branch-name]
Copy the code

Create a new branch and switch to it

$ git checkout -b [branch]
Copy the code

Create a new branch that points to the commit

$ git branch [branch] [commit]
Copy the code

Create a new branch and establish a trace relationship with the specified remote branch

$ git branch --track [branch] [remote-branch]
Copy the code

Switch to the specified branch and update the workspace

$ git checkout [branch-name]
Copy the code

Switch to the previous branch

$ git checkout -
Copy the code

Establish a trace relationship between an existing branch and a specified remote branch

$ git branch --set-upstream [branch] [remote-branch]
Copy the code

Merges the specified branch into the current branch

$ git merge [branch]
Copy the code

Select a COMMIT and merge it into the current branch

$ git cherry-pick [commit]
Copy the code

Delete the branch

$ git branch -d [branch-name]
Copy the code

Deleting a Remote Branch

$ git push origin --delete [branch-name]
Copy the code

The label

List all tags

$ git tag
Copy the code

Create a new tag for the current commit

$ git tag [tag]
Copy the code

Create a new tag before specifying commit

$ git tag [tag] [commit]
Copy the code

Deleting a Local Tag

$ git tag -d [tag]
Copy the code

Deleting a Remote Tag

$ git push origin :refs/tags/[tagName]
Copy the code

Viewing tag Information

$ git show [tag]
Copy the code

Submit the specified tag

$ git push [remote] [tag]
Copy the code

Submit all tags

$ git push [remote] --tags
Copy the code

Create a new branch that points to a tag

$ git checkout -b [branch] [tag]
Copy the code

Remote synchronization

Download all changes to the remote repository

$ git fetch [remote]
Copy the code

Display all remote warehouses

$ git remote -v
Copy the code

Displays information about a remote repository

$ git remote show [remote]
Copy the code

Add a new remote repository and name it

$ git remote add [shortname] [url]
Copy the code

Retrieve the changes from the remote repository and merge them with the local branch

$ git pull [remote] [branch]
Copy the code

Allows unrelated history submissions and forces merging

$ git pull origin master --allow-unrelated-histories
Copy the code

Upload the local specified branch to the remote repository

$ git push [remote] [branch]
Copy the code

Push current branch to remote repository forcibly, even if there are conflicts

$ git push [remote] --force
Copy the code

Push all branches to the remote repository

$ git push [remote] --all
Copy the code

undo

Restores the specified file from the staging area to the workspace

$ git checkout [file]
Copy the code

Restores the specified files of a COMMIT to the staging and workspace

$ git checkout [commit] [file]
Copy the code

Restores all files from the staging area to the workspace

$ git checkout .
Copy the code

Resets the specified file in the staging area, same as the last commit, but with the same workspace

$ git reset [file]
Copy the code

Reset the staging area and workspace as the last COMMIT

$ git reset --hard 
Copy the code

Reset the pointer to the current branch to specify COMMIT, and reset the staging area, but the workspace remains the same

$ git reset [commit]
Copy the code

Reset the HEAD of the current branch to specify a COMMIT, and reset the staging area and workspace to match the specified COMMIT

$ git reset --hard [commit]
Copy the code

Reset the current HEAD to specify commit, but leave the staging area and workspace unchanged

$ git reset --keep [commit]
Copy the code

Create a COMMIT that cancels all changes to the specified COMMIT and applies them to the current branch

$ git revert [commit]
Copy the code

Uncommitted changes are temporarily removed and moved in later

$ git stash
$ git stash pop
Copy the code

Ignore file configuration (.gitignore)

1. Configuration syntax:

Directories begin with a slash “/”;

Wildcard multiple characters with asterisk “*”;

With a question mark “?” Wildcard single character

Contains a list of matches for single characters in square brackets “[]”;

With exclamation “!” It does not ignore (trace) matched files or directories.

In addition, Git matches the.ignore configuration file from top to bottom, which means that if the previous rule matches more broadly, the later rule will not take effect.

2. Examples:

(1) Rule: fd1/* Note: Ignore all contents under directory fd1. Note that either the /fd1/ directory in the root directory or a subdirectory /child/fd1/ is ignored;

(2) Rule: /fd1/* Note: Ignore all contents of the /fd1/ directory under the root directory.

(3) Rules:

/ *! .gitignore ! /fw/bin/ ! /fw/sf/

Note: Ignore all contents except.gitignore files and /fw/bin/ and /fw/sf/ directories in the root directory.