Git practical skills and command summary

1. New construction

Create a new git version library. The configuration, storage and other information of this version library will be saved to Git folder

 

# Initialize the current project $ git init
# Create a new directory and initialize it as Git Code base $ git init [project-name]
# Creates an empty directory in the specified directory Git Warehouse. Running this command creates a directory,Contains only .git Empty directory for subdirectory.
$ git init --bare <directory>
# Download a project and its entire code history# This command is to copy a version library to another directory, and also copy all branches to the new version library. In this way, you can submit to the remote branch $git clone [url] in the new version library


2. Disposition

Change settings. It can be the setting of version library, system or global
# Show current Git to configure $ git config --list
# edit Git configuration file $ git config -e [--global]
# Output and set basic global variables $ git config --global user.email$ git config --global user.name
$ git config --global user.email "MyEmail@gmail.com"$ git config --global user.name "My Name"
# Define the author mailbox used by all submissions of the current user. $ git config --global alias.<alias-name> <git-command>
# Create a shortcut (alias) for the Git command$ Git config --system core. editor <editor>

3. Help

git has built-in very detailed explanation of the command, which can be consulted quickly
# Find available commands $ git help
# Find all available commands $ git help -a
# Find specific commands in the document# Git help < command > $git help add $git help commit $git help init

4. Status

Displays the difference between the index file (that is, the current workspace) and the submission pointed to by the current header pointer
# Show branches, untracked files, changes and other differences $ git status
# View other usage of git status $git help status

5. Information

Get git information such as some files, some branches, a submission, etc
# display commit History, and every time commit Changed documents $ git log --stat
# Search submission history according to keywords $ git log -S [keyword]
# Show a commit All subsequent changes, each commit Occupy a line $ git log [tag] HEAD --pretty=format:%s
# Show a commit All subsequent changes"Submission instructions"The search criteria must be met $ 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 time the specified file is related diff$ 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
# Show staging area and previous commit Differences $ git diff --cached [file]
# Displays the latest connection between the workspace and the current branch commit Differences between $ git diff HEAD
# Displays the difference between two submissions $ git diff [first-branch]...[second-branch]
# Show how many lines of code you wrote today $ git diff --shortstat "@{0 day ago}"
# Compare staging area and version library differences $ git diff --staged
# Compare staging area and version library differences $ git diff --cached
# Compare statistics only $ git diff --stat
# Displays the metadata and content changes of a submission $ git show [commit]
# Displays the files that have changed in a submission $ git show --name-only [commit]
# Displays the contents of a file at the time of a submission $ git show [commit]:[filename]
# Displays the most recent commits of the current branch $ git reflog
# View remote branches $ git br -r
# Create a new branch $ git br <new_branch>
# View the last submission information of each branch $ git br -v
# View branches that have been merged into the current branch $ git br --merged
# View branches that have not been merged into the current branch $git br -- no merged

6. Add

Add files to the current workspace. If you do not use git add to add files, they will not be added to subsequent submissions

# Add a file $ git add test.js
# Add files in a subdirectory $ git add /path/to/file/test.js
# regular expression $ git add ./*.js
# 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 .
# Confirmation is required before adding each change# For multiple changes in the same file, you can submit $git add -p in batches

7. Delete

rm, in contrast to the add command above, removes a file from the workspace

# remove HelloWorld.js$ git rm HelloWorld.js
# Remove files from subdirectories $ git rm /pather/to/the/file/HelloWorld.js
# Delete the workspace file and put the deletion into the staging area $ git rm [file1] [file2] ...
# Stop tracking the specified file, but the file will remain in the workspace $git rm --cached [file]

8. Branching

To manage branches, you can add, delete, change, query and switch branches through the following commands
# View all branches and remote branches $ git branch -a
# Create a new branch $ git branch [branch-name]
# Rename branch# git branch -m <Old name> <New name>$ git branch -m [branch-name] [new-branch-name]
# Introduction to editing branches $ git branch [branch-name] --edit-description
# List all local branches $ git branch
# List all remote branches $ git branch -r
# 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 pointing 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 one commit,Merge into 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]
# Switch to a branch $ git co <branch>
# Create a new branch and switch to the past $ git co -b <new_branch>
# be based on branch Create a new new_branch$ git co -b <new_branch> <branch>
# Submit a history record checkout If there is no branch information, it will be automatically deleted when switching to other branches $ git co $id
# Submit a history record checkout Come out and create a branch $ git co $id -b <new_branch>
# Delete a branch $ git br -d <branch>
# Forcibly delete a branch (it is necessary to forcibly delete branches that are not merged) $git Br - d < Branch >

9. Check out

Updates the current workspace to the workspace identified by the index or a specific workspace
# Check out a version library, which will be updated to by default master branch $ git checkout# Check out a specific branch $ git checkout branchName# Create a new branch and switch to the past, which is equivalent to "git branch < name >; git checkout < name >" $git checkout - B newbranch

10. Remote synchronization

Remote branch of remote synchronization

# Download all changes of remote warehouse $ git fetch [remote]
# Show all remote warehouses $ git remote -v
# Displays information about a remote warehouse $ git remote show [remote]
# Add a new remote warehouse and name it $ git remote add [shortname] [url]
# View remote server address and warehouse name $ git remote -v
# Add remote warehouse address $ git remote add origin git@ github:xxx/xxx.git
# Set remote warehouse address(Used to modify the remote warehouse address)$ git remote set-url origin git@ github.com:xxx/xxx.git
# Delete remote warehouse $ git remote rm <repository>
# Upload specified local branch to remote warehouse# Update local branch to remote origin of master On branch# git push <Far end> <branch># git push amount to git push origin master$ git push [remote] [branch]
# Forcibly push the current branch to the remote warehouse, even if there is a conflict $ git push [remote] --force
# Push all branches to remote warehouse $git push [remote] - all

