版本管理工具,Git 方案有多种,可以使用我们自己的git服务器,可以使用github,可以使用其他git厂商。master 是默认的主分支,由于业界收到种族歧视的影响,现在 github 用 main 取代了 master 字符。

常用厂家:GitHub,码云,阿里云,Coding

1. 常用命令

1.1. git remote -v

查看当前目录的远程仓库信息

origin  [email protected]:61c42bfeffb85da6bc06a415/dbzdb/dbzdb-microservice.git (fetch)
origin  [email protected]:61c42bfeffb85da6bc06a415/dbzdb/dbzdb-microservice.git (push)
 

git remote remove origin|移除远程 origin 地址,准备添加新地址 git remote add origin [email protected]:xxx.git|添加新的origin地址 git push —set-upstream origin master|将本地master分支推送到 origin master 分支。 git branch 查看本地所有分支情况,前面有*表示当前所在分支 git branch -a 查看所有分支情况,包括远程分支 git branch -d ‘local_branch_name’ 删除本地分支 git branch newBranchName|依据当前分支,新的本地分支 git checkout branchName|切换分支 放弃本地修改,还原代码。 git reset —hard git reset —hard origin/master |git pull|获取最新代码。| |git clone [email protected]:xxx.git|克隆原创代码仓库到本地。|

1.2. git reset

回滚当前分支的上一次提交,丢弃当前修改的所有内容。

git reset --hard HEAD^

1.3. git stash

当本地分支和远程修改了同一个文件代码,pull远程分支的代码的时候会出现文件冲突 出现这个错误 Please commit your changes or stash them before you merge. 可以先将当前的内容存储起来,git stash就可以把当前内容存储在栈内

git stash      # 先后再 
git pull       # 然后再pull新代码
git stash pop  # 然后再把存储在栈内的内容放出来
 
git stash list # 可以查看临时存储栈内的列表

1.4. git status

git status
git status --short

1.5. git add

git add .

1.6. git commit

git commit -m 'update'

1.7. git push

git push

2. 使用代理

由于网络环境问题,有些时候我们不能访问到目标网络,或者线路速率低。 实际上修改的文件是:~/.gitconfig 文件。

# 查看当前代理
git config --global http.proxy
git config --global https.proxy
 
# 使用 Http 代理
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
 
# 使用socks5代理
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
 
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

3. 配置 .gitignore

如下是一个配置文件样例

# Generated by Cargo
# will have compiled files and executables
 
/target/
 
# VSCode
.vscode/
 
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
 
# These are backup files generated by rustfmt
**/*.rs.bk

gitignore说明
在使用git的过程中,一般我们总会有些文件无需纳入git的管理,也不希望它们总出现在未跟踪文件列表,这些文件通常是日志文件、临时文件、编译产生的中间文件、工具自动生成的文件等等。
此时我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式,Git会根据这些模式规则来判断是否将文件添加到版本控制中。
注意:在windows下可以创建文件名为.gitignore.,保存之后系统会自动重命名为 .gitignore

格式规范
所有空行或者以注释符号 # 开头的行都会被 Git 忽略
可以使用标准的 glob 模式匹配
匹配模式最后跟斜杠(/)说明要忽略的是目录
要忽略指定模式以外的文件或目录,可以在模式前加上感叹号(!)进行取反
glob模式
所谓的 glob 模式是指 shell 所使用的简化了的正则表达式,匹配规则如下:
”*“:星号匹配零个或多个任意字符
[]:匹配任何一个列在方括号中的字符,如[ab]匹配a或者匹配b
”?”:问号匹配一个任意字符
[n-m]:匹配所有在这两个字符范围内的字符,如[0-9]表示匹配所有0到9的数字

匹配示例
logs/:忽略当前路径下的logs目录,包含logs下的所有子目录和文件
/logs.txt:忽略根目录下的logs.txt文件
.class:忽略所有后缀为.class的文件
!/classes/a.class:不忽略classes目录下的a.class文件
tmp/.txt:只忽略tmp目录下的.txt文件
**/foo:可以忽略/foo, a/foo, a/b/foo等

定义全局的.gitignore文件
除了可以在项目中定义.gitignore文件外,还可以设置全局的.gitignore文件来管理所有Git项目的行为。
这种方式在不同的项目开发者之间是不共享的,是属于项目之上Git应用级别的行为。
可以在任意目录下创建相应的.gitignore文件,然后再使用以下命令配置Git
git config —global core.excludesfile ~/.gitignore

.gitignore规则不生效
.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。所以一定要养成在项目开始就创建.gitignore文件的习惯。
解决方法就是先把本地缓存删除(改变成未track状态),然后再提交:

git rm -r —cached .

git add .
git commit -m “msg”

4. github

注册账号 [email protected] duchaoqun-x UI(jk)NM2@. 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

在本地电脑上生成密钥对。 在GitHub的用户个人信息里面添加公钥信息(id_rsa.pub)。

# .ssh/config    , add blow code.
Host github.com
 Hostname ssh.github.com
 Port 443
PubkeyAcceptedKeyTypes +ssh-rsa
HostKeyAlgorithms +ssh-rsa
 
# test if it is right.
ssh -T [email protected]

