current position:Home>Most complete git common commands

Most complete git common commands

2022-06-24 09:57:14BigChen_ up

One 、 New code base

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

#  Create a new directory , Initialize it to Git The code base 
$ git init [project-name]

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

Two 、 To configure

#  Show current Git To configure 
$ git config --list

#  edit Git The configuration file 
$ git config -e [--global]

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

3、 ... and 、 increase / Delete file

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

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

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

#  Add each change before , Will ask for confirmation 
#  For multiple changes in the same file , It can be submitted in batches 
$ git add -p

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

#  Stop tracking specified files , But the file remains in the workspace 
$ git rm --cached [file]

#  Rename file , And put it in the temporary storage area 
$ git mv [file-original] [file-renamed]

Four 、 Submit code

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

#  Submit the specified files of staging area to the warehouse area 
$ git commit [file1] [file2] ... -m [message]

#  Submit workspace since last commit The change after , Go straight to the warehouse area 
$ git commit -a

#  Show all... On submission diff Information 
$ git commit -v

#  Use a new commit, Instead of the last submission 
#  If there are no new changes in the code , To rewrite the last time commit The submission of 
$ git commit --amend -m [message]

#  Redo the last time commit, And include new changes to specified documents 
$ git commit --amend [file1] [file2] ...

5、 ... and 、 Branch


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

#  Create a new branch , And switch to the branch 
$ git checkout -b [branch]

#  Create a new branch , Point to designated commit
$ git branch [branch] [commit]

#  Create a new branch , Establish a trace 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 the existing branch and the specified remote branch 
$ git branch --set-upstream [branch] [remote-branch]

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

#  Select a commit, Merge into the current branch 
$ git cherry-pick [commit]

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

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

6、 ... and 、 label

#  List all tag
$ git tag

#  Create a new one tag At present commit
$ git tag [tag]

#  Create a new one tag In the 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 , Point to something tag
$ git checkout -b [branch] [tag]

7、 ... and 、 Check the information

#  Show changed documents 
$ git status

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

#  Show commit history , And every time commit Changed documents 
$ git log --stat

#  Search submission history , According to the key words 
$ git log -S [keyword]

#  Show a certain commit All the changes since , Every commit Occupy a line 
$ git log [tag] HEAD --pretty=format:%s

#  Show a certain commit All the changes since , Its " Submit instructions " Must meet search criteria 
$ git log [tag] HEAD --grep feature

#  Show version history of a file , Include file rename 
$ git log --follow [file]
$ git whatchanged [file]

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

#  Show past 5 Submission 
$ git log -5 --pretty --oneline

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

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

#  Show differences between staging area and workspace 
$ git diff

#  Show staging area and previous commit The difference of 
$ git diff --cached [file]

#  Show workspace and current branch up to date commit Differences between 
$ git diff HEAD

#  Show 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}"

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

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

#  Show when a submission , The contents of a document 
$ git show [commit]:[filename]

#  Shows the most recent commits for the current branch 
$ git reflog

8、 ... and 、 Remote synchronization


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

#  Show all remote warehouses 
$ git remote -v

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

#  Add a new remote warehouse , And name 
$ git remote add [shortname] [url]

#  Change to retrieve remote warehouse , And merge with local branches 
$ git pull [remote] [branch]

#  Upload local designated branch to remote warehouse 
$ git push [remote] [branch]

#  Force current branch to remote warehouse , Even if there are conflicts 
$ git push [remote] --force

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

Nine 、 revoke


#  Recover the specified files from the staging area to the workspace 
$ git checkout [file]

#  To restore something commit To the staging area and workspace 
$ git checkout [commit] [file]

#  Recover all files from staging area to workspace 
$ git checkout .

#  Reset the specified file in the staging area , With the last time commit bring into correspondence with , But the workspace doesn't change 
$ git reset [file]

#  Reset staging area and workspace , With the last time commit bring into correspondence with 
$ git reset --hard

#  Resets the pointer of the current branch to the specified commit, Also reset staging area , But the workspace doesn't change 
$ git reset [commit]

#  Resets the... Of the current branch HEAD For a given commit, Reset staging area and workspace at the same time , And specify commit Agreement 
$ git reset --hard [commit]

#  Reset current HEAD For a given commit, But keep staging area and workspace unchanged 
$ git reset --keep [commit]

#  Create a new one commit, Used to revoke a designation commit
#  All changes in the latter will be offset by the former , And apply to the current branch 
$ git revert [commit]

#  Temporarily remove uncommitted changes , Move in later 
$ git stash
$ git stash pop

Ten 、 other

#  Generate a compressed package for publishing 
$ git archive

copyright notice
author[BigChen_ up],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/175/202206240807400962.html

Random recommended