Repost:Common GIT Commands


Common GIT Commands

2017-12-20 10:17 by Genius Wolong, 51982 Read, 7 Comments, Collection, edit

Learn endlessly and strive for perfection!

Ten years east and ten years west, don't deceive young people into poverty!

Education represents your past, ability represents your present, and learning represents your future!

This blog was forwarded by someone else. Original address:

I haven't written a blog for a long time. I'm busy with my work. Forward one today!The provincial blog park is too cold...

I can still use the Git graphical interface, but the commands are not so good. Let's learn how to use the Git commands together...

In general, just remember the command 6 below for your daily use.However, if you are skilled in using them, I'm afraid you need to remember 60-100 commands.

Below is a list of the common Git commands I've compiled.Several proper nouns are translated as follows.

  • Workspace: Workspace
  • Index / Stage: Staging Area
  • Repository: Warehouse area (or local warehouse)
  • Remote: Remote Warehouse

1. New Code Base

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

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

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

2. Configuration

The settings file for Git is.gitconfig, which can be either in the user's home directory (global configuration) or in the project directory (project configuration).

# Show current Git configuration
$ git config --list

# Edit Git Profile
$ git config -e [--global]

# Set user information when submitting code
$ git config [--global] "[name]"
$ git config [--global] "[email address]"

3. Add/Remove Documents

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

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

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

# Ask for confirmation before adding each change
# Multiple submissions can be made for multiple changes to the same file
$ git add -p

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

# Stops tracking the specified file, but it 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]

4. Code Submission

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

# Submit designated documents for staging area to warehouse area
$ git commit [file1] [file2] ... -m [message]

# Submit workspace changes since last commit, directly to warehouse
$ git commit -a

# Show all diff information on submission
$ git commit -v

# Use a new commit instead of the last one
# Use to override the last commit submission if no new changes are made to the code
$ git commit --amend -m [message]

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

5. Branches

# 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 stay on the current branch
$ git branch [branch-name]

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

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

# Create a new branch to 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 the 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]

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

6. Labels

# List all tag s
$ git tag

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

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

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

# Remove remote Tags
$ 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]

7. Viewing Information

# Show changedfile
$ git status

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

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

# Search submission history by keyword
$ git log -S [keyword]

# Displays all changes since a commit, with one commit per line
$ git log [tag] HEAD --pretty=format:%s

# Show all changes after a commit whose Submit Notes must match the search criteria
$ git log [tag] HEAD --grep feature

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

# Displays every diff associated with the specified file
$ git log -p [file]

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

# Show all submitted users, sorted by number of submissions
$ git shortlog -sn

# Displays who modified the specified file at what time
$ git blame [file]

# Show temporary and workspaceCodedifference
$ git diff

# Show differences between staging area and previous commit
$ git diff --cached [file]

# Show differences between workspace and current branch latest commit
$ git diff HEAD

# Show differences between submissions
$ git diff [first-branch]...[second-branch]

# Show how many lines of code you wrote today
$ git diff --shortstat "@{0 day ago}"

# Display metadata and content changes for a submission
$ git show [commit]

# Show files that have changed in a submission
$ git show --name-only [commit]

# Displays the contents of a file during a submission
$ git show [commit]:[filename]

# Show recent submissions for the current branch
$ git reflog

# Update current branch from local master pull code: branch is generally master
$ git rebase [branch]

8. Remote Synchronization

# Download all changes to the remote warehouse
$ git fetch [remote]

# Show all remote warehouses
$ git remote -v

# Display information about a remote warehouse
$ git remote show [remote]

# Add a new remote warehouse and name it
$ git remote add [shortname] [url]

# Retrieve changes from remote warehouses and merge with local branches
$ git pull [remote] [branch]

# Upload locally specified branch to remote repository
$ git push [remote] [branch]

# Force the current branch to a remote warehouse, even in conflict
$ git push [remote] --force

# Push all branches to remote warehouse
$ git push [remote] --all

9. Cancellation

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

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

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

# Resets the specified file for the staging area, consistent with the previous commit, but the workspace remains unchanged
$ git reset [file]

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

# Reset the pointer to the current branch to specify commit while resetting the staging area, but the workspace is not changed
$ git reset [commit]

# Reset the HEAD of the current branch to specify commit, while resetting the staging and workspace as specified by commit
$ git reset --hard [commit]

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

# Create a new commit to undo the specified commit
# All changes to the latter will be cancelled out and applied to the current branch
$ git revert [commit]

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

10. Other

# Generate a compressed package for publishing
$ git archive

Keywords: git

Added by root on Wed, 12 Feb 2020 18:16:12 +0200