Git常用命令

常用

  • 克隆
    git clone <仓库地址>

    git clone <仓库地址> <自定义仓库名>
  • 查看当前文件状态
    git status
  • 暂存
    git add <fileName>

    git add .

提交

  • 提交到本地
    git commit -m "提交说明"
  • 修改提交信息
    git commit --amend,然后按i键编辑信息— 按esc退出编辑 — 输入“:wq!”(强制储存后退出)回车

推送

  • 推送当前分支到远程
    git push origin <remoteBranchName>

分支管理:

  • 创建分支
    git branch <branchName>
  • 创建并切换到本地分支
    git checkout -b <branchName>
  • 从远程分支中创建并切换到本地分支
    git checkout -b <branchName> <remoteBranchName>
  • 把本地分支推到远程分支
    git push origin <branchName>:<remoteBranchName>
  • 本地分支关联远程分支
    git push --set-upstream origin <branchName>
  • 查看本地分支
    git branch
  • 查看远程分支
    git branch -r
  • 查看所有分支
    git branch -a
  • 查看本地分支关联情况
    git branch -vv
  • 删除本地分支
    git branch -D <branchName>
  • 删除远程分支
    git push origin --delete <remoteBranchName>

    git push origin :<remoteBranchName>

标签

  • 查看所有标签
    git tag
  • 搜索标签
    git tag -l 'v0.1.*'
  • 创建轻量标签
    git tag <tagName> -light
  • 创建附注标签
    git tag -a <tagName> -m "说明信息"
  • 切换标签
    git checkout <tagName>
  • 查看标签信息
    git show <tagName>
  • 删除标签
    git tag -d <tagName>
  • 给指定的commit打标签
    git tag -a <tagName> <commitID>
  • 推送标签到远程
    git push origin <tagname>
  • 推送本地所有标签到远程
    git push origin --tags
  • 查看某个标签状态下的文件
    1.查看当前分支下的标签
    git tag
    2.切到目标标签
    git checkout <tagName>
    3.cat <目标文件名>

常见问题

  • 回滚
    1.查看commitID
    git log
    2.覆盖本地代码
    git reset --hard commitID
    3.推送到远程分支
    git push -f -u origin <remoteBranchName>

  • 更新仓库地址
    1.查看远程名
    git remote
    2.更新仓库地址
    git remote set-url <remoteName> <仓库地址>

  • error: The last gc run reported the following. Please correct the root cause
    and remove .git/gc.log.
    解决方案:
    git reflog expire --all --stale-fix
    rm .git/gc.log

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容