Git

Git Repo Initialization

Git仓库初始化

Posted by Hao on January 9, 2021

Git及Git仓库初始化

How to configure the branch name with highlight in ubuntu terminal?

Add those lines in ~/.bashrc

1
2
3
4
5
6
7
8
9
10
# git branch highlight

parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi

The following part should be replaced/commented

1
2
3
4
5
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

初始化repo的不同方式

  1. 从远程拉到本地
1
2
3
4
5
6
git clone <repo_name>
cd repo_name
git switch -c main
touch README.md
git add README.md
git commit -m "add README"

执行此段代码之后,可以显示此时正在使用的branch

  1. 将对应的本地文件夹连接到远程仓库

  2. 将本地的已有仓库连接到远程仓库

Git Init

在对应文件夹下执行git init命令,则会在当前目录生成一个.git目录;同理也可以制定目录通过命令 git init directory_name

此命令会在对应的目录下产生一个.git文件夹,通过第一次运行git addgit commit命令后,则会自动创建master branch。

git global ignore

The global git ignore file exists in ~/.gitignore_global or some name you defined, and we could run the command to configure for all projects.

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

如何链接到远程仓库并推送到远程仓库

配置当前用户及远程仓库可以通过直接修改.git/config内的配置或者

  • git remote add origin <url> 链接到远程仓库及配置用户名和其余信息等

其余要通过先创建本地仓库,之后再推送到远程端

PS:直接建立远程仓库后通过git clone下拉,并将代码移植过去是最简单的方法,不需要自己配置本地git信息

Git Config配置

config 配置有system级别 global(用户级别) 和local(当前仓库)三个 设置先从system-》global-》local 底层配置会覆盖顶层配置 分别使用–system/global/local 可以定位到配置文件

例如通过git config --system --list查询配置,其一般配置例如lfs等相关设置。

Git Config分为全局配置当前仓库配置系统级的配置,信息被记录在对应项目的.git/.config文件中;其中全局配置位于当前用户目录下.gitconfig文件中,而系统级别的配置粗处在/etc/gitconfig文件中,包含系统上每一个用户及他们仓库的通用配置。如果在执行 git config 时带上 –system 选项,那么它就会读写该文件中的配置变量。 (由于它是系统配置文件,因此你需要管理员或超级用户权限来修改它。)

  • 查询当前仓库的git配置:git config –local –list
  • 查询当前用户的git配置:git config –global –list

unsafe repository

当git项目的所有者为root用户时,其他用户进行对该库的调用,会造成git unsafe repository的报错,因此需要通过git config --global --add safe.directory的形式添加该目录。

我现在所做的,但可能不是一个完美的解决方案,就是找到所有的.git文件夹,并通过一个find命令添加它们。

1
find /full/path -name '.git' -type d -exec bash -c 'git config --global --add safe.directory ${0%/.git}' {} \;

或者通过全局配置所有的文件夹

1
git config --global --add safe.directory '*'

产生这一问题的本质原因是下载代码的所有权没有转移,所以,在修改代码时会报以上问题。因此除了通过修改safe.directory,也可以通过将文件下该目录的所有权转移给用户的形式来解决此问题

1
2
whoami
chown -R 用户名:用户组 .

url..insteadOf

以下是重写GitHub默认协议的示例:

1
2
3
git config --global url.https://github.com/.insteadOf git://github.com/
# 同理通过字符串
git config --global url."https://".insteadOf git://

这是一条 Git 配置命令,它的作用是让 Git 将某个 URL 地址映射到另一个 URL 地址。

具体来说,这条命令表示对于 Git 中的所有仓库,当使用 “git://” 协议进行远程仓库链接时,Git 将自动使用 “https://” 协议来代替。原因:当你想去克隆一个别人github上的repository时,发现系统不让你动,提示你防火墙禁止对git://的访问,这时候就只能用https://来访问repository。

git config [--global] url.<base>.insteadOf <other_url>: 任何以开头的URL都将被重写为以开头。当多个insteadOf字符串与给定的URL匹配时,将使用最长的匹配。

“git config –global” 命令可以用来配置 Git 的全局配置文件,”insteadOf” 配置项可以帮助您将一个 URL 映射到另一个 URL。它的语法如下:

1
git config --global url."<url-to-be-replaced>".insteadOf "<replacement-url>"

例如,如果您想将所有对 “old-company.com/repo.git” 的访问都映射到 “new-company.com/repo.git”,通过使用命令

1
git config --global url."https://old-company.com/repo.git".insteadOf "https://new-company.com/repo.git"

还可以在insteadOf选项中使用通配符来匹配多个URL。例如,以下配置将匹配所有以git://github.com/开头的URL:

1
2
3
# 此内容出现在.gitconfig文件中
[url "https://github.com/"]
    insteadOf = git://github.com/

取消config配置

在设置了部分 git 的 config 配置文件之后,需要取消对应修改:

使用对应的命令:

1
git config --global --unset <key> 

检索所有已有的 config 配置

1
2
3
4
5
6
$ git config --list
 
user.name=Mukvin
user.email= ...
...
url.git://.insteadof=https://

Example 预期,比如想取消 url.git://.insteadof=https://

1
git config --global --unset url.git://.insteadof