4.1. 设置你的用户名和邮箱

$ git config --global user.name "your name"
$ git config --global user.email "[email protected]"
 
# 存储用户和密码到文件,首次提交会提示输入用户名密码,之后就不会提示了。
$ git config --global credential.helper store

4.1.1. 使用Access Token

首先在Github上创建Access Token。

git remote remove origin
git remote add origin https://[email protected]/lvxinwen/fss.git
然后本地修改对应的地址
配置文件 ~/.gitconfig

[gui]
 
    encoding = utf-8
 
[user]
 
    name = duchaoqun
 
    email = [email protected]
 
[credential]
 
    helper = store
 
git remote set-url origin [email protected]:duchaoqun/duchaoqun.github.io.git

4.1.2. 在Web创建项目,本地更新

首先在github上创建一个新的项目,直接就创建好三个文件,README.md说明文档, LICENSE授权文件,.gitignore项目忽略管理,可以在页面上根据模板自动生成,非常方便。

然后把它克隆到本地git clone https://github.com/duchaoqun/aoye.git的一个目录中,然后把我们初始化出来的源代码复制到这个aoye中。然后根据经验修改 .gitignore 内容,例如.vscode。

然后再aoye目录打开 git bash 窗口,让 gitignore 文件生效,注意如果文件已经被git管理了,在设置到 gitignore 就不能生效了,要删除之后再重新设置。

git config core.excludesfile .gitignore

git add .

git commit -m ‘update’

git push

4.1.3. 在Web创建项目**,**本地初始化提交

echo "# test" >> README.md      # 创建一个文件
git init                        # 初始化一个空目录
git add README.md               # 添加一个文件
git add -A                      # 添加所有文件
git commit -m "first commit"    # 添加提交的描述信息
# 添加 origin 地址,注意https和git协议的两种不同方式
git remote add origin https://github.com/duchaoqun/test.git
 
# 推送到 origin 的 master 分支
git push -u origin master
 
# 提交代码到指定仓库
git push origin master
 
# 拉去当前分支最新代码
git pull
 
# 克隆项目到本地
git clone https://github.com/duchaoqun/ccc.git
 
# 创建分支,从远程checkout 一个新本地分支。
git checkout -b 5.3 origin/5.3    # 直接使用原来的名字
git checkout -b 5.3.1 origin/5.3  # 也可以使用一个新的名字,不推荐

4.2. 合并分支

# 合并分支:将newBranch的内容合并到当前的 master 分支
 
git merge newBranch
 
# 存在冲突的时候显示的提示信息:README.md 文件有冲突
 
Auto-merging README.md
 
CONFLICT (content): Merge conflict in README.md
 
Automatic merge failed; fix conflicts and then commit the result.
 
# 查看差异:然后手动修改差异文件后重新提交
 
git diff
 
diff --cc README.md
 
index 0815ca1,e1f8e8d..0000000
 
--- a/README.md
 
+++ b/README.md
 
@@@ -1,3 -1,2 +1,7 @@@
 
  # test
 
++<<<<<<< HEAD
 
 +
 
 +## 222222222222
 
++=======
 
+ ## 111111111
 
++>>>>>>> newBranch
 
# 准备提交,显示信息
 
git commit -a
 
 

5. codeup

https://codeup.aliyun.com 切换仓库GitHub到阿里云

前提是已经在阿里云配置好密钥

git remote remove origin 
# 留意https方式和git方式的写法,下面地址的冒号
git remote add origin [email protected]:61c42bfeffb85da6bc06a415/fss/fss.git
git push origin master
git push origin zhongjinfu
git push origin 4.0.0
git push origin dongbeizaidanbao
git push origin xinde

6. 常见问题

6.1. clone的时候出现“Peer reports incompatible or unsupported protocol version.”

CentOS 操作系统,git clone 项目时出现类似如下错误:

fatal: unable to access ‘https://github.com/rancher/rancher.git/

:Peer reports incompatible or unsupported protocol version.

解决:

yum update -y nss curl libcurl

6.2. Install git-lfs on Mac

Install “Command Line Tools”
根据操作系统版本,下载“Command Line Tools” 工具,按照到操作系统里面。
https://developer.apple.com/download/more/

Download the script of brew_install:
curl —socks5-hostname 127.0.0.1:8484 https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install

Install git-lfs with brew.
brew install git-lfs
git lfs install

执行 git lfs install 开启lfs功能
使用 git lfs track 命令进行大文件追踪 例如git lfs track "*.png" 追踪所有后缀为png的文件
使用 git lfs track 查看现有的文件追踪模式
提交代码需要将gitattributes文件提交至仓库. 它保存了文件的追踪记录
提交后运行git lfs ls-files 可以显示当前跟踪的文件列表
将代码 push 到远程仓库后,LFS 跟踪的文件会以『Git LFS』的形式显示:
clone 时 使用’git clone’ 或 git lfs clone均可

注意:

可以使用find . -size +10M -type f 找到需要标记的大文件。

在完成上述操作之后,再git add bigfile,然后push code。

6.3. git status 的时候中文显示乱码

git config --global core.quotepath false

GitHub 加速地址 https://github-hosts.tinsfox.com/hosts