当前位置:网站首页>Git common Command Reference Manual

Git common Command Reference Manual

2020-11-10 12:44:23 0xBoo

Although I use it every day Git, But some commands haven't been used for a long time , Or forget , So the purpose of this note is to sort out those Git Common commands .

Basic configuration

Git The settings file for is .gitconfig, It can be in the user's home directory ( Global configuration ), It can also be found in the project directory ( Project configuration ).

#  View the global configuration list 
$ git config --global --list
#  View the local configuration list 
$ git config --local --list

#  Set global user name / mailbox 
$ git config --global user.name "yourName"
$ git config --global user.email "example@example.com"

#  Set the local current workspace warehouse user name / mailbox 
$ git config --local user.name "yourName"
$ git config --local user.email "example@example.com"

#  Set the default text editor to  emacs/vim
$ git config --global core.editor emacs/vim

#  Edit the configuration file of the current warehouse 
$ git config -e  #  Equivalence and  vim .git/config
#  Edit global profile 
$ git config --global -e

Command alias configuration

#  Add alias  git st = git status
$ git config --global alias.st status

#  Delete  st  Alias 
$ git config --global --unset alias.st

#  Execute external command ,  Just put... In the front  !  that will do 
$ git config --global alias.st '!echo hello';

Agent configuration

If you want to know about Git More information about configuring agents , You can refer to This note .

#  To configure HTTP/HTTPS  agent 
$ git config --global https.proxy  http://127.0.0.1:1087
$ git config --global http.proxy  http://127.0.0.1:1087

#  see 
$ git config --global --get http.proxy
$ git config --global --get https.proxy

#  Cancel the agency 
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy

Generate SSHKey

About how to configure ssh config You can refer to This note .

#  take ssh key Generated by default , That is to say `~/.ssh/id_rsa`.
$ ssh-keygen -t rsa -C "youremail"

#  take ssh key Generated in the specified file name under the specified path 
$ ssh-keygen -t rsa  -f ~/.ssh/id_rsa_bitbucket -C "youremail"

preparation

#  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] [project-name]

#  Shallow clone ,  The history only clones the last one ,  Reduce clone time 
$ git clone --depth=1 https://github.com/0xAiKang/Note.git

Basic operation

The commands in the basic operations are very frequently used .

File status

#  View workspace status 
$ git status

#  List not to be  .gitignore  List of ignored files 
$ git status --ignored

#  List not to be  .gitignore  List of ignored files 
$ git ls-files

File operations

#  Hold all 
$ git add -A

#  Temporary storage of a file 
$ git add ./README.md

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

#  Temporary storage of a series of files 
$ git add 1.txt 2.txt ...

#  Delete files from the staging area (git add  Reverse operation of )
$ git rm [file] 

#  Temporary storage area 、 Delete the workspace together 
$ git rm -f [file]

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

See file changes

#  See all file changes 
$ git diff

#  Check for changes to specific documents 
$ git diff README.md

#  View specified  commit-id  Changes 
$ git diff [commit-id]

#  What's the difference between the latest version in the workspace and the repository 
$ git diff HEAD --[file-name]

#  View the history of a file modification 
$ git log README.md
$ git show [commit-id] README.md

Undo and rollback

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

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

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

#  Roll back previous version 
$ git reset --hard HEAD^

#  Back to the specified version ( The staging and workspace will be reset )
$ git reset --hard [commit-id]

#  Back to the specified version ( The staging and workspace will not be reset , It will return to the temporary state of the version )
$ git reset --soft [commit-id]

Submit

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

#  Submit the specified file of staging area to the local warehouse 
git commit README.md -m [message]

#  Submit and display diff change 
git commit -v

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

journal

#  View the full history submission 
$ git log

#  See the former n  Bar record 
$ git log -n

#  View the complete history submission in graphical form 
$ git log --graph --pretty=oneline --abbrev-commit

#  adopt commit log  To search 
$ git log -i --grep="fire bug"

#  List the number of contributors ,  Only authors and contributions will be printed 
$ git shortlog -sn