Username & Email

  • git config –global user.name “Hao Ban”
  • git config –global user.email “Hao.Ban@outlook.com”

配置取消windows下的长路径限制

  • git config –system core.longpaths true

Git SSH 配置 (非必需,可通过https url连接)

SSH key的默认路径为~/.sshC:\Users\Administrator\.ssh,进入该路径并查看是否存在id_isaid_isa.pub文件,或者同类型的不同名文件。

如果不存在,通过命令生成SSH

  • 直接使用默认命令ssh-keygenssh-keygen -t rsa -C username@email -f ~/.ssh/your_id_rsa.pub,选择是否需要修改文件名(防止覆盖其他密钥),输入yes确认路径,确认是否需要密码等。

    代码参数含义:

    • -t 指定密钥类型,默认是 rsa ,可以省略。
    • -C 设置注释文字,比如邮箱。
    • -f 指定密钥文件存储文件名。

    PS:如果需要对密钥文件进行重命名,或者配置多个公钥给当前用户,则需要添加到配置文件中~/.ssh/config

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
     # gitee
     Host gitee.com
     HostName gitee.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/gitee_id_rsa
     # github
     Host github.com
     HostName github.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/github_id_rsa
    

    单一用户生成多个git ssh密钥的方法:通过SSH-agent处理多个密钥

  • 打开git个人设置,将SSH keys输出cat ~/.ssh/id_rsa.pub,并拷贝到git SSH keys下, (windows下可通过命令clip < C:\Users\Administrator\.ssh\id_rsa.pub进行复制),通过选项Add SSH key添加密钥

  • (非必需)在本地通过ssh-agent添加RSA密钥对
    1
    2
    3
    4
    5
    
     $ eval `ssh-agent`
     > Agent pid 1853
    
     $ ssh-add ~/.ssh/id_rsa
     > Identity added: /c/Users/Administrator/.ssh/id_rsa (Administrator@DEEP-2019KBQGOA)
    
  • 测试是否配置成功: ssh -T git@github.com

    如果出现You’ve successfully authenticated, but GitHub does not provide shell access,则证明尝试访问的路径与库所链接的远程库路径不同,可以查看当前库链接的远程库地址

    1
    
     git remote -v
    

    如果一开始是通过https url形式clone的库,则可以用修改.config文件下的远程仓库(origin)的url来更新成SSH协议来实现每次更新。

SSH配置时遇到的各种问题:

  • 如需添加邮箱,则确认好添加的邮箱地址是否为Primary邮箱
  • 查看当前文件夹是否为/.ssh文件夹,而非/ssh文件夹

Alias

  • 将st命名为status的别名: git config –global alias.st status
  • 删除名为st的别名: git config –global –unset alias.st

常用的Alias

1
2
3
4
5
6
7
8
9
10
11
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.last "log -1 HEAD"
git config --global alias.cane "commit --amend --no-edit"
git config --global alias.pr "pull --rebase"
git config --global alias.cm "commit -m"
git config --global alias.cam "commit -a -m"
git config --global alias.rh "reset --hard"
git config --global alias.caan "commit -a --amend --no-edit"

此为常用的git命令alias,同理可配置命令至.bashrc来获取更简单的系统指令。

Merge & Difftools 及其他

  • 可用于配置git diffgit merge工具,例如meld,kaleidoscope等
  • 可用于指定使用的editor等
  • 可用于定义默认的git pullgit push类型: git config –global push.default matching
  • 可用于定义默认的git branch是否使用自动合并功能:git config –global branch.autoSetupMerge always

Git进阶

Repo下.gitignore文件

针对于每次git add操作时,在.gitignore下的文件不会被添加到每次的commit中,常用的添加包括编译生成文件,项目文件,临时文件等。例如CMakelist.user, .sln文件(Visual Studio的项目文件)等。在Github/gitignore仓库中有针对不同的编程语言写的.gitignore的模板,可以直接使用,根据需要添加自己需要的文件等。

PS: windows下由于文件名格式不能为空,则可以先命名为.gitignore.,回车确定之后windows会自动将最后一个点去掉。

Repo下.gitattribute文件

可以针对特定的路径或者文件类型配置某些设置项,这样Git就只对特定的子目录或子文件集运用它们。 这些基于路径的设置项被称为 Git 属性。也能用来指定lfs(large file system)相关内容,并指定diff,filter和merge等默认操作。

Case 1: 在进行对 Microsoft Word 文档进行版本控制时,文件会被识别成二进制文件以比较不同。在运行 git diff 命令后,你只能得到如下的结果:

1
2
3
4
$ git diff
diff --git a/chapter1.docx b/chapter1.docx
index 88839c4..4afcb7c 100644
Binary files a/chapter1.docx and b/chapter1.docx differ

Git属性能很好地解决此问题。 把下面这行文本加到你的.gitattributes文件中:

1
*.docx diff=word

这告诉Git当你尝试查看包含变更的比较结果时,所有匹配.docx模式的文件都应该使用“word”过滤器。我们会对Git进行配置,令其能够借助docx2txt程序将Word文档转为可读文本文件,这样不同的文件间就能够正确比较了。详细内容可访问Git官方文档

Case 2: core.autocrlf 配置依赖

image

image

Reference