侧边栏壁纸
博主头像
一叶舟的秘密花园 博主等级

行动起来,活在当下

  • 累计撰写 37 篇文章
  • 累计创建 15 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Git进阶命令

Li
Li
2025-04-18 / 0 评论 / 0 点赞 / 172 阅读 / 0 字
温馨提示:
本文最后更新于2025-04-18,若内容或图片失效,请留言反馈。 八月长江万里晴,千帆一道带风轻

展示帮助信息

git help -g

回到远程仓库的状态

抛弃本地所有的修改,回到远程仓库的状态。

git fetch --all && git reset --hard origin/master

重设第一个 commit

也就是把所有的改动都重新放回工作区,并清空所有的 commit,这样就可以重新提交第一个 commit 了

git update-ref -d HEAD

展示工作区和暂存区的不同

输出工作区暂存区的 differences(不同)。

git diff

展示暂存区和最近版本的不同

输出暂存区和本地最近的版本(commit)的differences(不同)。

git diff --staged

展示暂存区、工作区和最近版本的不同

输出工作区暂存区 和本地最近的版本(commit)的differences(不同)。

git diff HEAD

快速切换分支

git checkout -

展示本地分支关联远程仓库的情况

git branch -vv

关联远程分支

关联之后,git branch -vv就可以展示关联的远程分支名了,同时推送到远程仓库直接:git push,不需要指定远程仓库了。

git branch -u origin/mybranch

或者在push时加上-u参数

git push origin/mybranch -u

创建并切换到远程分支

git checkout -b <branch-name> origin/<branch-name>

删除远程分支

git push origin --delete <remote-branchname>

或者

git push origin :<remote-branchname>

重命名本地分支

git branch -m <new-branch-name>

展示当前分支的最近的 tag

git describe --tags --abbrev=0

本地创建标签

git tag <version-number>

默认 tag 是打在最近的一次 commit 上,如果需要指定 commit 打 tag:

$ git tag -a <version-number> -m "v1.0 发布(描述)" <commit-id>

推送标签到远程仓库

首先要保证本地创建好了标签才可以推送标签到远程仓库:

git push origin <local-version-number>

一次性推送所有标签,同步到远程仓库:

git push origin --tags

删除本地标签

git tag -d <tag-name>

删除远程标签

删除远程标签需要先删除本地标签,再执行下面的命令

git push origin :refs/tags/<tag-name>

或(git >= v1.7.0)

git push origin --delete tag <tagname>

重命名一个远程标签

假设要把旧标签 old 重命名为 new

git tag new old
git tag -d old
git push origin --delete old
git push --tag

放弃工作区的修改

git checkout <file-name>

放弃所有修改:

git checkout .

回到某一个 commit 的状态,并重新增添一个 commit

git revert <commit-id>

回到某个 commit 的状态,并删除后面的 commit

和 revert 的区别:reset 命令会抹去某个commit id 之后的所有 commit

git reset <commit-id>

修改上一个 commit

git add <fileName>
git commit --amend

显示本地执行过 git 命令

就像 shell 的 history 一样

git reflog

修改作者名

git commit --amend --author='Author Name <email@address.com>'

修改远程仓库的 url

git remote set-url origin <URL>

查看两个星期内的改动

git whatchanged --since='2 weeks ago'

把 A 分支的某一个 commit,放到 B 分支上

这个过程需要cherry-pick命令,参考

git checkout <B> && git cherry-pick <commit-id>

给 git 命令起别名

简化命令

git config --global alias.<handle> <command>

比如:git status 改成 git st,这样可以简化命令

git config --global alias.st status

存储当前的修改,但不用提交 commit

详解可以参考廖雪峰老师的git教程

git stash

保存当前状态,包括 untracked 的文件

untracked 文件:新建的文件

git stash -u

展示所有 stashes

git stash list

回到某个 stash 的状态

git stash apply <stash@{n}>

回到最后一个 stash 的状态,并删除这个 stash

git stash pop

删除所有的 stash

git stash clear

从 stash 中拿出某个文件的修改

git checkout <stash@{n}> -- <file-path>

展示所有 tracked 的文件

git ls-files -t

展示所有 untracked 的文件

git ls-files --others

展示所有忽略的文件

git ls-files --others -i --exclude-standard

强制删除 untracked 的文件

可以用来删除新建的文件。如果不指定文件文件名,则清空所有工作的 untracked 文件。clean命令,注意两点

  1. clean 后,删除的文件无法找回

  2. 不会影响 tracked 的文件的改动,只会删除 untracked 的文件

git clean <file-name> -f

强制删除 untracked 的目录

可以用来删除新建的目录,注意:这个命令也可以用来删除 untracked 的文件。详情见上一条

git clean <directory-name> -df

展示简化的 commit 历史

git log --pretty=oneline --graph --decorate --all

git log --online --graph

把某一个分支导出成一个文件

git bundle create <file> <branch-name>

从包中导入分支

新建一个分支,分支内容就是上面git bundle create命令导出的内容

git clone repo.bundle <repo-dir> -b <branch-name>

执行 rebase 之前自动 stash

git rebase --autostash

从远程仓库根据 ID,拉下某一状态,到本地分支

git fetch origin pull/<id>/head:<branch-name>

详细展示一行中的修改

git diff --word-diff

清除 gitignore 文件中记录的文件

git clean -X -f

展示所有 alias 和 configs

注意: config 分为:当前目录(local)和全局(golbal)的 config,默认为当前目录的 config

git config --local --list (当前目录)
git config --global --list (全局)

展示忽略的文件

git status --ignored

commit 历史中显示 Branch1 有的,但是 Branch2 没有 commit

git log Branch1 ^Branch2

在 commit log 中显示 GPG 签名

git log --show-signature

删除全局设置

git config --global --unset <entry-name>

新建并切换到新分支上,同时这个分支没有任何 commit

相当于保存修改,但是重写 commit 历史

git checkout --orphan <branch-name>

之后如果需要清空工作区所有文件(比如新建了一个空的文档分支 gh-pages):

git rm -rf .

展示任意分支某一文件的内容

git show <branch-name>:<file-name>

clone 下来指定的单一分支

git clone -b <branch-name> --single-branch https://github.com/user/repo.git

忽略某个文件的改动

关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动

git update-index --assume-unchanged path/to/file

恢复 track 指定文件的改动

git update-index --no-assume-unchanged path/to/file

忽略文件的权限变化

不再将文件的权限变化视作改动

git config core.fileMode false

展示本地所有的分支的 commit

最新的放在最上面

git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/

在 commit log 中查找相关内容

通过 grep 查找,given-text:所需要查找的字段

git log --all --grep='<given-text>'

把暂存区的指定 file 放到工作区中

git reset <file-name>

强制推送

git push -f <remote-name> <branch-name>

将本地 test 分支推送为远程 master 分支

git push origin test:master

视具体情况可以加 -f 参数强制推送

同时推送到多个远端

在已有远端地址 origin 的基础上:

git remote set-url --add origin git@XXXXX.git

之后 git push 就会同时推送到多个远端

把依然在本地显示的已被删除的远程分支也从本地删除

git remote prune origin

彻底在所有历史 commits 中删除某个/批文件

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch docs/*.exs' --prune-empty --tag-name-filter cat -- --all

会删除所有匹配 docs/*.exs 的文件。随后可以 git push --force --all

原地址来源:

https://gist.github.com/Maples7/af8ce5371fe883a0334374ba79a9f4f0

0

评论区