git push origin :branchName # 删除远程分支
git push origin --delete new # 删除远程分支new
git branch -d branchName # 删除本地分支,强制删除用-D
git branch -d test # 删除本地test分支
git branch -D test # 强制删除本地test分支
git remote prune origin # 远程删除了,本地还能看到远程存在,这条命令删除远程不存在的分支
将本地分支提交到远程主机中:
git push -u origin branchName # 提交分支到远程origin主机中
当远程分支已被删除,但本地仍存在同名分支时,使用以下命令自动清理:
git fetch -p #拉取远程分支时,自动清理 远程分支已删除,本地还存在的对应同名分支。
将分支合并到当前分支:
git merge branchName # 合并分支 - 将分支branchName和当前所在分支合并
git merge origin/master # 在本地分支上合并远程分支。
git rebase origin/master # 在本地分支上合并远程分支。
git merge test # 将test分支合并到当前分支
重命名分支:
git branch -m old new #重命名分支
git branch # 列出本地分支
git branch -r # 列出远端分支
git branch -a # 列出所有分支
git branch -v # 查看各个分支最后一个提交对象的信息
git branch --merged # 查看已经合并到当前分支的分支
git branch --no-merged # 查看为合并到当前分支的分支
git remote show origin # 可以查看remote地址,远程分支
git branch test # 新建test分支
git branch newBranch 3defc69 # 指定哈希3defc69,新建分支名字为newBranch
git checkout -b newBranch origin/master # 取回远程主机的更新以后,在它的基础上创建一个新的分支
git checkout -b newBranch 3defc69 # 以哈希值3defc69,新建newBranch分支,并切换到该分支
建立本地分支与远程分支之间的链接:
git branch --set-upstream dev origin/dev # 将本地dev分支与远程dev分支之间建立链接
git branch --set-upstream master origin/next # 手动建立追踪关系
切换分支:
git checkout test # 切换到test分支
git checkout -b test # 新建+切换到test分支
git checkout -b test dev # 基于dev新建test分支,并切换
git fetch <远程主机名> <分支名> # fetch取回所有分支(branch)的更新
git fetch origin remotebranch[:localbranch] # 从远端拉去分支[到本地指定分支]
git merge origin/branch # 合并远端上指定分支
git pull origin remotebranch:localbranch # 拉去远端分支到本地分支
git push origin branch # 将当前分支,推送到远端上指定分支
git push origin localbranch:remotebranch # 推送本地指定分支,到远端上指定分支
git push origin :remotebranch # 删除远端指定分支
git checkout -b [--track] test origin/dev # 基于远端dev分支,新建本地test分支[同时设置跟踪]
git clone https://github.com/jaywcjlove/handbook.git --depth=1 --recurse-submodules
git submodule add --force '仓库地址' '路径'
# 其中,仓库地址是指子模块仓库地址,路径指将子模块放置在当前工程下的路径。
# 注意:路径不能以 / 结尾(会造成修改不生效)、不能是现有工程已有的目录(不能順利 Clone)
git submodule init # 初始化submodule
git submodule update # 更新submodule(必须在根目录执行命令)
git submodule update --init --recursive # 下载的工程带有submodule
git submodule foreach git pull # submodule 里有其他的 submodule 一次更新
git submodule foreach git pull origin master # submodule更新
git submodule foreach --recursive git submodule init
git submodule foreach --recursive git submodule update
git rm -rf node_modules/
Git是一个分布式代码管理工具,因此可以支持多个仓库。在Git中,服务器上的仓库在本地被称为远程(Remote)。个人开发时,可能用到多个远程仓库。
git remote add origin1 git@github.com:yanhaijing/data.js.git
git remote # 显示全部源
git remote -v # 显示全部源+详细信息
git remote rename origin1 origin2 # 重命名
git remote rm origin # 删除
git remote show origin # 查看指定源的全部信息
在开发到一定阶段时,给代码打标签是非常有用的。
git tag -a v0.1 -m 'my version 1.4' # 新建带注释标签
git push origin --tags # 一次性推送所有分支
git push origin v1.5 # 推送单个tag到orgin源上
git tag -v v1.4.2.1 # 验证标签,验证已经签署的标签
git tag # 列出现有标签
git tag v0gi.1 # 新建标签
git checkout tagname # 切换到标签
git tag -d v0.1 # 删除标签
git push origin :refs/tags/v0.1 # 删除远程标签
git pull --all # 获取远程所有内容包括tag
git --git-dir='<绝对地址>/.git' describe --tags HEAD # 查看本地版本信息
git config format.pretty oneline #显示历史记录时,每个提交的信息只显示一行
git config color.ui true #彩色的 git 输出
git log #查看最近的提交日志
git log --pretty=oneline #单行显示提交日志
git log --graph --pretty=oneline --abbrev-commit
git log -num #显示第几条log(倒数)
git reflog #查看所有分支的所有操作记录
git log --since=1.day #一天内的提交;你可以给出各种时间格式,比如说具体的某一天(“2008-01-15”),或者是多久以前(“2 years 1 day 3 minutes ago”)。
git log --pretty="%h - %s" --author=自己的名字 #查看自己的日志
git log -p -2 #展开两次更新显示每次提交的内容差异
git log --stat #要快速浏览其他协作者提交的更新都作了哪些改动
git log --pretty=format:"%h - %an, %ar : %s"#定制要显示的记录格式
git log --pretty=format:'%h : %s' --date-order --graph # 拓扑顺序展示
git log --pretty=format:'%h : %s - %ad' --date=short #日期YYYY-MM-DD显示
git log <last tag> HEAD --pretty=format:%s # 只显示commit
git config --global format.pretty '%h : %s - %ad' --date=short #日期YYYY-MM-DD显示 写入全局配置
选项 | 说明 |
---|---|
%H | 提交对象(commit)的完整哈希字串 |
%h | 提交对象的简短哈希字串 |
%T | 树对象(tree)的完整哈希字串 |
%t | 树对象的简短哈希字串 |
%P | 父对象(parent)的完整哈希字串 |
%p | 父对象的简短哈希字串 |
%an | 作者(author)的名字 |
%ae | 作者的电子邮件地址 |
%ad | 作者修订日期(可以用 -date= 选项定制格式) |
%ar | 作者修订日期,按多久以前的方式显示 |
%cn | 提交者(committer)的名字 |
%ce | 提交者的电子邮件地址 |
%cd | 提交日期 |
%cr | 提交日期,按多久以前的方式显示 |
%s | 提交说明 |
以上表格列出了在Git中可用的Pretty Formats选项及其说明。您可以根据需要选择相应的选项来自定义log输出格式,例如展示作者、提交日期和提交说明等信息。