本文发布于Cylon的收藏册,转载请著名原文链接~


Tag

command describe
git tag 列出所有tag
git tag -l v1.* 列出符合条件的tag(筛选作用)
git tag [tag_name] 创建轻量tag(无-m标注信息)
git push REMOTE TAG 推送一个tag到远端
git push origin –tags* 推送所有本地tag到远程
git push origin :refs/tags/[REMOTE TAG]
git push –delete REMOTE TAG
删除远程指定tag
git fetch origin [remote_tag_name] 拉取远程指定tag
git show [tag_name] 显示指定tag详细信息
git push origin [local_tag_name] 推送指定本地tag到远程
git tag NEW_TAG OLD_TAG
git tag -d OLD_TAG
重命名本地tag
git tag -d [local_tag_name] 删除本地指定tag
git ls-remote –tags origin 查询远程tags
git tag -a [tag_name] 创建含注解的tag
git fetch origin [remote_tag_name]
git checkout [remote_tag_name]
git branch
checkout远端tag到本地

Checking

检查工作目录与暂存区的状态

command describe
git status 查看在你上次提交之后是否有对文件进行再次修改
git status -s 获得简短的输出结果
git diff 用于比较文件间差异
git diff –cached
git diff –staged
显示暂存区(已add但未commit文件)和最后一次commit(HEAD)之间的所有不相同文件的增删改
git diff HEAD 工作目录(已track但未add文件)和暂存区(已add但未commit文件)与最后一次commit之间的的所有不相同文件的增删改
git diff <branch1> <branch2>                           比较两个分支上最后 commit 的内容的差别

Log

command describe
git diff branch1 branch2 –stat  显示出所有有差异的文件(无内容)
git log <b1>..<b2> 查看 b1 中的 log 比 b2 中的 log 多提交了哪些内容
git log –oneline 以 <commit_id> <comment> 为一行来显示
git log -S <‘LoginViewController’> log 的输出将添加火删除对应字符串
git log –all –grep=<‘0117’> log 的输出将过滤出与对应字符串相关的commit

Checkout

checkout 是指不同tag或分支间的切换行为

command describe
git checkout TAG 切换至一个tag
git checkout -b BRANCH TAG 创建一个新分支,并切换至这个tag
git checkout BRANCH 切换至一个分支
git checkout -m BRANCH 在切换分支时如有冲突则合并
git checkout COMMIT_HASH 切换至一个commit
git checkout -b BRANCH HEAD~4
git checkout -b BRANCH COMMIT_HASH
切换并创建为新分支
git checkout COMMIT – FILE_PATH           将 FILE_PATH 指定的文件恢复为当前分支的最新版本(仅未add)

Remote

remote 子命令用于管理repositories

command describe
git remote List all remote
git remote rename OLD_REMOTE NEW_REMOTE Rename remote
git remote prune REMOTE Remove stale remote tracking branches

Branch

branch 用于管理分支

command describe
git branch List all branches
git checkout -b BRANCH Create the branch on your local machine and switch in this branch
git branch BRANCH COMMIT_HASH     Create branch from commit
git push REMOTE BRANCH Push the branch to remote
git branch -m OLD_BRANCH NEW_BRANCH       Rename other branch
git branch -m NEW_BRANCH Rename current branch
# Rename branch locally
git branch -m OLD_BRANCH NEW_BRANCH

# Delete the old branch
git push origin :OLD_BRANCH

# Push the new branch, set local branch to track the new remote
git push –set-upstream REMOTE NEW_BRANCH
Rename remote branch
git branch -D BRANCH
git push REMOTE :BRANCH
Delete a branch locally and remote
git branch | grep -v “master” | xargs git branch -D Delete all local branches but master

Commit

Record changes to the repository

command describe
git reset –hard HEAD~1 Undo last commit
git rebase -i HEAD~5
git reset –soft HEAD~5
git add .
git commit -m “Update”
git push -f origin master
Squash last n commits into one commit
git branch newbranch

# Go back 3 commits. You will lose uncommitted work.1
git reset –hard HEAD~3

git checkout newbranch
Move last commits into new branch:
git rebase -i HEAD^^^
git add .
git rebase –continue
Make changes to older commit

Merge

Join two or more development histories together

command describe
git checkout BRANCH
git merge –no-ff BASE_BRANCH
Merge commits from master into feature branch
git merge BRANCH Current branch merge to BRANCH branch
git merge -m “Merge from Dev” When merge branch add comment
git merge -s ours obsolete Merge obsolete branch to current branch using ours policy
git merge –no-commit maint Merge maint branch to current, than do not make new comment

Cherry Pick

Apply the changes introduced by some existing commits

git cherry-pick COMMIT_HASH_A COMMIT_HASH_B

Revert

command describe
git revert HEAD Revert the previous commit
git revert –no-commit HEAD~3.. Revert the changes from previous 3 commits without making commit

Amend

command describe
git commit –amend Amend previous commit
git commit –amend –no-edit
git commit –amend -m “COMMIT_MESSAGE” Changing git commit message
git commit –amend -m “COMMIT_MESSAGE”
git push –force REMOTE BRANCH
Changing git commit message after push

Reflog

reference log

command describe
git reflog Show reflog
Get commit

Rebase

Rebase the current branch onto another branch

command describe
git rebase BASE_BRANCH Rebase the current branch onto another branch
git rebase –continue Continue rebase
git rebase –abort Abort rebase

Tracking

command describe
git clean Remove untracked files
git reset FILE_PATH Remove file from index
git reset Reset the index to match the most recent commit
git reset –hard Reset the index and the working directory to match the most recent commit
git rm file Remove files from the working tree and from the index
git ls-files Show information about files in the index and the working tree
git ls-files -d Show deleted files in the output.
git ls-files -m Show modified files in the output.
git ls-files -i Show only ignored files in the output.
git ls-files –no-empty-directory Do not list empty directories. Has no effect without –directory.

Config

command describe
git config -l list all git config
git config –global [key] “[value]” set global configuation
git config [key] “[value]” set current repositories configuation

Reference

awesome-git-commands

本文发布于Cylon的收藏册,转载请著名原文链接~

链接:https://www.oomkill.com/2020/11/awesome-git-command/

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」 许可协议进行许可。