git常用命令

  |  

git submodule add xx.git xx    添加子模块

git submodule init         初始化子模块

git submodule update    更新子模块

git branch xx         创建本地分支
git branch 查看本地分支
git branch -r 查看远程分支
git branch -a 查看所有分支
git branch –set-upstream-to=origin/api api 本地关联远程分支

git push origin xx      创建远程分支

git push origin –delete xx 删除远程分支
git branch -d dev_20181018 删除本地分支
git branch -D dev_20181018 如果删除不了可以强制删除

git merge xx            合并分支

git submodule foreach git checkout v5.4.0 循环修改子模块分支

git remote set-url origin https://*.git 更换远程分支

git remote add origin url 添加 rul

git submodule sync 子模块同步

git checkout -b mybranch 创建并切换分支

git 删除/撤销远已经 push 到程服务器上某次代码提交

1
2
3
4
5
git reset --hard <commit_id>
git push origin HEAD --force

git reset --hard 126f206185f225879f2723ca421f4dee44ca8fe7
git push origin HEAD --force

git 如何撤销 commit(未 push)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#撤销commit一般用git reset ,语法如下:
git reset [ --mixed | --soft | --hard] [<commit ID>]
#使用参数--mixed(默认参数),如git reset --mixed <commit ID>或git reset <commit ID>
撤销git commit,撤销git add,保留编辑器改动代码

#使用参数--soft,如git reset --soft<commit ID>
撤销git commit,不撤销git add,保留编辑器改动代码

#使用参数--hard,如git reset --hard <commit ID>——此方式非常暴力,全部撤销,慎用
撤销git commit,撤销git add,删除编辑器改动代码

git reset HEAD^ # 此时代码保留,回到 git add 之前

git reset --hard HEAD^ # 回到最新的一次提交

git log 与 git reflog 的 区别

1
2
3
git log: commit 的版本日志 包含提交的版本 操作者 日期 (方便查看commit的版本,但是版本回退后,使用git log 看不到回退版本号之后的版本记录)
git reflog: 使用git 命令进行操作的日志 包括当前步骤所在哪个版本(一个commit 产生一个版本, 指定版本回退只能回退到该commit) 以及操作的具体内容
版本回退后,仍然可以看到所有的版本记录 方便查看每个操作步骤所在的版本,可以根据版本号自由前进后退

指定子模块分支
branch = tri
在这里插入图片描述

git commit --amend
这时候会进入 vim 编辑器,修改完成你要的注释后保存即可。
git commit --amend -m '提交'
使用一次新的 commit,替代上一次提交

代码 git add 到缓存区,并未 commit 提交

1
2
git reset HEAD .  或者
git reset HEAD a.txt

通过 git revert 用一次新的 commit 来回滚之前的 commit

1
2
git log # 得到你需要回退一次提交的commit id
git revert <commit_id> # 撤销指定的版本,撤销也会作为一次提交进行保存

注:

  1. git revert 和 git reset 的区别
    git revert 用一次新的 commit 来回滚之前的 commit,此次提交之前的 commit 都会被保留, revert 是放弃指定提交的修改,但是会生成一次新的提交,需要填写提交注释,以前的历史记录都在;;
    git reset 回到某次提交,提交及之前的 commit 都会被保留,但是此 commit id 之后的修改都会被删除

git 设置本地分支跟踪远程分支

1
2
3
1. git  checkout -b brach-name origin/branch-name
2. git branch --set-upstream branch-name origin/branch-name
3. git branch -u origin/branch-name

标签

1
2
3
4
5
6
7
8
9
git tag 标签名 打在最新提交的commit上
git tag 查询所有标签
git tag 标签名 f52c633 给特定的commit打标签
git tag -a 标签名 -m "msg" commit的id 给标签设置说明
git show 标签名 查询标签内容
git tag -d 标签名 删除标签
git push origin 标签名 推送某个标签到远程
git push origin --tags 推送所有标签
git push origin :refs/tags/<tagname> 可以删除一个远程标签。

存储

1
2
3
4
5
6
git stash list 查询旧存储列表
git stash create [<message>] 创建存储
git stash drop 删除最新的那一个 等同于git stash drop stash@{0}
git stash drop stash@{4} 指定删除
git stash clear 删除所有
git stash --help 查看帮助

‘git help gc’ for manual housekeeping

  1. 查询原因 git fsck
  2. 解决问题 git gc --prune=now

其他命令参考文档
git 命令大全

文章目录