目 录CONTENT

文章目录
git

Git相关操作·简

青云
2023-12-18 / 0 评论 / 1 点赞 / 29 阅读 / 7183 字
温馨提示:
本文最后更新于 2023-12-18,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

用于在远程仓库的操作

  • git remote -v
    • 查看当前 git 仓库的远程
  • git remote show [remote]
    • 显示某个远程仓库的信息
  • git remote add [shortname] [url]
    • 添加远程版本库, shortname 为本地版本库
  • git remote rm [name]
    • 删除远程仓库
  • git remote rename [old_name] [new_name]
    • 修改仓库名

详情看:连接远程仓库remote

版本回退(+撤回上一次提交)

git reset --soft HEAD^
- 撤回上一次的 commit 操作
- 等同于 git reset --soft HEAD~1
- 如果进行了两次 commit 提交,都想撤回,可以使用 git reset --soft HEAD~2

  • 扩展
    • --mixed
      • 不删除工作空间改动代码,撤销 commit,并且撤销 git add . 操作,这个为默认参数git reset --mixed HEAD^ = git reset HEAD^
    • --soft
      • 不删除工作空间改动代码,撤销 commit,不撤销 git add .
    • --hard
      • 删除工作空间改动代码,撤销 commit 并撤销 git add .
      • **注意:此操作后就恢复到了上一次的 ****commit**状态。
    • git commit --amend
      • 如果只是 commit 注释写错了,只想改一下注释,只需要使用该命令,此时会进入默认 vim 编辑器,修改注释完成后保存就好了。
      • 关联:

; 分割多个 git 命令

  • 如题

别名

  • 命名别名能提高可重用性

    • git config --global alias.[alias name] '[command]'
      使用  git config --global alias.show-origin 'remote -v' ' 将  show-origin  作为  remote -v  的别名
      使用 git config --global alias.show-origin 'remote -v' ' 将 show-origin 作为 remote -v 的别名

    效果

设置代理 proxy

  • 在有条件的情况下, 设置代理能为我们节省很多时间, 让我们开始
    # http/https 协议, port 需与代理软件设置一致
    git config --global http.proxy http://127.0.0.1:port
    # socks5 协议
    git config --global http.proxy socks5://127.0.0.1:port
    
    详情看:设置代理 proxy

设置全局 .gitignore

  • 在根目录下的.gitignore_global文件中(没有则新增)添加忽略.DS_Store文件规则
    • echo .DS_Store >> ~/.gitignore_global

      成功新增之后

    • 将这个全局的 .gitignore文件加入Gitconfig

      • git config --global core.excludesfile ~/.gitignore_global

分支

详细:分支操作

本地分支

  • 查看所有分支(本地和远程)
    • git branch -a

  • 查看本地分支,并且当前分支会用*标记
    • git branch

  • 创建新分支
    • git branch 新分支名称

  • 切换分支
    • git checkout 分支名称

  • 创建分支的同时, 切换到该分支上
    • git checkout -b 新分支名称
  • 删除本地分支
    • git branch -d 分支名称

      • 如果删除时报错:error: The branch '分支名称' is not fully merged.(分支未完全合并),可以使用强制删除
        • git branch -D 分支名称

远程

  • 查看远程分支列表
    • git branch -r

  • 删除远程分支
    • git push origin :分支名称
      • ⚠️注意:分支名称前面有个冒号, 分支名称前的冒号代表删除
1

评论区