博客
关于我
Git的使用命令详解
阅读量:82 次
发布时间:2019-02-26

本文共 1575 字,大约阅读时间需要 5 分钟。

在Windows上安装Git

安装Git是Windows用户的首选方式。推荐从官方网站下载安装包(如网速较慢请切换至镜像地址),选择默认参数完成安装。

安装完成后,打开“开始菜单” -> “Git” -> “Git Bash”可以看到命令行界面,确认Git已成功安装。

最后一步配置用户信息:

git config --global user.name "Your Name"git config --global user.email "email@example.com"

创建版本库

初始化Git仓库使用 git init 命令。

将文件添加到Git仓库需两步:

  • 使用 git add <file> 可多次添加多个文件。
  • 使用 git commit -m "commit message"git commit -am "commit message" 提交。
  • 版本回退

  • 使用 git reset --hard commit_id 可回到指定版本。
  • 查看提交历史使用 git log,重返未来用 git reflog
  • 撤销修改

  • 工作区修改无备份时,用 git checkout -- file 恢复。
  • 修改已入暂存区时,用 git reset HEAD <file> 后重复上述步骤。
  • 提交后发现问题,可参考上述步骤撤销。
  • 删除文件

    使用 git rm 删除文件,提交前慎重操作,无法恢复最近修改。

    远程库管理

  • 关联远程库用 git remote add origin git@server-name:path/repo-name.git
  • 推送命令:第一次用 git push -u origin master,后续用 git push origin master
  • Git的分布式特点使其不依赖远程库,仅在需要时同步更新。

    克隆仓库

    使用 git clone 克隆仓库,如:

    git clone git@github.com:michaelliao/gitskills.git

    支持多协议,ssh 最佳。

    分支管理

  • 查看分支:git branch
  • 创建分支:git branch name
  • 切换分支:git checkout name
  • 创建并切换分支:git checkout -b name
  • 合并分支:git merge name
  • 删除分支:git branch -d name
  • 解决冲突

    解决Git合并冲突时,手动编辑文件后提交。

    查看分支历史:git log --graph

    标签操作

  • 新建标签:git tag tagName
  • 添加信息:git tag -a tagName -m "msg"
  • 删除本地标签:git tag -d tagName
  • 推送标签:git push origin tagName
  • 删除远程标签:git push origin :refs/tags/tagName
  • 分支管理策略

    合并时使用 --no-ff 保留分支记录。

    Bug分支与Feature分支

  • 创建新分支修复bug后,合并回主分支,删除无用分支。
  • 工作中先 git stash 存储现场,再修复。
  • 多人协作

  • 查看远程库:git remote -v
  • 推送分支:git push origin branch-name
  • 创建对应分支:git checkout -b branch-name origin/branch-name
  • 关联分支:git branch --set-upstream branch-name origin/branch-name
  • 克隆分支:git pull
  • Rebase操作

    整理本地提交历史,使用 git rebase

    技术笔记

    本文内容基于廖雪峰技术博客整理,详细操作步骤可参考原文链接。

    转载地址:http://axtk.baihongyu.com/

    你可能感兴趣的文章
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>