11. Rescind

# Restore the specified files in the staging area to the workspace $ git checkout [file]
# Restore a commit To the staging area and workspace $ git checkout [commit] [file]
# Restore all files in the staging area to the workspace $ git checkout .
# Resets the specified file in the staging area, which is the same as the last time commit Consistent, but workspace unchanged $ git reset [file]
# Reset staging area and workspace, same as last time commit bring into correspondence with $ git reset --hard
# Resets the pointer of the current branch to the specified value commit,The staging area is reset at the same time, but the workspace remains unchanged $ git reset [commit]
# Reset current branch HEAD Specify for commit,Reset the staging area and workspace at the same time, as specified commit agreement $ git reset --hard [commit]
# Reset current HEAD Specify for commit,But leave the staging area and workspace unchanged $ git reset --keep [commit]
# Create a new one commit,Used to undo the assignment commit# All changes in the latter will be offset by the former and applied to the current branch $ git revert [commit]
# Restore the last submitted state $ git revert HEAD
# Temporarily remove uncommitted changes and move in later $ git stash$ git stash pop
# Column all stash$ git stash list
# Recover staged content $ git stash apply
# Delete staging $git stash drop

12,commit

Save the changes submitted by the current user as a new index
# Submit temporary storage area to warehouse area with submission information $ git commit -m [message]
# Submit the specified files in the temporary storage area to the warehouse area $ git commit [file1] [file2] ... -m [message]
# Submit workspace since last commit After the change, go directly to the warehouse area $ git commit -a
# Show all on submission diff information $ git commit -v
# Use a new commit,Replace last submission# If there are no new changes in the code, it is used to rewrite the last time commit Submitted information $ git commit --amend -m [message]
# Redo the last commit and include the new changes of the specified file $git commit --amend [file1] [file2]

13,diff

Displays the difference between the current workspace and the submitted
# Show differences between working directory and index $ git diff
# Displays the difference between the index and the last submitted $ git diff --cached
# Display the working directory and the last submitted different $git diff HEAD

14,grep

You can quickly find optional configurations in the version Library:

# thank Travis Jeffery The following usage is provided:# Show line numbers in search results $ git config --global grep.lineNumber true
# Yes, the search results are more readable
$ git config --global alias.g "grep --break --heading --line-number"# In all java Find in variableName$
git grep 'variableName' -- '*.java'
# Search all lines containing "arrayListName" and "add" or "remove" $git grep - E 'arrayListName' -- and \ (- e add - e remove \)

15,log

Show all submissions for this repository
# Show all submissions $ git log
# Show some submitted information $ git log -n 10
# Show merge submissions only $ git log --merges
# View the record of each submission of this file $ git log <file>
# View the details of each modification diff$ git log -p <file>
# View the details of the last two modifications diff$ git log -p -2
#Git $stat submit statistics --

16,merge

Merging is to merge external submissions into their own branches
# Merge other branches into the current branch $ git merge branchName
# Create a new merged submission when merging# Do not merge fast forward, which can generate merge submission $git merge -- no FF branchname

17,mv

Rename or move a file
# rename $ git mv test.js test2.js
# move $ git mv test.js ./new/path/test.js
# Rename the file and put the rename in the staging area $ git mv [file-original] [file-renamed]
# Force rename or move# This file already exists and will overwrite $git mv -f myFile existingFile

18,tag


# List all tag$ git tag
# Create a new one tag At present commit$ git tag [tag]
# Create a new one tag In specified commit$ git tag [tag] [commit]
# Delete local tag$ git tag -d [tag]
# Delete remote tag$ git push origin :refs/tags/[tagName]
# see tag information $ git show [tag]
# Submit designation tag$ git push [remote] [tag]
# Submit all tag$ git push [remote] --tags
# Create a new branch and point to a tag $git checkout - B [branch] [tag]

19,pull

Merge from remote repository to current branch
# From the far end origin of master Branch update version Library# git pull <Far end> <branch>$ git pull origin master
# Grab all branch updates from the remote warehouse and merge them locally. Do not fast forward merge $git pull -- no FF

20,ci

$ git ci <file>$ git ci .# take git add, git rm and git ci And other operations are combined together $ git ci -a$ git ci -am "some comments"# Modify the last submission record $git ci --amend

21. rebase (use with caution)

Apply all commit history on one branch to another
_ Do not use rebase_. On a remote branch that has been exposed
# take experimentBranch apply to master above# git rebase <basebranch> <topicbranch>$ git rebase master experimentBranch

22. reset (use with caution)

Resets the current header pointer to a specific state. This allows you to undo merge, pull, commit, add, and so on
This is a very powerful command, but you must be aware of its consequences when using it.

# send staging The region is restored to the state it was in when it was last submitted without changing the current working directory $ git reset
# send staging Restore the area to the state of the last submission and overwrite the current working directory $ git reset --hard
# Restore the current branch to a submission without changing the current working directory# All changes still exist in the working directory $ git reset dha78as
# Restore the current branch to a submission and overwrite the current working directory# And delete all uncommitted changes and all submissions after the specified submission $git reset --hard dha78as

23. Others

# Generate a compressed package for publishing $ git archive
# patch up $ git apply ../sync.patch
# Test whether the patch is successful $ git apply --check ../sync.patch
# View git version $git -- version




Tags: git

Posted by garethhall on Tue, 17 May 2022 17:04:52 +0300