放肆青春的博客
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)

放肆青春

一个前端菜鸟的技术成长之路
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)
  • 开发相关

    • 项目管理

      • 团队工具
      • git

        • git 汇总

          • git
          • git 总结
          • git 配置
          • git 命令
            • git 常用命令
            • git 回退代码命令
              • 1. add 之后回退
              • 2. commit 之后回退
              • 3. push 之后回退
            • git 分支命令
            • git 远程仓库命令
            • git help(帮助命令)
            • git 打标签
            • git rebase
            • git 其它命令
            • git 命令应用
              • git 初始化到提交流程
              • 使.gitignore 中的内容生效
              • 创建忽略文件:
          • github
          • gitee
          • svn
        • git blog

      • UI 及 UE工具
      • 代码管理
      • jenkins
      • sonar
      • 移动端调试
      • nexus
    • 开发工具

      • node
      • npm
      • yarn
      • pm2
      • pnpm
      • safe
      • eslint
      • gitbook
      • vuepress 总结
      • vitepress
      • markdown
    • 开发软件

      • software
      • vscode
      • postman
      • SourceTree
      • idea
      • sublime
      • notepad
      • ediplus
      • xmind
    • 浏览器

      • chrome 汇总
      • chrome lighthouse
      • firefox
  • office

    • excel

      • excel
    • word

      • word
    • ppt

      • ppt
  • 实用工具

    • ps
    • premiere
  • tools
放肆青春
2020-07-16

git 命令

# git 常用命令

  • 添加远程仓库 git remote add origin 远程仓库地址

  • 本地分支关联远程分支 git branch --set-upstream-to=origin/dev dev

  • 强制推送远程 git push -f origin master

# git 回退代码命令

# 1. add 之后回退

本地仓库代码覆盖缓存区代码: git reset

// 将本地仓库某一文件覆盖缓存区:
git reset HEAD testReset.txt

// 将匹配的文件覆盖缓存区:
git reset HEAD *.txt

// 将所有文件覆盖缓存区:
git reset HEAD .
1
2
3
4
5
6
7
8

注意: 改变的是缓存区代码, 工作区间代码不变(编辑器代码不会改变)

# 2. commit 之后回退

(1) 本地仓库代码覆盖工作区代码 git checkout

// 将本地仓库某一文件代码 覆盖本地工作区:
git checkout head testReset.txt

// 将本地仓库所有文件代码 覆盖本地工作区:(谨慎操作):
git checkout head .
1
2
3
4
5

(2) 本地仓库代码覆盖工作区代码 git reset --hard

查看 commit 列表

// 查看commit id, 查看提交记录(git commit的记录)
git log
git log --pretty=oneline

// 查看以往提交历史(包括 撤销回退 记录)
git reflog
1
2
3
4
5
6

根据 commit 回退

// 本地工作区间代码 回退到上一次版本、上上次、前10个版本
git reset --hard HEAD^
git reset --hard HEAD^^
git reset --hard HEAD~10

// 本地工作区间代码 回退到指定版本(“d362816”为commit id)
git reset --hard d362816
1
2
3
4
5
6
7

(3) 远程仓库代码覆盖本地仓库代码(清除 未 push 的 commit)

// 本地工作区间代码回退到远程版本
git reset –-hard origin/master
1
2

# 3. push 之后回退

远程仓库代码回滚(线上代码回滚)

  1. 使用git revert将新的 commit 替换掉(回退某一个或极少个 commit)
// 替换掉上次提交的代码文件(上次的commit记录会保留)
git revert HEAD
git commit -m "回滚上次commit"
git push origin master
1
2
3
4
  1. git 回退某个 merge 节点

(1) 先试试直接 revert

git revert commit_id // merge的那个节点commit id

报错了,报了一个 Commit is a merge but no -m option was given.

原因:这是一个 merge 的提交。那么在撤销时,git 并不知道我要撤销具体哪次

(2) revert merge 的第一次提交

git revert commit_id -m 1 // 参数 -m 就是指定要撤销的那个commit

(3) 解决冲突,然后 add - commit - push

  1. git reset 回退到某个 commit(回退许多个 commit)

1.查找 commit_id,git log

2.重置命令 git reset --hard commit_id

回滚到上个版本 git reset --hard HEAD^ 回滚到前 3 次提交之前,以此类推,回退到 n 次提交之前 git reset --hard HEAD~3 回滚到指定 commit 的 sha 版本 git reset --hard commit_id

3.强制推送到远程 git push origin branch --force

参考 blog:https://segmentfault.com/a/1190000007070302 (opens new window)

# git 分支命令

  • 查看 git 的所有本地分支 git branch

  • 查看远程分支 git branch -r

  • 创建 dev 分支 git branch dev

  • 切换分支 git checkout dev

  • 创建分支,并切换到 dev 分支 git checkout -b dev

  • 创建 dev 分支并关联远程分支 git checkout --track origin/branch_name

  • 根据远程 dev 创建本地 dev 分支但没有关联,并切换到 dev 分支 git checkout -b dev origin/dev

  • 删除本地 dev 分支 git branch -d dev

  • 合并分支 git merge dev

git merge –no-ff 可以保存你之前的分支历史。能够更好的查看 merge 历史,以及 branch 状态。

  • 本地分支关联远程分支 git branch --set-upstream-to=origin/dev dev
  • 重命名分支

重命名 git branch -m oldBranchName newBranchName

删除远程分支 git push origin :oldBranchName

将重命名过的分支提交 git push origin newBranchName

# git 远程仓库命令

  • 克隆远程指定分支 git clone -b <分支名> <仓库地址>

  • 查看远程仓库 git remote -v

  • 删除远程仓库 git remote rm <仓库名>

  • 从远程库克隆项目 git clone <仓库地址>

  • 本地分支关联远程分支 git branch --set-upstream-to=origin/dev dev

  • 强制推送远程 git push -f origin master

  • 重命名分支

重命名 git branch -m oldBranchName newBranchName

删除远程分支 git push origin :oldBranchName

将重命名过的分支提交 git push origin newBranchName

# git help(帮助命令)

  • 展示 config 的相关命令

    git config -h

  • 会通过浏览器打开本地帮助文档

    git help config

# git 打标签

# git rebase

  • 使用 rebase 拉取代码 git pull --rebase

  • 解决冲突后使用 git add 表示冲突已经解决

  • 继续下一个冲突 git rebase --continue

  • 跳过冲突 git rebase --skip

  • 退出 rebase 模式 git rebase --abort

  • 设置 rebase 为 pull 时候默认执行的动作 git config --global pull.rebase true

# git 其它命令

  • 一个 commit 应用于多个分支 cherry-pick

develop 分支有个 commit,test 下输入命令:git cherry-pick develop,会把 develop 唔知的最新一个 commit 应用到 test 分支

  • 添加远程仓库 git remote add origin 远程仓库地址

# git 命令应用

# git 初始化到提交流程

  • git init

  • git add .

  • git commit -m "first commit"

  • git remote add origin https://git.oschina.net/name/package.git

  • git push -u origin master

# 使.gitignore 中的内容生效

  • git rm -r --cached .
  • git add .
  • git commit -m 'update .gitignore'

# 创建忽略文件:

touch .gitignore

git 阮一峰总结命令: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html (opens new window)

更新时间: 12/22/2021, 6:20:36 PM
git 配置
github

← git 配置 github→

最近更新
01
前端权限管理
02-24
02
vue2指令
02-24
03
vue2 hook
02-24
更多文章>
Theme by Vdoing | Copyright © 2019-2022 放肆青春
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式