Featured image of post Git 代码及一切文本档案的管理神器

Git 代码及一切文本档案的管理神器

Git Usage

简介

Git 是 Linux 之父设计的分布式版本控制软件,最出色的代码管理利器。 Git不只能用于代码的管理,一切配置文件、文本档案,甚至多媒体大文件,都很适合用Git来追踪变化,异步写作开发。 本文介绍 Git 的基本操作,并记录一些复杂需求的操作流程。

Discern

Github 是一个使用Git机制提供集中代码和文档追踪服务的平台,和Git没有官方联系。

安装

  • Windows
    • Windows版本在Github分发
      • 2.46.2是最后支持Windows7的版本
    • 使用setup安装时建议换行符方式选择 Checkout as is, commit as is
  • Linux发行版仓库中一般带有 git

相关配套工具和插件

  • git-lfs
    • 集成在Windows版本中,Linux需额外从发行版仓库安装。
    • 用于追踪大文件减少空间消耗

配置

配置文件位置

  • 当前用户的配置在 .gitconfig
  • 每个仓库的配置文件在 .git/config
  • 每个文件夹中的配置文件
    • .gitignore 设置追踪变更时忽视的路径
    • .gitattribution 设置每个文件追踪属性如不记录差异

配置命令

git config [级别] [选项] 变量 值

  • 级别有 –system –global –local –worktree –file 等
  • 选项有 –list –unset 等
  • 全局快速合并 git config –global pull.ff=only
  • 设置基本信息
    • git config user.name [用户名]
    • git config user.email [电子邮件]
  • 设置http/https协议的代理 git config http.[网址.]proxy “协议://IP地址:端口”
    • git config http.proxy “socks5h://127.0.0.1:1080”
    • git config http.https://github.com.proxy “socks5h://127.0.0.1:1080”
    • git config –local remote.<远端名称>.proxy ""
  • 设置SSH(代理用ssh方式设置)

基本使用

创建本地仓库

1
2
3
4
5
git init
git config --local --add user.name [意向使用的用户名如user]
git config --local --add user.email [意向使用的邮箱如user@example.com]
git branch -M [主分支名,如main]
git remote add [远端名称,默认origin] [远端URL]

克隆和拉取

  • 子模块
    • 直接一起克隆 git clone –recurse-submodules
    • 克隆后在拉取
      • git clone 链接
      • git submodule init
      • git submodule update
        • --remote 选项可以解决 did not contain xxxxxxxxx 类的问题
  • 精简克隆

提交

1
2
3
4
git add 文件
git commit "说明"
git reset --soft HEAD^	# 本地撤销commit
git push -u [远端名称,默认origin] [本地分支,如main]

分支

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 查看分支
git branch -a

# 创建分支
git checkout -b [分支名]

# 列出标签
git tag [-l | --list]

# 创建标签
git tag -a 标签名 [-m 信息 | -F 信息文件]

# 删除标签
git tag -d 标签名

# 切换分支
git checkout [分支名]

# 切换到标签
git checkout [标签名 | tags/标签名]

流程参考

PullRequest

通常实在网络平台上使用git用此操作进行贡献

  • fork别人的仓库
  • 做修改并提交
  • 在自己fork的仓库新建PullRequest
  • [可选]跟进上游进展 git remote add upstream https://[上游仓库地址].git

https://github.com/firstcontributions/first-contributions

GitLFS

  • 追踪 git lfs track 文件在仓库路径如Patch/test.bin
  • 添加配置 git add .gitattributes
  • 正常添加 it add 文件在仓库路径如Patch/test.bin
  • 正常提交 git commit -m "说明备注"

不小心提交敏感信息

  • 本地撤销commit git reset --soft HEAD^
  • 创建新分支 git branch -b master
  • 重新提交 git push
  • 在github中Settings切换默认分支
  • 在github中Code的分支菜单中View all branches删除原分支
  • 可以考虑再把新分支master重命名回去
    1
    2
    3
    4
    
    git branch -m master 原分支名
    git fetch github
    git branch -u github/原分支名 原分支名
    git remote set-head github -a
    

其他

Git相关的 GUI、WebUI、Server 等另开一篇博文介绍。

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy