Git common commands



Git minimum configuration

All Git warehouses under an account are valid
git config --global user.name 'Your name'
git config --global user.email 'Yours Email'
Only valid for current Git warehouse
git config --local user.name 'Your name'
git config --local user.email 'Yours Email'

View Git configuration

View configuration items of global type
git config --global --list

View configuration items that only apply to the current warehouse

git config --local --list

Clear Git configuration

Clear configuration items of global type
git config --unset --global A configuration item
Clear configuration items of a warehouse
git config --unset --local A configuration item

Local basic operation

View changes
git status
View which branch you are currently working on
git branch -v
Switch to the specified branch
git checkout Specify branch name
Add all changes in the current directory and its subdirectories to the temporary storage area
git add .
Add all changes in the warehouse to the staging area
git add -A
Adds the specified file to the staging area
git add File 1 file 2 file 3
Create a formal commit
git commit
Compare the differences between a file workspace and a staging area
git diff A file
Compare the difference between a file staging area and HEAD
git diff --cached A file
Compare the difference between a file workspace and HEAD
git diff HEAD A file
Compare all differences between workspace and staging area
git diff
Compare all differences between staging and HEAD
git diff --cached
Restore the specified files in the workspace to be the same as the staging area
git checkout File 1 file 2 file 3
Restore the specified file in the temporary storage area to the same as HEAD
git reset File 1 file 2 file 3
Restore all files in the staging area and workspace to be the same as HEAD
git reset --hard
Compare the difference between any two commit with difftool
git difftool Submit A Submit B
See which files are not controlled by Git
git ls-files --others

Processing of temporary tasks in Gaza

Save unprocessed changes to stash first
git stash
Continue the previous unfinished work after the temporary task is processed
git stash pop
 perhaps
git stash apply
pop Not reserved stash, apply retain stash
View all stash
git stash list
Retrieve a stash change
git stash pop stash@{number n}

Modify personal branch history

Modify the last commit
1) Modify files in the workspace
2) git add .
3) git commit --amend
Modify the middle commit (code X)
1) git rebase -i X Front one commit of id
2) Modify files in the workspace
3) git add .
4) git rebase --continue
 Subsequent conflicts may need to be handled until rebase end

View history of changes

Each commit of the current branch is displayed in one line
git log --oneline
Display the nearest n commit s
git log --n
Graphically display the history of all branches
git log --oneline --graph --all
View all commit s involving a file change
git log A file
Last modify the corresponding commit and author in each line of a file
git blame A file

Branches and labels

Create a new branch based on the current branch
git branch New branch
Creates a new branch based on the specified branch
git branch New branch existing branch
Create a branch based on a commit
git branch A new branch commit of id
Create a branch and switch to it
git checkout -b New branch
List local branches
git branch -v
List local and remote branches
git branch -av
List all remote branches
git branch -rv
? Lists remote branches whose names conform to a style
git branch -rv -l 'A style'
Safely delete a local branch
git branch -d Branch to be deleted
Forcibly delete a local branch
git branch -D Branch to be deleted
Delete all local branches that have been merged into the master branch
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d 
Delete all local branches where the remote origin no longer exists
git remote prune origin
Label commit
git tag Tag name commit of id

Integration between two branches

Merge branch A into the current branch and create A commit for the merge
git merge A branch
Merge branch A into branch B, and create A commit for the merge
git merge A branch B branch
rebase the current branch based on the B branch to merge the B branch into the current branch
git rebase B branch
rebase branch A based on branch B so that branch B can be combined into branch A
git rebase B branch A branch
Using mergetool to resolve conflicts
git mergetool

Interaction with remote

List all remote
git remote -v
Add remote
git remote add url address
Delete remote
git remote remove remote Name of
Change the name of remote
git remote rename Old name new name
Pull all remote branch and label changes to the local
git fetch remote
Pull the changes of the remote branch to the local branch, and merge to the local branch
git pull remote Name branch name
push local branch to remote
git push remote Name branch name
Delete remote branch
git push remote --delete Remote branch name
 perhaps
git push remote :Remote branch name
Submit the specified label to the remote
git push remote Tag name
Submit all tags to remote
git push remote --tags

reference resources

https://git-scm.com/book/zh/v2

Keywords: IDE

Added by stylezeca on Fri, 26 Nov 2021 04:16:41 +0200