Git's common command Guide

Posted by Hobgoblin11 on Sun, 27 Feb 2022 17:23:54 +0100

reference material: https://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

Common Git commands

What is Git?

Git is a distributed version control system. Different from the centralized version control system, everyone works in the local version library established through cloning. In other words, everyone has a complete version library. All operations such as viewing submission log, submitting, creating milestone and branch, merging branch and fallback are completed directly locally without network connection;

Before using git, we need to know several special nouns:

**Work area: * * the place where we modify the file is the directory you can see on the computer;

**Temporary storage area: * * it is used to temporarily store the modified contents in the work area. It is generally stored in the In the index file (. git/index) under git directory, we often use git add Is to push the contents of the work area to the temporary storage area;

**Local warehouse: * * workspace has a hidden directory Git is not a workspace, but a git version library. Generally, we use git commit -m "" to push the files in the staging area to the local warehouse;

**Remote warehouse: * * the remote warehouse corresponding to the local warehouse. Generally, the warehouse we put on the github;

Key concepts:

HEAD: in fact, the branch in Git is only a variable pointer to the commit object, and HEAD is a pointer to the current workspace. Therefore, HEAD - > Master in the command line refers to: the current branch points to the master branch, and git reset HEAD < File > refers to restoring to the state of the file in the current branch;

Recommended learning tools: https://learngitbranching.js.org/?locale=en_US

New code warehouse

# Create a Git code base in the current directory
$ git init

# Create a new directory and initialize it as a Git code base
$ git init [catalogue]

# Download a project and its entire code history
$ git clone [address]

# Displays the current Git configuration
$ git config --list

# Set the user information when submitting code. If the -- global parameter is removed, it is only valid for the current warehouse
$ git config [--global] user.name "full name"
$ git config [--global] user.email "e-mail address"

Submission and modification

# Adds the specified file to the staging area
$ git add [file1] [file2] ...

# Adds the specified directory to the staging area, including subdirectories
$ git add [dir]

# Add all files in the current directory to the staging area
$ git add .

# Delete the workspace file and put the deletion into the staging area
$ git rm [file1] [file2] ...

# Stops tracking the specified file, but the file remains in the workspace
$ git rm --cached [file]

# Rename the file and put the rename in the staging area
$ git mv [file-original] [file-renamed]

# Submit staging area to warehouse area
$ git commit -m [message]

# Submit the specified files in the temporary storage area to the warehouse area
$ git commit [file1] [file2] ... -m [message]

# Submit the changes in the workspace since the last commit and go directly to the warehouse area
$ git commit -a

# Show all diff information when submitting
$ git commit -v

# Use a new commit instead of the last commit
# If there are no new changes in the code, it is used to rewrite the submission information of the last commit
$ git commit --amend -m [message]

# Redo the last commit and include new changes to the specified file
$ git commit --amend [file1] [file2] ...

# Migrate all commit s to the specified branch
$ git rebase

# Compress and merge all commit and migrate to the specified branch
$ git rebase -i [startpoint] [endpoint]

# Merge from current head to 15f745b(commit id)
$ git rebase -i 15f745b

# Merge the last two submissions
$ git rebase -i HEAD~2

Key points:

The difference between git merge, git rebase and git cherry pick. In short, GIT merge is to merge the commit of the specified branch into the current branch. git rebase is to merge the commit of the current branch into the specified branch and go down the history of the specified branch. Git cherry pick is to specify a commit into the current branch (all dependent commits must be combined together)

git merge:

git rebase:

git cherry pick:

Multi branch management

## List all local branches
$ git branch

# List all remote branches
$ git branch -r

# List all local and remote branches
$ git branch -a

# Create a new branch, but still stay in the current branch
$ git branch [branch-name]

# Create a new branch and switch to it
$ git checkout -b [branch]

# Create a new branch and point to the specified commit
$ git branch [branch] [commit]

# Create a new branch and establish a tracking relationship with the specified remote branch
$ git branch --track [branch] [remote-branch]

# Switch to the specified branch and update the workspace
$ git checkout [branch-name]

# Switch to previous branch
$ git checkout -

# Establish a tracking relationship between an existing branch and a specified remote branch
$ git branch --set-upstream [branch] [remote-branch]

# Merge the specified branch to the current branch
$ git merge [branch]

# Select a commit to merge into the current branch
$ git cherry-pick [commit]

# Delete branch
$ git branch -d [branch-name]

# Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

see information

# Show changed documents
$ git status

# Displays the version history of the current branch
$ git log

# Displays the commit history and the files that change each time a commit occurs
$ git log --stat

# Search submission history according to keywords
$ git log -S [keyword]

# All changes after a commit are displayed, and each commit occupies one line
$ git log [tag] HEAD --pretty=format:%s

# Display all changes after a commit, and its "submission description" must meet the search criteria
$ git log [tag] HEAD --grep feature

# Displays the version history of a file, including file renaming
$ git log --follow [file]
$ git whatchanged [file]

# Displays each diff related to the specified file
$ git log -p [file]

# Show last 5 submissions
$ git log -5 --pretty --oneline

# Displays all submitted users, sorted by submission times
$ git shortlog -sn

# Displays who modified the specified file and when
$ git blame [file]

# Show differences between staging and workspace
$ git diff

# Displays the difference between the staging area and the previous commit
$ git diff --cached [file]

# Displays the difference between the workspace and the latest commit of the current branch
$ git diff HEAD

# Displays the difference between two submissions
$ git diff [first-branch]...[second-branch]

# Displays the recent commit s of the current branch, including those that have been deleted and overwritten
$ git reflog

label

Tags are often used to mark the release version. When we want to go back to a certain version, we can check the tag;

# List all tag s
$ git tag

# Create a new tag in the current commit
$ git tag [tag]

# Specify a new commit tag
$ git tag [tag] [commit]

# Delete local tag
$ git tag -d [tag]

# Delete remote tag
$ git push origin :refs/tags/[tagName]

# View tag information
$ git show [tag]

# Submit the specified tag
$ git push [remote] [tag]

# Submit all tag s
$ git push [remote] --tags

# Create a new branch to point to a tag
$ git checkout -b [branch] [tag]

revoke

# Restore the specified files in the staging area to the workspace
$ git checkout [file]

# Restore the specified file of a commit to the staging area and workspace
$ git checkout [commit] [file]

# Restore all files in the staging area to the workspace
$ git checkout .

# Reset the specified file in the staging area, which is consistent with the last commit, but the workspace remains unchanged
$ git reset [file]

# Reset the staging area and workspace to be consistent with the last commit
$ git reset --hard

# Reset the pointer of the current branch to the specified commit, and reset the staging area at the same time, but the workspace remains unchanged
$ git reset [commit]

# Reset the HEAD of the current branch to the specified commit, and reset the staging area and workspace at the same time, which is consistent with the specified commit
$ git reset --hard [commit]

# Reset the current HEAD to the specified commit, but leave the staging area and workspace unchanged
$ git reset --keep [commit]

# Create a new commit to revoke the specified commit
# All changes in the latter will be offset by the former and applied to the current branch
$ git revert [commit]

# Temporarily remove uncommitted changes and move in later
$ git stash
$ git stash pop

Topics: Front-end git github