• 欢迎来到本站,本站主要做程序员相关技术、博客、笔记,如果喜欢本网站那么赶紧使用Ctrl+D 收藏吧!
  • 如果有任何疑问,可以在下方文章下方留下你的评论~

[Complete] List of commonly used git commands

赚钱副业 8个月前 (10-06) 2次浏览 0个评论

I use git every day, but I can’t remember a lot of commands.
Generally speaking, daily use as long as you remember the following six commands, it is OK. But if you use it skillfully, I’m afraid you have to remember 60-100 commands.

[Complete] List of commonly used git commands

Ⅰ. Set up Git and initialize the repository

#Create a git code base in the current directory
$ git init
#Create a new directory and initialize it as the GIT code base
$ git init [project-name]
#Download a project and its entire code history
$ git clone [url]

Ⅱ. Configure

Git’s setting file is .gitconfig, which can be in the user’s home directory (global configuration) or in the project directory (project configuration).

#Displays the current git configuration
$ git config --list
#Edit git 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]"

Ⅲ. Add / delete files

#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 of the current directory to the staging area
$ git add .
#Confirmation is required before each change is added
#For multiple changes of the same file, multiple submission can be realized
$ git add -p
#Delete the workspace file and place the deletion in the staging area
$ git rm [file1] [file2] ...
#Stops tracing the specified file, but it remains in the workspace
$ git rm --cached [file]
#Rename the file and put it in the temporary storage area
$ git mv [file-original] [file-renamed]

Ⅳ. Code commit

#Submit staging area to warehouse area
$ git commit -m [message]
#Submit the specified file of the staging area to the warehouse area
$ git commit [file1] [file2] ... -m [message]
#Commit the workspace changes since the last commit, directly to the warehouse area
$ git commit -a
#Show all diff information on submit
$ git commit -v
#Use a new commit instead of the previous commit
#If there are no new changes to the code, it is used to rewrite the commit information of the last commit
$ git commit --amend -m [message]
#Redo the last commit and include new changes to the specified file
$ git commit --amend [file1] [file2] ...

Ⅴ. Branch


#List all local branches
$ git branch
#List all remote branches
$ git branch -r
#Lists all local and remote branches
$ git branch -a
#Create a new branch, but remain in the current branch
$ git branch [branch-name]
#Create a new branch and switch to it
$ git checkout -b [branch]
#Create a new branch and point 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 previous branch
$ git checkout -
#Establish a trace 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 and merge it into the 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]

Ⅵ. Git tag


#List all tags
$ 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 tag
$ git tag -d [tag]
#Delete remote Tag
$ git push origin :refs/tags/[tagName]
#View tag information
$ git show [tag]
#Submit the specified tag
$ git push [remote] [tag]
#Submit all tags
$ git push [remote] --tags
#Create a new branch and point to a tag
$ git checkout -b [branch] [tag]

Ⅶ. Git-states of information


#Document showing changes
$ git status
#Displays the version history of the current branch
$ git log
#Shows the commit history, as well as the files that changed each commit
$ git log --stat
#Search submission history by keyword
$ git log -S [keyword]
#Show all changes after a commit, each on a line
$ git log [tag] HEAD --pretty=format:%s
#To display all changes after a commit, the submit description must match the search criteria
$ 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 diff associated with the specified file
$ git log -p [file]
#Show the last 5 submissions
$ git log -5 --pretty --oneline
#Displays all submitted users, sorted by number of submissions
$ git shortlog -sn
#Shows who modified the specified file and when
$ git blame [file]
#Displays the difference between the staging and workspace
$ git diff
#Shows the difference between the staging and the previous commit
$ git diff --cached [file]
#Displays the difference between the workspace and the latest commit of the current branch
$ git diff HEAD
#Shows the difference between two commits
$ git diff [first-branch]...[second-branch]
#Shows how many lines of code you wrote today
$ git diff --shortstat "@{0 day ago}"
#Display the metadata and content changes of a submission
$ git show [commit]
#Displays the files that have changed during 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

Ⅷ. Git remote and synchronous

#Download all changes to 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 repository and name it
$ git remote add [shortname] [url]
#Retrieve the changes from the remote warehouse and merge with the local branch
$ git pull [remote] [branch]
#Upload local designated branch to remote warehouse
$ git push [remote] [branch]
#Force 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

Ⅸ. Git revert

#Restore the specified file from the staging area to the workspace
$ git checkout [file]
#Recover the specified file of a commit to the staging area and workspace
$ git checkout [commit] [file]
#Recover all files from the staging area to the workspace
$ git checkout .
#Reset the specified file of the staging area, which is consistent with the previous commit, but the workspace remains unchanged
$ git reset [file]
#Reset the staging area and workspace, consistent with the last commit
$ git reset --hard
#Resets the pointer of the current branch to the specified commit, and resets the staging area, but the workspace remains unchanged
$ git reset [commit]
#Reset the head of the current branch to the specified commit, and reset the staging area and workspace at the same time, consistent with the specified commit
$ git reset --hard [commit]
#Reset the current head to the specified commit, but leave the staging and workspace unchanged
$ git reset --keep [commit]
#Create a new commit to cancel the specified commit
#All changes in the latter are offset by the former and applied to the current branch
$ git revert [commit]
#Remove uncommitted changes temporarily and move in later
$ git stash
$ git stash popa

Ⅹ. Other

#Generate a compressed package for publishing
$ git archive

中国笔友, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:[Complete] List of commonly used git commands
喜欢 (0)
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址