#  Sort by the number of contributions submitted and print out the information 
$ git shortlog -n

#  Use mailbox format to view contribution 
$ git shortlog -e

Branch

#  View local branch 
git branch

#  View all branches 
git branch -a

#  View the remote branches associated with the local branch 
git branch -vv

#  View local  master  Branch creation time 
git reflog show --date=iso master

#  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-id]

#  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]

#  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-id]

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

#  Force delete specified branch 
$ git branch -D [branch-name]

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

Remote warehouse management

#  View remote repository ( The default is origin, This is a git  The default name that will be used )
$ git remote 

#  Appoint -v,  View all remote warehouse addresses 
$ git remote -v

#  Add a new remote warehouse 
$ git remote add [origin-name] https://github.com/0xAiKang/Note.git

#  View the details of the specified remote warehouse 
$ git remote show [origin-name]

#  Rename the remote repository 
$ git remote rename [old-name] [new-name]

#  Remove remote warehouse 
$ git remote remove [origin-name]

Push

#  Default push current branch 
$ git push

#  Push content to the main branch , And establish a tracking relationship 
$ git push -u origin master

#  Push the local branch to the specified remote branch , ( Local branch : Remote branch )
$ git push origin [branch]:[branch]

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

Pull

#  Retrieve changes to the default remote repository , And automatically merge with local branches 
$ git pull

#  Retrieve changes to the specified remote warehouse , And automatically merge with local specified branches ( Remote branch name : Local branch name )
$ git pull [remote] [branch]:[branch]

#  Retrieve changes to the specified remote warehouse , And automatically merge with the local current branch 
$ git pull origin master

#  Retrieve all changes to the remote warehouse , But it doesn't automatically merge with the local current branch 
$ git fetch

Advanced operation

The commands in advanced operations are very useful , But it may not be used very often , So take them out on their own .

cherry-pick

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

#  Keep the original author information for submission 
$ git cherry-pick -x [commit-id]

Stash

#  Hide the current workspace 
$ git stash

#  Restore hidden workspace , And remove the hidden record from the hidden list 
$ git stash pop

#  Restore hidden workspace , Keep this hidden record 
$ git stash apply

#  View the current hidden list 
$ git stash list

Blame

git blame Used to view the modification history of a file by which author made the change .

#  see  README.md  The modification history of the file , Including time 、 Author and content 
$ git blame README.md

#  See who changed  README.md  Of documents  11 That's ok -12 That's ok 
$ git blame -L 11,12 README.md

#  See who changed  README.md  file 11 After that 
$ git blame -L 11 README.md

label

#  List all local labels 
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]

#  List all remote tags 
$ git ls-remote --tags origin

#  Create tags with notes 
$ git tag -a v1.1.0 -m " Label description "

#  View local 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]

Git ProTips

Git ProTips It's sort of sorted out Git It's very skillful .

#  By using aliases , Optimize  git log  Output , Here are several other models ,  You can choose the one you like for alias configuration 
$ git config --global alias.lg "log --graph --pretty=format:'%Cred%h - %Cgreen[%an]%Creset -%C(yellow)%d%Creset %s %C(yellow)<%cr>%Creset' --abbrev-commit --date=relative"

$ git config --global alias.his "log --graph --decorate --oneline --pretty=format:'%Creset %s %C(magenta)in %Cred%h %C(magenta)commited by %Cgreen%cn %C(magenta)on %C(yellow) %cd %C(magenta)from %Creset %C(yellow)%d' --abbrev-commit --date=format:'%Y-%m-%d %H:%M:%S'"

$ git config --global alias.hist "log --graph --decorate --oneline --pretty=format:'%Cred%h - %C(bold white) %s %Creset %C(yellow)%d  %C(cyan) <%cd> %Creset %Cgreen(%cn)' --abbrev-commit --date=format:'%Y-%m-%d %H:%M:%S'"

$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

$ git config --global alias.lg "log --pretty=format:'%h - %an, %ar : %s' "

Reference link

版权声明
本文为[0xBoo]所创,转载请带上原文链接,感谢